App cannot build when using ProGuard in Android Studio

1.1k views Asked by At

I am using lasted version of Android Studio, and i have obfuscated my project by ProGuard in Android Studio.

Content of Build.gradlesuch as this:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

but when start the build project, an error like that see error

warning Image : see warning

How can I fix this?

1

There are 1 answers

1
Jamie On BEST ANSWER

The short answer

If you are completely sure that your app works as expected with other build types (debug), then suppress the warning by adding the following to your proguard-rules.pro file

-dontwarn com.github.siyamed.shapeimageview.**


The long answer

The warning says that it can't find the referenced class org.kxml2.io.KXmlParser. KXmlParser is only used in SvgToPath.java as illustrated below:

import org.kxml2.io.KXmlParser;
...
public class SvgToPath {
    ...
    private static PathInfo parse(InputStream in, boolean ignoreDefs, float dpi) {
            try {
                XmlPullParser xr = new KXmlParser();
                ...
            } catch (Exception e) {
                Log.w(TAG, "Parse error: " + e);
                throw new RuntimeException(e);
            }
    }
    ...
}

I think that this results in one of two potential reasons for why it fails:

  1. The library you are using does not include org.kxml2.io.KXmlParser, so you would have to include it yourself
  2. Proguard is breaking com.github.siyamed.shapeimageview or org.kxml2.io.KXmlParser by obfuscating their code.

If you think Proguard is breaking your builds then consider the following

At the end of the day the libraries that you and most people use are nearly always open source. Obfuscating open source code provides no benefit as any potential attacker could just look up the source code online.

You can keep the classes (stopping them being obfuscated by Proguard) by adding the following to your proguard-rules.pro file:

-keep class com.github.siyamed.shapeimageview.** { *; }

-keep interface com.github.siyamed.shapeimageview.** { *; }

-keep class org.kxml2.io.KXmlParser.** { *; }

-keep interface org.kxml2.io.KXmlParser.** { *; }