Proguard导致崩溃

时间:2017-08-15 07:07:38

标签: android android-proguard

设置

  

minifyEnabled true

     

useProguard true

在我的gradle中,我得到一个像这样的NullPointerException:

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'
    }
}

Gradle是:

title contains

在阅读StackOverflow时,我发现了proguard-rules.pro文件的一些设置,如下所示:

    -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 (...);
    }

当我解除minifyEnabled时,everthing工作正常。 Thancks

2 个答案:

答案 0 :(得分:0)

您可以在Proguard中使用此代码

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

但这会导致Proguard忽略LoginActivity类,并且您的代码不会被混淆。

最好的方法是查看哪个元素已将此创作引入Proguard。

答案 1 :(得分:0)

我还没有看到你的proguard文件,但我很确定添加这个:

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

将解决问题。

一些文档in here

获取您添加的proguard文件,它应该是:

-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 (...);
}