ProGuard leads to NullPointerException

xani picture xani · Jan 2, 2017 · Viewed 7.6k times · Source

When setting minifyEnabled true in my gradle I get a NullPointerException when starting my app:

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ae.formulaecalendar/de.ae.formulaecalendar.view.calendar.CalendarActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
   at de.ae.formulaecalendar.view.calendar.CalendarActivity.onCreate(Unknown Source)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1367) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

While reading through StackOverflow I found some settings for the proguard-rules.pro file:

######### KEEP ANDROID SUPPORT V7 AND DESIGN
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

which didn't really solve this problem (but a previous NullPointerException).

Finally the important part of my gradle:

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

When I disbale minifyEnabled everthing works fine.

Thanks for your help!

Answer

Tobias picture Tobias · Nov 6, 2017

It's likely that this happens, when serializing objects and the fields are not annotated by @SerializedName("param") (or similar) and therefor can not be found.

For debugging it also helps to temporarily set -keepnames class ** so that the class names are not obfuscated.