NullPointerException with proguard

2019-09-05 17:20发布

问题:

I'm using Proguard with my apps and I'm getting NullPointerExceptions sent to my Developer Console for random users. When I do a ReTrace, it seems to be an issue with the AdView I'm using. The confusing thing is, the error is happening in my Service which, obviously, doesn't use ads. Not sure if Proguard is doing something to the code or what. This is the de-obfuscated stack trace:

java.lang.NullPointerException
at com.google.ads.internal.AdWebView$1.a(Unknown Source)
at com.app.base.MainService.onHandleIntent(Unknown Source)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)

MainService is my service, which is then showing, from what I can tell, a NullPointerException related to com.google.ads.internal.AdView.

I recently realized I wasn't calling destroy() on my AdView, so I added this to the Activities that are using it:

 @Override
 public void onDestroy() 
 {
    if (adView != null)
      adView.destroy();

    super.onDestroy();
  }

Not sure if that would cause the issue.

This is my proguard.cfg file:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-libraryjars C:\Workspace\JARs\GoogleAdMobAdsSdk-6.0.0.jar
-libraryjars C:\Workspace\JARs\android-support-v4.jar

-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-dontwarn **CompatHoneycomb
-dontwarn **CompatHoneycombMR2
-dontwarn **CompatCreatorHoneycombMR2

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.Fragment

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keep class com.app.base.KeywordsFragment
-keep class com.app.base.ListingFragment

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

# This will avoid all the onClick listeners referenced from XML Layouts from being removed
-keepclassmembers class * extends android.app.Activity { 
       public void *(android.view.View); 
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-dontwarn android.support.**

UPDATE:

After looking at the Proguard manual, I found this is in the troubleshooting section:

"MissingResourceException or NullPointerException Your processed code may be unable to find some resource files. ProGuard simply copies resource files over from the input jars to the output jars."

Wonder if I need to use keepdirectories

UPDATE 2:

I'm wondering if adding this will fix my issue. I can't test this though because I am unable to recreate the error myself. It is only happening for random users:

 -keep class com.google.ads.** {*;}

UPDATE 3:

After retracing other errors I'm getting in the Developer Console, they all seem to be related to com.google.ads, eg:

 java.lang.NullPointerException
 at com.google.ads.InterstitialAd.a(Unknown Source)

回答1:

I think using the following should work:

-keepattributes *Annotation*
-keep public class com.google.ads.**

The annotation piece may be in there already. I think the issue is that the SDK has it's own proguard, and relies on it's public classes to not be proguarded by the developer.



回答2:

The second proguard statement should be:

-keep public class com.google.ads.** {*;}

without {*;} it doesn't work for me



回答3:

I spent an evening figuring out that introspection can cause a NullPointerException with proguard. In retrospect it is obvious.

For example, I had to remove the line:

Log.i(TAG, new Object(){}.getClass().getEnclosingMethod().getName());