启动带有proguard的签名版本APK时出现奇怪的NoClassDefFoundError错误

时间:2016-02-12 02:42:15

标签: android facebook proguard noclassdeffounderror

我尝试过的一些事情:

  • 删除Facebook Dependency使应用程序不再崩溃,但之后它只显示一个空白屏幕。这是通过proguard和multidex启用的。
  • 将gradle升级到最新版本无法解决任何问题。我升级到gradle 2.0.0-beta4插件并使用gradle 2.10.0。
  • 安装以前版本的Android Studio 1.5.1并没有解决任何问题。
  • 我已经安装了Java JDK 1.8,如果这是任何事情的线索。我还可以发布一些logcat日志来查看发生了什么。

我在启用了MultiDex的应用程序中使用Proguard支持。

当我建立一个APK文件没有 proguard时,一切正常,并在我的Android手机上安装(我有超过8部手机,所以手机不是问题)。

当我构建一个版本APK WITH proguard启用后,它会在安装应用程序后在应用启动时崩溃。

我收到以下日志消息:

I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.n>
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.n>
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.o>
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.o>
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.r>
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.app.r>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.facebook.FacebookActivity>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.facebook.FacebookActivity>

我看到以下RuntimeException

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.mycustomapp.app, PID: 6814
              java.lang.NoClassDefFoundError: Failed resolution of: Lcom/facebook/FacebookActivity;
                  at com.facebook.b.aa.c(Validate.java:175)
                  at com.facebook.p.M(FacebookSdk.java:160)
                  at com.mycustomapp.app.utils.a.b.a.<init>(CustomFacebookManagerUtility.java:68)
                  at com.mycustomapp.app.utils.a.b.a.ct(CustomFacebookManagerUtility.java:54)
                  at com.mycustomapp.app.utils.application.MyCustomApplication.onCreate(MyCustomApplication.java:84)
                  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1021)
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5990)
                  at android.app.ActivityThread.access$1700(ActivityThread.java:205)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1756)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:6895)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
               Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.FacebookActivity" on path: DexPathList[[zip file "/data/app/com.mycustomapp.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycustomapp.app-1/lib/arm, /vendor/lib, /system/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                  at com.facebook.b.aa.c(Validate.java:175) 
                  at com.facebook.p.M(FacebookSdk.java:160) 
                  at com.mycustomapp.app.utils.a.b.a.<init>(CustomFacebookManagerUtility.java:68) 
                  at com.mycustomapp.app.utils.a.b.a.ct(CustomFacebookManagerUtility.java:54) 
                  at com.mycustomapp.app.utils.application.MyCustomApplication.onCreate(MyCustomApplication.java:84) 
                  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1021) 
                  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5990) 
                  at android.app.ActivityThread.access$1700(ActivityThread.java:205) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1756) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:6895) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                Suppressed: java.lang.NoClassDefFoundError: com.facebook.FacebookActivity
                  at dalvik.system.DexFile.defineClassNative(Native Method)
                  at dalvik.system.DexFile.defineClass(DexFile.java:226)
                  at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
                  at dalvik.system.DexPathList.findClass(DexPathList.java:321)
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
                        ... 18 more
                Suppressed: java.lang.ClassNotFoundException: com.facebook.FacebookActivity
                  at java.lang.Class.classForName(Native Method)
                  at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                  at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                        ... 17 more
               Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

不知道可能导致此问题的原因,但通过消除过程Proguard似乎会导致一些问题。

我查看了Facebook SDK文档,他们提到我的Facebook SDK不需要任何特定的proguard规则。

有谁知道如何解决这个问题?

编辑:添加应用的build.gradle

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath "com.newrelic.agent.android:agent-gradle-plugin:5.4.0"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'newrelic'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude '.readme'
        exclude 'LICENSE.txt'
        exclude 'README.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/MANIFEST.MF'
    }

    defaultConfig {
        applicationId "com.mycustomapp.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 3
        versionName "1.2"

        /**
         * Enabling multidex support.
         */
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/gcm.jar')

/**
 * For MultiDex Support
 */
compile 'com.android.support:multidex:1.0.1'

/**
 * android support/compatibility libraries.
 */
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:palette-v7:23.1.1'
compile 'com.google.android.gms:play-services-gcm:8.3.0'

/**
 * facebook sdk
 */
compile 'com.facebook.android:facebook-android-sdk:4.1.0'

compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okio:okio:1.1.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.jakewharton:disklrucache:2.0.2'
compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
    transitive = true;
}
compile 'com.amplitude:android-sdk:2.2.0'
compile 'com.appboy:android-sdk-ui:1.11.+'
compile 'joda-time:joda-time:2.3'
compile 'com.facebook.conceal:conceal:1.0.1@aar'
compile 'com.newrelic.agent.android:android-agent:5.4.0'

}

Proguard规则:

# Obfuscation parameters:
#-dontobfuscate
-useuniqueclassmembernames
-keepattributes SourceFile,LineNumberTable
-allowaccessmodification

# Ignore warnings:
#-dontwarn org.mockito.**
#-dontwarn org.junit.**
#-dontwarn com.robotium.**

# Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
    **[] $VALUES;
    public *;
}

# Crashlytics
-keep class com.crashlytics.** { *; }
-keep class com.crashlytics.android.**

# Ignore warnings: We are not using DOM model
-dontwarn com.fasterxml.jackson.databind.ext.DOMSerializer
# Ignore warnings: https://github.com/square/okhttp/wiki/FAQs
-dontwarn com.squareup.okhttp.internal.huc.**
# Ignore warnings: https://github.com/square/okio/issues/60
-dontwarn okio.**
# Ignore warnings: https://github.com/square/retrofit/issues/435
-dontwarn com.google.appengine.api.urlfetch.**

# Keep GSON stuff
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.** { *; }

# Keep Jackson stuff
-keep class org.codehaus.** { *; }
-keep class com.fasterxml.jackson.annotation.** { *; }

# Butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

# Keep these for GSON and Jackson
-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod

# Retrofit 1.X
-keep class com.squareup.okhttp.** { *; }
-keep class retrofit.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn retrofit.**
-dontwarn rx.**

-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

# If in your rest service interface you use methods with Callback argument.
-keepattributes Exceptions

# If your rest service methods throw custom exceptions, because you've defined an ErrorHandler.
-keepattributes Signature

# Joda Time
-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }

# Keep Picasso
-keep class com.squareup.picasso.** { *; }
-keepclasseswithmembers class * {
    @com.squareup.picasso.** *;
}
-keepclassmembers class * {
    @com.squareup.picasso.** *;
}

# RenderScript
-keep class android.support.v8.renderscript.** { *; }

# DBFlow
-keep class com.raizlabs.android.dbflow.config.GeneratedDatabaseHolder

# AppBoy
-dontwarn com.amazon.device.messaging.**
-dontwarn bo.app.**
-dontwarn com.appboy.ui.**
-dontwarn com.google.android.gms.**
-keep class bo.app.** { *; }
-keep class com.appboy.** { *; }

# Android Volley
# see: http://stackoverflow.com/questions/21816643/volley-seems-not-working-after-proguard-obfuscate
-keep class org.apache.commons.logging.** { *; }

2 个答案:

答案 0 :(得分:8)

WOW。导致问题的是NewRelic和KeepAttributes,添加以下proguard规则解决了这个问题:

if(x)

答案 1 :(得分:0)

也许这不是最好的答案,但我希望它会对你有所帮助。至于facebook关注启用proguard时,是的,在文档中没有facebook sdk的规则。 注意:我使用的是skd版本4.7.0 'com.facebook.android:facebook-android-sdk:4.7.0'。我已经在我的项目上实现了这个并且没有任何问题。请在您的项目上试试这个:

  1. 使用 buildToolsVersion&#34; 23&#34; 或最新版 23.0.2
  2. 使用以下buildToolsVersion useLibrary&#39; org.apache.http.legacy&#39;
  3. 尝试更改依赖项classpath 'com.android.tools.build:gradle:1.3.1'中的根gradle 下面我向您展示我的proguard规则和我的build.gradle。
  4. 希望这可以解决您的问题。

    这是我的规则:

    -keep class butterknife.** { *; }
    -dontwarn butterknife.internal.**
    -keep class **$$ViewBinder { *; }
    
    -keepclasseswithmembernames class * {
        @butterknife.* <fields>;
    }
    
    -keepclasseswithmembernames class * {
        @butterknife.* <methods>;
    }
    
    -dontwarn com.squareup.okhttp.**
    
    #
    # Twitter library - start
    #
    
    -dontwarn twitter4j.**
    -keep class twitter4j.** { *; }
    
    #
    # Twitter library - end
    #
    
    # adcolony start
    -dontwarn com.immersion.**
    -dontnote com.immersion.**
    -dontwarn android.webkit.**
    #adcolony end
    
    #app billing
    -keep class com.android.vending.billing.**
    #end app billing
    

    这是我的build.gradle:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23"
        useLibrary 'org.apache.http.legacy'
    
        defaultConfig {
            applicationId "antiboring.game"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 11
            versionName "1.9.0"
    
    
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                        'proguard-rules.pro'
            }
        }
    
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/services/javax.annotation.processing.Processor'
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    
        sourceSets {
            androidTest {
                java.srcDirs = ['androidTest/java']
            }
            main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] }
        }
    
        lintOptions {
            disable 'InvalidPackage'
        }
    }
    
    
    
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        // App dependencies
        compile 'com.android.support:support-annotations:23.1.1'
        compile 'com.google.guava:guava:18.0'
        //unit testing
        androidTestCompile 'com.android.support:support-annotations:23.1.1'
        androidTestCompile 'com.android.support.test:runner:0.4.1'
        androidTestCompile 'com.android.support.test:rules:0.4.1'
    
        //apps library
        compile files('libs/twitter4j-core-4.0.2.jar')
        compile fileTree(dir: 'libs', include: ['adcolony.jar'])
        compile 'com.android.support:appcompat-v7:23.1.1'
        compile 'com.android.support:recyclerview-v7:23.1.1'
        compile 'com.android.support:palette-v7:23.1.1'
        compile 'com.android.support:design:23.1.1'
        compile 'com.balysv.materialmenu:material-menu-toolbar:1.5.4'
        compile 'com.google.android.gms:play-services-ads:8.4.0'
        compile 'com.jakewharton:butterknife:7.0.1'
        compile 'com.squareup.picasso:picasso:2.5.2'
    
        //social media
        compile 'com.facebook.android:facebook-android-sdk:4.7.0'
    
        //animation
        compile 'com.nineoldandroids:library:2.4.0'
        compile 'com.daimajia.easing:library:1.0.1@aar'
        compile 'com.daimajia.androidanimations:library:1.1.3@aar'
    
    }