ProGuard leads to NullPointerException

2019-07-11 18:42发布

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!

5条回答
劳资没心,怎么记你
2楼-- · 2019-07-11 18:45

The idea is to keep the files provided from the libraries you included in your project. I'm sharing one of my proguard-rules.pro which might help you to understand.

-keep class com.google.** { *; }
-keep class com.github.** { *; }
-keep class org.apache.** { *; }
-keep class com.android.** { *; }
-keep class junit.** { *; }
-keep class android.support.v7.widget.SearchView { *; }
-keep class com.myproject.model.** { *; }

Hope that helps!

查看更多
\"骚年 ilove
3楼-- · 2019-07-11 18:48

most of the time the error because of android libs, try this

-keep interface android.support.** { *; }
-keep class android.support.** { *; }
查看更多
Emotional °昔
4楼-- · 2019-07-11 18:48

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.

查看更多
\"骚年 ilove
5楼-- · 2019-07-11 18:50

For me, adding lines (you may have other name for package where you put your POJO files):

-keep class [mypackagename].model.** { *; }
-keep class [mypackagename].datamodel.** { *; }

to proguard.rules worked perfectly, then options:

android
{
    ...

    buildTypes {

        release {

            minifyEnabled true 
            shrinkResources true

        }
    }
}

are set in build.gradle (Module: app)

查看更多
干净又极端
6楼-- · 2019-07-11 18:52

I solved the problem:

After running Clean Project in Android Studio I got an error about a file called resource-release-stripped.ap_ that cannot be found. Some searching via Google showed up that I need to disable shrinkResources in the gradle file because it is not compatible with Jack.

查看更多
登录 后发表回答