Android Proguard - ClassNotFoundException

Android Proguard - ClassNotFoundException

我为我的 android 应用启用了混淆器。我可以成功构建项目,但它在启动时因 classNotFoundException 而崩溃。它甚至找不到启动器 activity。这是我的混淆器规则文件

-libraryjars libs

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application

-keepnames class com.mypackage.** { *; } //someone suggested this but doesn't work

# We only want obfuscation
-keepattributes InnerClasses,Signature

# Sdk
-keep public interface com.zendesk.sdk.** { *; }
-keep public class com.zendesk.sdk.** { *; }

# Appcompat and support
-keep interface android.support.v7.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v4.** { *; }
-keep class android.support.v4.** { *; }

# Gson
-keep interface com.google.gson.** { *; }
-keep class com.google.gson.** { *; }

# Retrofit
#-keep class com.google.inject.** { *; }
#-keep class org.apache.http.** { *; }
#-keep class org.apache.james.mime4j.** { *; }
#-keep class javax.inject.** { *; }
#-keep class retrofit.** { *; }
#-keep interface retrofit.** { *; }

# Retrofit
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

-dontwarn rx.**
-dontwarn retrofit.**
-dontwarn okio.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

# Jackson
-keepattributes *Annotation*,EnclosingMethod,Signature
-keepnames class com.fasterxml.jackson.** { *; }
 -dontwarn com.fasterxml.jackson.databind.**
 -keep class org.codehaus.** { *; }
 -keepclassmembers public final enum org.codehaus.jackson.annotate.JsonAutoDetect$Visibility {
 public static final org.codehaus.jackson.annotate.JsonAutoDetect$Visibility *; }
-keep public class your.class.** {
  public void set*(***);
  public *** get*();
}

#Picasso
-dontwarn com.squareup.okhttp.**

#-dontwarn javax.management.**
#-dontwarn java.lang.management.**
#-dontwarn org.apache.log4j.**
#-dontwarn org.apache.commons.logging.**
#-dontwarn org.json.*
#-dontwarn org.apache.commons.codec.binary.Base64

#-keep class javax.** {* ; }
#-keep class org.** { *; }

-dontwarn org.mortbay.**
-dontwarn org.slf4j.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.apache.commons.codec.binary.** 

如果有帮助,这是我的 build.gradle 文件,但它不包含所有依赖项。我在 libs 文件夹中也有一些罐子。

apply plugin: 'com.android.application'
apply plugin: 'robolectric'
apply plugin: 'android'
apply plugin: 'newrelic'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.mypackage"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 54
        versionName "2.002"
        multiDexEnabled true
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        resConfigs "en", "fr" , "ar"
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    repositories{
        maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        compile 'com.android.support:appcompat-v7:21.+'
        compile 'com.github.chrisbanes.photoview:library:1.2.3'
        compile 'com.etsy.android.grid:library:1.0.5'
        compile 'com.facebook.android:facebook-android-sdk:3.21.1'
        compile 'com.google.android.gms:play-services-base:6.5.+'
        compile 'com.google.android.gms:play-services-wallet:6.5.+'
        compile 'com.google.android.gms:play-services-ads:6.5.+'
        compile 'com.google.android.gms:play-services-maps:6.5.+'
        compile 'com.google.android.gms:play-services-drive:6.5.+'
        compile 'com.google.android.gms:play-services-appindexing:6.5.+'
        compile 'com.google.android.gms:play-services-location:6.5.+'
        compile 'com.google.android.gms:play-services-identity:6.5.+'
        compile 'com.google.android.gms:play-services-plus:6.5.+'
        compile 'com.android.support:multidex:1.0.+'
        compile project(':Adjust')
        compile project(':aFileChooser-cbccac1d1cb74a6d57d25c5ded61e4bf4fc40c91')
        compile 'com.parse.bolts:bolts-android:1.+'
        compile 'com.squareup.picasso:picasso:2.4.0'
        compile 'com.android.support:recyclerview-v7:+'
        compile (group: 'com.zendesk', name: 'sdk', version: '1.0.0.1'){
            //    exclude group:'picasso'
        }
        compile 'com.android.support:recyclerview-v7:+'
        compile 'com.newrelic.agent.android:android-agent:4.+'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
        compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.2'
        compile 'com.fasterxml.jackson.core:jackson-core:2.4.2'


        /*compile ('oauth.signpost:signpost-commonshttp4:1.2.1.2') {
            exclude module: 'commons-logging'
            exclude module: 'httpcore'
            exclude module: 'httpclient'
        }
        compile ('oauth.signpost:signpost-core:1.2.1.2') {
            exclude module: 'commons-codec'
        }*/

        androidTestCompile 'org.robolectric:robolectric:2.4'
        androidTestCompile('junit:junit:4.12')
        androidTestCompile('org.mockito:mockito-core:1.10.19')

    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'LICENSE.txt'
    }
}



robolectric {
    include '**/*Test.class'
}

这是完整的堆栈跟踪:

02-17 19:01:09.752: E/AndroidRuntime(2079): FATAL EXCEPTION: main
02-17 19:01:09.752: E/AndroidRuntime(2079): Process: com.mypackage, PID: 2079
02-17 19:01:09.752: E/AndroidRuntime(2079): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/mypackage/activities/MainActivity;
02-17 19:01:09.752: E/AndroidRuntime(2079):     at cmypackage.application.ApplicationContextProvider.onCreate(Unknown Source)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1011)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4518)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.app.ActivityThread.access00(ActivityThread.java:144)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.os.Looper.loop(Looper.java:135)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at java.lang.reflect.Method.invoke(Native Method)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at java.lang.reflect.Method.invoke(Method.java:372)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-17 19:01:09.752: E/AndroidRuntime(2079): Caused by: java.lang.ClassNotFoundException: Didn't find class "mypackage.activities.MainActivity" on path: DexPathList[[zip file "/data/app/mypackage-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
02-17 19:01:09.752: E/AndroidRuntime(2079):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-17 19:01:09.752: E/AndroidRuntime(2079):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-17 19:01:09.752: E/AndroidRuntime(2079):     ... 12 more
02-17 19:01:09.752: E/AndroidRuntime(2079):     Suppressed: java.lang.NoClassDefFoundError: mypackage.activities.MainActivity
02-17 19:01:09.752: E/AndroidRuntime(2079):         at dalvik.system.DexFile.defineClassNative(Native Method)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at dalvik.system.DexFile.defineClass(DexFile.java:226)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at dalvik.system.DexPathList.findClass(DexPathList.java:321)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
02-17 19:01:09.752: E/AndroidRuntime(2079):         ... 14 more
02-17 19:01:09.752: E/AndroidRuntime(2079):     Suppressed: java.lang.ClassNotFoundException: mypackage.MainActivity
02-17 19:01:09.752: E/AndroidRuntime(2079):         at java.lang.Class.classForName(Native Method)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-17 19:01:09.752: E/AndroidRuntime(2079):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-17 19:01:09.752: E/AndroidRuntime(2079):         ... 13 more
02-17 19:01:09.752: E/AndroidRuntime(2079):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

编辑:下面是堆栈跟踪中缺失的部分。

02-17 19:01:09.749: I/MultiDex(2079): VM with version 2.1.0 has multidex support
02-17 19:01:09.749: I/MultiDex(2079): install
02-17 19:01:09.749: I/MultiDex(2079): VM has multidex support, MultiDex support library is disabled.
02-17 19:01:09.750: I/art(2079): Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.FragmentActivity>
02-17 19:01:09.750: I/art(2079): Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.FragmentActivity>
02-17 19:01:09.751: I/art(2079): Rejecting re-init on previously-failed class java.lang.Class<mypackage.activities.MainActivity>
02-17 19:01:09.751: I/art(2079): Rejecting re-init on previously-failed class java.lang.Class<mypackage.activities.MainActivity>
02-17 19:01:09.751: D/AndroidRuntime(2079): Shutting down VM
02-17 19:01:09.751: D/AndroidRuntime(2079): --------- beginning of crash

删除与 activity 相关的混淆规则以及所有与支持相关的规则 libaries.Android studio 默认具有那些 templates.You 可以通过在您的 [=21= 中添加此规则来启用它们]

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }

另请编辑 Jackson 处理器的规则

-keep public class your.class.** {
  public void set*(***);
  public *** get*();
}

在此您的 class.** 是您为解析您的响应而创建的 POJO(getter/setter) class

您还需要确保添加在此处找到的 New Relic proguard 异常:https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-installation/android/installing-android-apps-gradle-android-studio#proguard

-keep class com.newrelic.** { *; }
-dontwarn com.newrelic.**
-keepattributes Exceptions, Signature, InnerClasses

我知道这是一个相当老的 post 但我想强调以下内容,以帮助将来的人。

ClassNotFoundException 是对使用反射访问的 class 应用名称混淆引起的异常。例如,如果 ProGuard 重命名 class MyClass,它在如下代码中访问:Class.forName("MyClass");,如果字符串仍然引用 [=11],将抛出 ClassNotFoundException =].

这是我们需要添加 -keep 选项的地方,以便 ProGuard 不重命名 class;

-keep class com.example.MyClass

当使用广泛的 -keep 选项(以 .** { *; } 结尾)时,您将指示 ProGuard 不要缩小、优化或混淆所有 classes 和 class 成员通配符之前提到的包名称。这最终将导致优化不佳的项目。因此,最好将 -keep 选项缩小到只针对缺失的 class。在 OP 的示例中,抛出以下错误:

Caused by: java.lang.ClassNotFoundException: Didn't find class "mypackage.activities.MainActivity"

为缺失的 class 添加一个 -keep 选项,如下所示将解决这个特定问题;

-keep class mypackage.activities.MainActivity

如果您将 -addconfigurationdebugging 添加到配置文件,ProGuard 可以帮助您设置缩小范围的 -keep 选项,ProGuard 手册 here 中记录了有关此功能的更多详细信息。

最近发布了 ProGuard Playground,您可以快速直观地看到广泛的 -keep 选项与缩小的选项的效果。这里的一个好处是您不需要不断地(重新)构建项目。