Firebase android performance NoClassDefFoundError -> FirebasePerfUrlConnection

Viewed 4379

Does anyone face the same problem? After adding firebase performance to project error occurs immediately after starting the application. It is crashing only in debug build, in release build everything works as expected. Maybe someone has real answer because all I found in similar questions was juggling library versions by trial and error method. Multidex is turned ON.

Error:

E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-2
Process: {PROJECT PACKAGE}, PID: 9872
io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/perf/network/FirebasePerfUrlConnection;
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:69)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)
 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/perf/network/FirebasePerfUrlConnection;
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy.createHttpUrlConnection(WalledGardenInternetObservingStrategy.java:124)
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy.isConnected(WalledGardenInternetObservingStrategy.java:107)
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy$1.apply(WalledGardenInternetObservingStrategy.java:66)
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy$1.apply(WalledGardenInternetObservingStrategy.java:64)
    at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:57)
    at io.reactivex.internal.operators.observable.ObservableInterval$IntervalObserver.run(ObservableInterval.java:82)
    at io.reactivex.Scheduler$PeriodicDirectTask.run(Scheduler.java:532)
    at io.reactivex.Scheduler$Worker$PeriodicTask.run(Scheduler.java:479)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:919) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.perf.network.FirebasePerfUrlConnection" on path: DexPathList[[zip file "/data/app/{PROJECT PACKAGE}--VDzbFrnWyQza3gXDNMSSA==/base.apk"],nativeLibraryDirectories=[/data/app/{PROJECT PACKAGE}--VDzbFrnWyQza3gXDNMSSA==/lib/x86_64, /data/app/{PROJECT PACKAGE}--VDzbFrnWyQza3gXDNMSSA==/base.apk!/lib/x86_64, /system/lib64, /system/product/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy.createHttpUrlConnection(WalledGardenInternetObservingStrategy.java:124) 
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy.isConnected(WalledGardenInternetObservingStrategy.java:107) 
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy$1.apply(WalledGardenInternetObservingStrategy.java:66) 
    at com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy$1.apply(WalledGardenInternetObservingStrategy.java:64) 
    at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:57) 
    at io.reactivex.internal.operators.observable.ObservableInterval$IntervalObserver.run(ObservableInterval.java:82) 
    at io.reactivex.Scheduler$PeriodicDirectTask.run(Scheduler.java:532) 
    at io.reactivex.Scheduler$Worker$PeriodicTask.run(Scheduler.java:479) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:919) 

Buildscript:

buildscript {
    ext {
        commonVariables = [
                applicationVersionName: "{APP VERSION}",
                applicationVersionCode: {APP VERSION CODE},
                compileSdkVersion     : 29,
                minSdkVersion         : 21,
                targetSdkVersion      : 29,
                buildToolsVersion     : "29.0.2"
        ]

        kotlin_version = "1.3.72"
        rx_binding_version = "3.1.0"
        koin_version = "2.1.5"
        moshi_version = "1.9.2"
        lifecycle_version = "2.2.0"
        paging_version = "2.1.2"
        flipper_version = "0.44.0"
        nav_version = "2.2.2"
        room_version = "2.2.5"
        realm_version = "6.1.0"

        commonDependencies = [
            kotlinLib               : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
            appCompat               : "androidx.appcompat:appcompat:1.1.0",
            rxJava                  : "io.reactivex.rxjava2:rxjava:2.2.19",
            rxAndroid               : "io.reactivex.rxjava2:rxandroid:2.1.1",
            rxKotlin                : "io.reactivex.rxjava2:rxkotlin:2.4.0",
            coreKtx                 : "androidx.core:core-ktx:1.2.0",
            activity                : "androidx.activity:activity-ktx:1.1.0",
            fragment                : "androidx.fragment:fragment-ktx:1.2.4",
            recyclerView            : "androidx.recyclerview:recyclerview:1.1.0",
            swipeRefreshLayout      : "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0",
            materialComponents      : "com.google.android.material:material:1.2.0-alpha06",
            constraintLayout        : "androidx.constraintlayout:constraintlayout:1.1.3",
            jodaTime                : 'joda-time:joda-time:2.10.6',
            rxPermissions           : "com.github.tbruyelle:rxpermissions:0.10.2",
            rxBindingsCore          : "com.jakewharton.rxbinding3:rxbinding-core:$rx_binding_version",
            rxBindingsAppCompat     : "com.jakewharton.rxbinding3:rxbinding-appcompat:$rx_binding_version",
            rxBindingsMaterial      : "com.jakewharton.rxbinding3:rxbinding-material:$rx_binding_version",
            rxBindingsRecyclerView  : "com.jakewharton.rxbinding3:rxbinding-recyclerview:$rx_binding_version",
            koin                    : "org.koin:koin-core:$koin_version",
            koinExperimental        : "org.koin:koin-core-ext:$koin_version",
            koinAndroidX            : "org.koin:koin-android:$koin_version",
            koinAndroidXScope       : "org.koin:koin-androidx-scope:$koin_version",
            koinAndroidXViewModel   : "org.koin:koin-androidx-viewmodel:$koin_version",
            koinAndroidXFragment    : "org.koin:koin-androidx-fragment:$koin_version",
            koinAndroidXExperimental: "org.koin:koin-androidx-ext:$koin_version",
            moshi                   : "com.squareup.moshi:moshi-kotlin:$moshi_version",
            moshiKapt               : "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version",
            moshiAdapters           : "com.squareup.moshi:moshi-adapters:$moshi_version",
            lifecycleRuntime        : "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version",
            lifecycleExtensions     : "androidx.lifecycle:lifecycle-extensions:$lifecycle_version",
            pagingCommon            : "androidx.paging:paging-common-ktx:$paging_version",
            pagingRuntime           : "androidx.paging:paging-runtime-ktx:$paging_version",
            pagingRxJava            : "androidx.paging:paging-rxjava2-ktx:$paging_version",
            flipper                 : "com.facebook.flipper:flipper:$flipper_version",
            flipperNetworkPlugin    : "com.facebook.flipper:flipper-network-plugin:$flipper_version",
            soloader                : "com.facebook.soloader:soloader:0.5.1",
            navigationRuntime       : "androidx.navigation:navigation-runtime-ktx:$nav_version",
            navigationUI            : "androidx.navigation:navigation-fragment-ktx:$nav_version",
            navigationFragment      : "androidx.navigation:navigation-ui-ktx:$nav_version",
            room                    : "androidx.room:room-runtime:$room_version",
            roomKtx                 : "androidx.room:room-ktx:$room_version",
            rxRoom                  : "androidx.room:room-rxjava2:$room_version",
            roomAnnotation          : "androidx.room:room-compiler:$room_version"
        ]
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.3.3"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.1.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        classpath "io.realm:realm-gradle-plugin:$realm_version"
        classpath 'com.google.firebase:perf-plugin:1.3.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

App gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'realm-android'
apply plugin: 'com.google.firebase.firebase-perf'

android {
    ...

    defaultConfig {
        ...
        multiDexEnabled true
        ...
    }
}

dependencies {
    ...
    implementation 'com.android.support:multidex:1.0.3'

    releaseImplementation 'com.google.firebase:firebase-analytics:17.4.2'
    releaseImplementation 'com.google.firebase:firebase-crashlytics:17.0.0'
    releaseImplementation 'com.google.firebase:firebase-perf:19.0.7'

    implementation 'com.google.firebase:firebase-messaging:20.2.0'
    implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
    implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.1.1'
    ...
}
4 Answers

Either include all or remove all of these

Build Gradle (AppName)

dependencies {
    classpath 'com.google.firebase:perf-plugin:1.3.4'
}

Build Gradle (:app)

At top of page

apply plugin: 'com.google.firebase.firebase-perf'

Then

dependencies {
    implementation 'com.google.firebase:firebase-perf:19.1.0'
}

Changing

releaseImplementation 'com.google.firebase:firebase-analytics:17.4.2'
releaseImplementation 'com.google.firebase:firebase-crashlytics:17.0.0'
releaseImplementation 'com.google.firebase:firebase-perf:19.0.7'

To

implementation 'com.google.firebase:firebase-analytics:17.4.2'
implementation 'com.google.firebase:firebase-crashlytics:17.0.0'
implementation 'com.google.firebase:firebase-perf:19.0.7'

Should solve the issue as those dependencies are currently defined only for release buildType

The line:

apply plugin: 'com.google.firebase.firebase-perf'

Is adding the dependencies that are causing your app to crash in debug mode. You either need to figure out a way to do that only to your release build (there are some workarounds available), or just include Firebase Performance as part of your debug build.

Newest version of firebase works perfect. Here is sample:

Dependencies:

implementation platform('com.google.firebase:firebase-bom:28.2.1')
releaseImplementation 'com.google.firebase:firebase-analytics-ktx'
releaseImplementation 'com.google.firebase:firebase-crashlytics-ktx'
releaseImplementation 'com.google.firebase:firebase-perf-ktx'
Related