5
votes

When setting

minifyEnabled true

useProguard true

in my gradle I get a NullPointerException like this :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jafari.farhad.ubazdidcontroller/com.jafari.farhad.ubazdidcontroller.LoginActivity}: java.lang.NullPointerException
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  at android.app.ActivityThread.access$600(ActivityThread.java:141)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5041)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
  at com.jafari.farhad.ubazdidcontroller.LoginActivity.onCreate(Unknown Source)
  at android.app.Activity.performCreate(Activity.java:5104)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:5041) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
  at dalvik.system.NativeStart.main(Native Method) 

Gradle is :

buildTypes {
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

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

    -dontusemixedcaseclassnames
    -dontskipnonpubliclibraryclasses
    -verbose
    
    
    -dontoptimize
    -dontpreverify
    
    -keepattributes *Annotation*
    -keep public class com.google.vending.licensing.ILicensingService
    -keep public class com.android.vending.licensing.ILicensingService
    
    # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
    -keepclasseswithmembernames class * {
        native ;
    }
    
    # keep setters in Views so that animations can still work.
    # see http://proguard.sourceforge.net/manual/examples.html#beans
    -keepclassmembers public class * extends android.view.View {
       void set*(***);
       *** get*();
    }
    
    # We want to keep methods in Activity that could be used in the XML attribute onClick
    -keepclassmembers class * extends android.app.Activity {
       public void *(android.view.View);
    }
    
    # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
    -keepclassmembers enum * {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }
    
    -keepclassmembers class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator CREATOR;
    }
    
    -keepclassmembers class **.R$* {
        public static ;
    }
    
    -dontwarn android.support.**
    
    # Understand the @Keep support annotation.
    -keep class android.support.annotation.Keep
    
    -keep @android.support.annotation.Keep class * {*;}
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep ;
    }
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep ;
    }
    
    -keepclasseswithmembers class * {
        @android.support.annotation.Keep (...);
    }

When I disbale minifyEnabled everthing works fine. Thancks

2
and what is in your proguard file?Matias Olocco
yes, send it ? @MatiasOloccohanderianous
show proguard file and full gradle.build fileNguyễn Trung Hiếu
@handerianous I'm asking you to include your proguard file, otherwise we cannot know what is wrong with it.Matias Olocco
remove useProgaurd true from bauild.gradle file its unnecessary, and post complete build.gradle file.nivesh shastri

2 Answers

0
votes

you can use this code in Proguard

-keep class com.jafari.farhad.ubazdidcontroller.LoginActivity** 

But this will cause the Proguard to ignore the LoginActivity class, and your code will not be obfuscation.

The best way is to see which element has caused this creation to be introduced to the Proguard.

0
votes

I haven't seen your proguard file yet, but I'm quite sure that adding this:

-keep class com.jafari.farhad.ubazdidcontroller.** { *; }

Will solve the problem.

Some docs in here.

Taking the proguard file that you added, it should be:

-keep class com.jafari.farhad.ubazdidcontroller.** { *; }
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose


-dontoptimize
-dontpreverify

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native ;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

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

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

-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
@android.support.annotation.Keep ;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep ;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep (...);
}