2

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!

Denis Dmitrienko
  • 1,532
  • 2
  • 16
  • 26
xani
  • 784
  • 1
  • 7
  • 21
  • The question is a bit unclear to me. Keeping the classes did solve the null pointer exception? And can you please post the code of `CalendarActivity.onCreate()`? – Reaz Murshed Jan 02 '17 at 16:44
  • Please make an [SSCCE](http://sscce.org/). – m0skit0 Jan 02 '17 at 17:00
  • @Reaz Murshed: Before this NullPointerException occured there was a previous one caused by ProGuard which I could resolve with the posted proguard rules. But these rules don't help with this NullPointerException and I don't know why. Sorry if it was unclear. I added the onCreate and the layout xml. – xani Jan 02 '17 at 17:01
  • @m0skit0: I will try to make one – xani Jan 02 '17 at 17:04
  • Proguard rules for common Android libraries [proguard-rules](https://gist.github.com/jemshit/767ab25a9670eb0083bafa65f8d786bb) – Hanieh nikjoo Aug 11 '19 at 06:55

4 Answers4

12

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.

Tobias
  • 7,282
  • 6
  • 63
  • 85
8

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)

Denis Dmitrienko
  • 1,532
  • 2
  • 16
  • 26
5

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!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Thanks for your answer. For me it looks like it cannot find `android.support.v7.widget.RecyclerView` or `android.support.v7.widget.RecyclerView.LayoutManager` which is part of one of the libraries included in my project (`compile 'com.android.support:recyclerview-v7:25.0.0'`). That's why I added `-keep class android.support.v7.** { *; }` to my proguard rules, without any success. – xani Jan 02 '17 at 17:38
  • You need to keep the class started with `com.android.` too. Check the list I've added. These are the usual classes you need to keep. – Reaz Murshed Jan 02 '17 at 17:44
  • 1
    I solved it. It was a problem with `shrinkResources` in the gradle file. However, thank you for your help. I learned something about ProGuard. – xani Jan 02 '17 at 20:21
  • 1
    This is way too loose of a rule set. You might as well turn proguard off entirely. – tir38 Nov 29 '18 at 06:31
2

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

-keep interface android.support.** { *; }
-keep class android.support.** { *; }