Search code examples
androidandroid-studioapkproguardandroid-proguard

App Crash after signing app with resource shrinking and Proguard


This is the first time I got this type of exception while building a signed APK. I searched a lot but could not find any useful information to fix this.

Any information will help me a lot. Because I already spend a lot of time fixing this. I also want to reduce the APK size.

I am creating a signed APK with below release settings and proguard

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

Proguard:

-dontwarn okhttp3.internal.platform.*
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
-dontwarn javax.annotation.**
-dontwarn kotlin.Unit
-dontwarn retrofit2.-KotlinExtensions

After building a signed APK, I tried testing that and found that google and facebook login is not working (I am not using any other way to log in) and also app crash when I open Privacy policy or Terms Activity which request an API endpoint using Retrofit 2.

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

Below is the logcat of the crash.

2018-12-31 15:24:42.054 15126-15126/com.sathi E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.sathi.c.a.i$a.a()' on a null object reference
        at com.sathi.activities.PrivacyPolicyActivity$1.a(Unknown Source)
        at com.sathi.c.d$3.a(Unknown Source)
        at d.h$a$1$1.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
2018-12-31 15:24:42.281 15126-15126/com.sathi E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sathi, PID: 15126
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.sathi.c.a.i$a.a()' on a null object reference
        at com.sathi.activities.PrivacyPolicyActivity$1.a(Unknown Source)
        at com.sathi.c.d$3.a(Unknown Source)
        at d.h$a$1$1.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

I don't know what is the cause of this exception. Any information will help. Thanks in advance!

Update:

These are the Java Compile Warning which I am getting while building a sign APK file without Proguard but keeping minifyEnabled true.

enter image description here

Answer:

Thanks to Reaz's Answer The issue was the model class which I used to get the response from Retrofit

I added below line in my Proguard

-keepclassmembers class com.sathi.models.** { *; }

Solution

  • You need to keep the classes. Turning the warnings down will not keep the classes and will shrink accordingly by proguard. In that case, the libraries from Google or Facebook might behave incorrectly.

    Same thing goes for the classes used to call the APIs. When you are trying to serialize the JSON data from your API response into a class, the class members should have the exact names where the values returned can be mapped correctly.

    I am sharing the rules from my project for your reference. Hope that helps!

    -keep class com.google.** { *; }
    -keep class com.github.** { *; }
    -keep class com.android.** { *; }
    -keep class junit.** { *; }
    -keep class com.example.com.mymodels.** { *; }
    -keepclassmembers class com.example.com.mymodels.** { *; }
    

    From your logcat, I think the model classes that you are using for parsing the responses from your API is causing the error. Can you please keep the classes and let me know if that crashes your program?