Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get provider androidx.startup.InitializationProvider

Tags:

android

I am trying to run the project on Android 9 device and I am getting the following Error saying Unable to get provider androidx.startup.InitializationProvider:.

2020-12-16 15:19:03.075 25234-25234/com.example.phoE/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pho, PID: 25234
    java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: java.lang.ClassNotFoundException: Didn't find class "androidx.startup.InitializationProvider" on path: DexPathList[[zip file "/data/appcom.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/lib/arm64, /data/app/com.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:6857)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6399)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6303)
        at android.app.ActivityThread.access$1300(ActivityThread.java:241)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7188)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.startup.InitializationProvider" on path: DexPathList[[zip file "/data/app/com.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/lib/arm64, /data/app/com.example.pho-Z8JhKzo4aoi5K98ZrK71WQ==/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
        at androidx.core.app.CoreComponentFactory.instantiateProvider(CoreComponentFactory.java:67)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6841)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6399) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6303) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:241) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7188) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

I tried everything mentioned here but nothing is working. Really appreciate if someone can help me.

Gradle file

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'
    }
}

plugins {
    id "com.diffplug.gradle.spotless" version "3.16.0"
}

apply plugin: 'com.android.application'
apply plugin: 'jacoco-android'
apply plugin: 'realm-android'
apply plugin: "com.github.b3er.local.properties"

def keystorePropertiesFile = rootProject.file("sample_keystore.properties")

def keystoreProperties = new Properties()

def LOCAL_KEY_PRESENT = project.hasProperty('SIGNING_KEY_FILE') && rootProject.file(SIGNING_KEY_FILE).exists()

keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "org.fossasia.phimpme"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 14
        versionName '1.11.0'
        multiDexEnabled true
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        vectorDrawables.useSupportLibrary = true
        resConfigs "en"
        ndk {
            abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
        }
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        if (TRAVIS_BUILD) {
            release {
                storeFile KEYSTORE_FILE
                storePassword System.getenv("STORE_PASS")
                keyAlias System.getenv("ALIAS")
                keyPassword System.getenv("KEY_PASS")
            }
        } else if (LOCAL_KEY_PRESENT) {
            release {
                storeFile rootProject.file(SIGNING_KEY_FILE)
                storePassword STORE_PASS
                keyAlias ALIAS
                keyPassword KEY_PASS
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            if (LOCAL_KEY_PRESENT || TRAVIS_BUILD)
                signingConfig signingConfigs.release
        }
        debug {
            testCoverageEnabled false

        }
    }

    packagingOptions {
        exclude 'META-INF/maven/org.apache.maven/maven-artifact/pom.xml'
        exclude 'licenses/javolution.license.TXT'
        exclude 'META-INF/maven/org.apache.maven.wagon/wagon-http-shared/pom.properties'
        exclude 'META-INF/maven/*'
        exclude 'org/codehaus/classworlds/uberjar/boot/Bootstrapper.class'
    }

    configurations {
        implementation.exclude group: 'org.jetbrains', module: 'annotations'
    }

    lintOptions {
        abortOnError false
        disable "Instantiatable"
    }

    tasks.withType(JavaCompile) {
        configure(options) {
            // TODO
            compilerArgs << "-Xlint:unchecked"
            compilerArgs << "-Xlint:-rawtypes"
        }
    }

    dexOptions {
        jumboMode = true
    }
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
}

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

spotless {
    java {
        googleJavaFormat()
        target '**/*.java'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    // Core library
    androidTestImplementation 'androidx.test:core:1.3.0'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'

    //retrofit
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

    //google and support
    implementation "androidx.appcompat:appcompat:$rootProject.supportLibraryVersion"
    implementation "androidx.vectordrawable:vectordrawable-animated:1.1.0"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation "com.google.android.material:material:1.2.1"
    implementation "androidx.vectordrawable:vectordrawable:1.1.0"
    implementation "androidx.palette:palette:1.0.0"
    implementation "androidx.cardview:cardview:1.0.0"
    implementation "androidx.browser:browser:1.2.0"
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'

    //exoplayer
    implementation 'com.google.android.exoplayer:exoplayer:r1.5.7'

    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"


    implementation 'com.koushikdutta.ion:ion:3.0.9'
    implementation 'org.jetbrains:annotations-java5:20.1.0'
    implementation 'com.android.support:multidex:1.0.3'
    implementation group: 'com.box', name: 'box-android-sdk', version: '5.0.0'



}

apply plugin: 'com.android.application'
apply plugin: 'jacoco-android'
like image 981
IamConficker Avatar asked Dec 16 '20 10:12

IamConficker


3 Answers

You can fix it by disabling automatic initialization for all your components and dependencies.

Include this snippet in your manifest:

    <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove" />

I also added a new dependency, although that might not be actually required:

implementation "androidx.startup:startup-runtime:1.0.0"

Read more about this (new?) automatic app initialization stuff here: https://developer.android.com/topic/libraries/app-startup

like image 77
ThePBone Avatar answered Nov 15 '22 16:11

ThePBone


If you have androidx.startup:startup-runtime:1.0.0 in your app, you'll need to add some code to prevent R8 from removing the InitializationProvider and also ensure the InitializationProvider is included in the primary dex file. Hopefully, this issue will be fixed in the next version. Start by declaring the multiDexKeepProguard in your release build type in the app level build.gradle file:

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexKeepProguard = file('multidex-config.pro')
        }

Then, in the top-level multidex-config.pro file, add this:

-keep class androidx.startup.AppInitializer
-keep class * extends androidx.startup.Initializer

You'll also want to add some extra lines to your proguard-rules.pro file:

-keepnames class * extends androidx.startup.Initializer
# These Proguard rules ensures that ComponentInitializers are are neither shrunk nor obfuscated,
# and are a part of the primary dex file. This is because they are discovered and instantiated
# during application startup.
-keep class * extends androidx.startup.Initializer {
    # Keep the public no-argument constructor while allowing other methods to be optimized.
    <init>();
}

From https://issuetracker.google.com/issues/191729671 and https://developer.android.com/studio/build/multidex#multidexkeepproguard-property

like image 38
prfarlow Avatar answered Nov 15 '22 16:11

prfarlow


For those who are using Hilt for DI and facing this issue.

I change from

@HiltAndroidApp
class MyApplication : Application() {
}

to

@HiltAndroidApp
class MyApplication : MultiDexApplication() {
}

And the exception is gone.

like image 33
JS84 Avatar answered Nov 15 '22 16:11

JS84