realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

SLF4J and Realm 2.0.1 #3568

Closed mrp14 closed 7 years ago

mrp14 commented 8 years ago

Hello,

I just updated realm from an older version to 2.0.1. From here, I'm unable to have my app working. It seems to be the same bug as #3018 and #3043 :

In debug mode, app builds fine but crashes at launch with this error :

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/gradle/internal/logging/slf4j/OutputEventListenerBackedLoggerContext;
                                                                          at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:30)
                                                                          at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:27)

....

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext" on path: DexPathList[[dex file "/data/data/.../files/instant-run/dex/slice-volley_8dd6661e4587145ffb6c9879c222bc51d500f59b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-twitter-text-1.13.0_73d58164894a99049dcfd4f340b714ec4dfff779-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-support-annotations-24.2.1_c6ee83d5ac5d11c34968a1fdeaa1c92ddb4169e1-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slf4j-api-1.7.12_8403b189700353d8954870a9ab4b928754b6726f-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-retrofit-mock-1.8.0_536419611962dc0f5a7dba16c5de9cbd3ffcc007-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-retrofit-1.8.0_66b3384cf917570f83f165d8fd59e737c65d8d49-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-realm-annotations-2.0.1_d5c759715192ab7f28772555b7c057574cf41796-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-protobuf-java-2.6.1_3d226d37e23b7f0c65ae825cf36e522dfb577471-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-picasso-2.5.2_1b2610b4fc0832caa7649b7ae564642090129b2b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-netty-common-4.1.0.Beta5_067153349f28557a0254ff7129dc51e8ba45753b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-netty-buffer-4.1.0.Beta5_bb5b00239d7f938d53f7b2af8f6fc4e60f2a0f3d-classes.dex", dex file "/data/data/.../
10-06 09:47:48.276 17605-17605/... D/Error: ERR: exClass=java.lang.ClassNotFoundException
10-06 09:47:48.276 17605-17605/... D/Error: ERR: exMsg=Didn't find class "org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext" on path: DexPathList[[dex file "/data/data/.../files/instant-run/dex/slice-volley_8dd6661e4587145ffb6c9879c222bc51d500f59b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-twitter-text-1.13.0_73d58164894a99049dcfd4f340b714ec4dfff779-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-support-annotations-24.2.1_c6ee83d5ac5d11c34968a1fdeaa1c92ddb4169e1-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-slf4j-api-1.7.12_8403b189700353d8954870a9ab4b928754b6726f-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-retrofit-mock-1.8.0_536419611962dc0f5a7dba16c5de9cbd3ffcc007-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-retrofit-1.8.0_66b3384cf917570f83f165d8fd59e737c65d8d49-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-realm-annotations-2.0.1_d5c759715192ab7f28772555b7c057574cf41796-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-protobuf-java-2.6.1_3d226d37e23b7f0c65ae825cf36e522dfb577471-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-picasso-2.5.2_1b2610b4fc0832caa7649b7ae564642090129b2b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-netty-common-4.1.0.Beta5_067153349f28557a0254ff7129dc51e8ba45753b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-netty-buffer-4.1.0.Beta5_bb5b00239d7f938d53f7b2af8f6fc4e60f2a0f3d-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-net.danlew-android.joda-2.9.2_20414ec42e96fe4d2f49d22bba76f3b99e810184-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-me.leolin-ShortcutBadger-1.1.6_d59f8b5a23d0b47e2d707561f40f014d4983c997-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-library-2.4.0_265a2ac7e32367588cae5b6ff904496508213bc0-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-jul-to-slf4j-1.7.12_4ae047c43e83c396b289f783af72b206a1410606-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-jsr305-2.0.1_f098b5342d9eecf48cfd5cc82f41e5bca2267add-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-joda-time-2.9.2-no-tzdb_c56a9daa13ad90750ba344d6f2b040f34c61dc5b-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-javax.servlet-api-3.1.0_8f79d7ad58f4626ab26f0ae35222a1a350c347ea-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-io.underdark-underdark-1.0.10_731a34523f1f6497e067ede2b50c4fd6b2275467-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-io.realm-realm-android-library-2.0.1_6713d04e7feebf2ffade5b92941705ea3ab203d3-classes.dex", dex file "/data/data/.../files/instant-run/dex/slice-io.realm-androi
10-06 09:47:48.276 17605-17605/... D/Error: ERR: file=BaseDexClassLoader.java
10-06 09:47:48.276 17605-17605/... D/Error: ERR: class=dalvik.system.BaseDexClassLoader
10-06 09:47:48.276 17605-17605/... D/Error: ERR: method=findClass line=56
10-06 09:47:48.277 17605-17605/... D/Error: ERR: stack=java.lang.NoClassDefFoundError: Failed resolution of: Lorg/gradle/internal/logging/slf4j/OutputEventListenerBackedLoggerContext;
                                                                 at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:30)
                                                                 at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:27)

In release mode, app does not build :

Error:Gradle: Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
> java.io.IOException: Can't write [...\app\build\intermediates\transforms\proguard\release\jars\3\1f\main.jar] (Can't read [...\app\build\intermediates\transforms\RealmTransformer\release\folders\1\1\realm(;;;;;;**/*.class)] (Duplicate zip entry [org/slf4j/impl/StaticLoggerBinder.class]))

Here is my messy build.gradle :

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

repositories {
    jcenter()
    maven {
        url 'https://maven.fabric.io/public'
    }
    maven {
        url 'https://dl.bintray.com/underdark/android/'
    }
    maven {
        url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "..."
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 5
        versionName "1.1"
        generatedDensities = []
        multiDexEnabled true
    }

    aaptOptions {
        additionalParameters "--no-version-vectors"
    }

    lintOptions {
        abortOnError false
    }

    signingConfigs {
        config {
            storeFile file('...jks')
            storePassword '...'
            keyAlias '...'
            keyPassword '.'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        debug {
            debuggable true
            useProguard false
        }
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
            signingConfig signingConfigs.config
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':linkedin-sdk')

    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'

    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.androidannotations:androidannotations-api:4.0.0'
    apt "org.androidannotations:androidannotations:4.0.0"
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'commons-validator:commons-validator:1.5.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'net.danlew:android.joda:2.9.2'
    compile 'com.wang.avi:library:1.0.5'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'io.underdark:underdark:1.0.10'
    compile 'com.facebook.stetho:stetho:1.4.1'
    compile 'com.uphyca:stetho_realm:2.0.0'
    compile 'io.realm:android-adapters:1.3.0'
    compile 'me.leolin:ShortcutBadger:1.1.6@aar'
    compile 'com.github.halysongoncalves:pugnotification:1.8.1'

    compile 'com.facebook.android:facebook-android-sdk:4.11.0'
    compile('com.twitter.sdk.android:twitter:1.14.1@aar') {
        transitive = true;
    }
    compile 'com.google.android.gms:play-services-auth:9.2.1'
}

configurations {
    all*.exclude group: 'commons-logging', module: 'commons-logging'
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName '...'
    }
}

apply plugin: 'com.google.gms.google-services'

Regards,

Zhuinden commented 8 years ago

What was the older version?

mrp14 commented 8 years ago

It was 1.1.0.
I have a weird behavior : I just tried to downgrade to 1.1.0, and got the same error. It was working fine before, I don't get it.... Any clue about how to find the culprit ?

Zhuinden commented 8 years ago

My first advice would be to disable Instant Run, then do a clean/rebuild.

mrp14 commented 8 years ago

I did it, same results. I also removed realm related dependencies (realm-stetho and realm android adapters). No success for the moment. And I'm still in 1.1.0.

zaki50 commented 8 years ago

What gradle version are you using?

mrp14 commented 8 years ago

Gradle 2.14.1, and Android plugin 2.2.0

zaki50 commented 8 years ago

I suspect that multidex does not put org.gradle.internal.logging.slf4j.* classes into main dex.

Could you edit app/build.gradle like:

android {
  defaultConfig {
    ...
    multiDexEnabled = true
    multiDexKeepProguard file('multi-dex-keep.txt') // ADD THIS LINE
  }
  ...
}

and put app/multi-dex-keep.txt whose content is:

-keep public class org.gradle.internal.logging.slf4j.** {
 *;
}

Does this solve the problem?

kneth commented 8 years ago

Please reopen if you have any updates.

ghost commented 7 years ago

We just encountered this issue as well. We have our own StaticLoggerBinder but it seems like Realm is trying to bring his own (Which I really think should not be the case!). Apparently the one from Realm is used and the project fails to find org.gradle.internal.logging.slf4j, most probably because proguard removed them as unused being that our StaticLoggerBinder is not using them.

I'm trying to find a way to exclude the realm-imported StaticLoggerBinder so that our can work, do you have suggestions?

kneth commented 7 years ago

@lucavitucci And the suggestion about DEX above didn't help you?

vexdev commented 7 years ago

Same error even after applying your fix, Caused by: java.lang.ClassNotFoundException: Didn't find class "org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext" on path: DexPathList[[zip file "/data/app/com.centralway.numbrs.debug-1/base.apk"],nativeLibraryDirectories=[/data/app/com.centralway.numbrs.debug-1/lib/arm, /data/app/com.centralway.numbrs.debug-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]

We verified and removing Realm makes the app run again.

Otherwise, in build/intermediates/classes we can see our StaticLoggerBinder while in build/intermediates/transforms/realmTransformer our class is already magically replaced with the StaticLoggerBinder from gradle.

ghost commented 7 years ago

Stacktrace:

01-30 16:14:43.223 14832-14832/com.vexdev.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.vexdev.myapplication, PID: 14832
                                                                          java.lang.NoClassDefFoundError: Failed resolution of: Lorg/gradle/internal/logging/slf4j/OutputEventListenerBackedLoggerContext;
                                                                              at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:30)
                                                                              at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:27)
                                                                              at org.slf4j.impl.StaticLoggerBinder.getSingleton(StaticLoggerBinder.java:33)
                                                                              at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
                                                                              at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
                                                                              at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
                                                                              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
                                                                              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
                                                                              at com.vexdev.myapplication.Main2Activity.<init>(Main2Activity.java:15)
                                                                              at java.lang.Class.newInstance(Native Method)
                                                                              at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                           Caused by: java.lang.ClassNotFoundException: Didn't find class "org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext" on path: DexPathList[[zip file "/data/app/com.vexdev.myapplication-2/base.apk"],nativeLibraryDirectories=[/data/app/com.vexdev.myapplication-2/lib/arm64, /data/app/com.vexdev.myapplication-2/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
                                                                              at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                              at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                              at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                              at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:30) 
                                                                              at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:27) 
                                                                              at org.slf4j.impl.StaticLoggerBinder.getSingleton(StaticLoggerBinder.java:33) 
                                                                              at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150) 
                                                                              at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124) 
                                                                              at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412) 
                                                                              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357) 
                                                                              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383) 
                                                                              at com.vexdev.myapplication.Main2Activity.<init>(Main2Activity.java:15) 
                                                                              at java.lang.Class.newInstance(Native Method) 
                                                                              at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Sample project:

https://github.com/lucavitucci/RealmSlf4j

zaki50 commented 7 years ago

I reproduced this bug with your sample project. It seems that there are some bugs in our transformer.

zaki50 commented 7 years ago

Fixed by #4127

zaki50 commented 7 years ago

@lucavitucci Thanks. Your sample project helps me a lot! Snapshot build will be deployed shortly.

Could you try it when you have a time? https://github.com/realm/realm-java/blob/master/README.md#using-snapshots (I already confirmed that your sample project worked as expected with my local build of Realm though.

ghost commented 7 years ago

@zaki50 Thank you for the fastest reaction time I've ever encounter after reporting a bug 👍 I just verified on our main project and can confirm that 2.4.0-SNAPSHOT is now working.

zaki50 commented 7 years ago

@lucavitucci @mrp14 Thank you for your confirmation.I'll close this.

mlostekk commented 6 years ago

This seems to be happening again with realm

    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.10"
    classpath "io.realm:realm-gradle-plugin:4.2.0"
Zhuinden commented 6 years ago

@cmelchior @mlostekk can you provide the complete list of dependencies?

I've tried Kotlin things before and this didn't happen?

mlostekk commented 6 years ago

Hey @Zhuinden please see the issue https://github.com/realm/realm-java/issues/5641

When rolling back to the latest major version 3, then everything runs fine. Maybe the issue got introduced again like it already happened twice?