Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange NoClassDefFoundError error when launching a signed release APK with proguard enabled

Some Things I have Tried:

  • Removing the Facebook Dependency makes the app not crash anymore but then it only shows a blank white screen. This is with proguard and multidex enabled.
  • Upgrading gradle to the latest version doesn't solve anything. I upgraded to gradle 2.0.0-beta4 plugin and gradle 2.10.0.
  • Installing a previous version of Android Studio 1.5.1 did not solve anything.
  • I have installed Java JDK 1.8 if this is a clue into anything. I can also post some logcat logs to see what is happening.

I am using MultiDex support in my application with Proguard enabled.

Everything works fine when I build a APK file WITHOUT proguard enabled and install that on my android phone (I have more than 8 phones so a phone is a non-issue).

When I build a release APK WITH proguard enabled it crashes on app startup after i install the app.

I get the following log messages:

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>

And I see the following 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

At a loss of what could be causing this issue, but through process of elimination Proguard seems to cause some issues.

I looked at the Facebook SDK docs, and they mentioned I don't need any specific proguard rules for their Facebook SDK.

Anybody know how to solve this?


EDIT: Adding in the app's 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 Rules:

# 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.** { *; }
like image 699
AndyRoid Avatar asked Feb 12 '16 02:02

AndyRoid


1 Answers

WOW. It was NewRelic and KeepAttributes that was causing issues, adding the following proguard rules solved the issue:

# New Relic
-keep class com.newrelic.** { *; }
-dontwarn com.newrelic.**
-keepattributes Exceptions, Signature, InnerClasses
like image 196
AndyRoid Avatar answered Sep 22 '22 14:09

AndyRoid