22

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'
Zoe
  • 27,060
  • 21
  • 118
  • 148
udi
  • 3,672
  • 2
  • 12
  • 33
  • 1
    see this answer https://stackoverflow.com/questions/50787812/my-app-crashes-on-startupjava-lang-runtimeexception-unable-to-get-provider – Suhaib Raadan Dec 16 '20 at 10:26
  • @SuhaibRaadan thank u for the comment. But it does not work for me. I tryed it – udi Dec 16 '20 at 10:34
  • 2
    make sure you don't refer android.support.* from your manifest file. Use the corresponding androidX updated path for file proivder reference -> androidx.core.content.FileProvider – Suhaib Raadan Dec 16 '20 at 10:38
  • 3
    Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/q/354427/6296561), and [the tagging guide](/help/tagging). Use [android] or other relevant tags instead. – Zoe Dec 26 '20 at 14:00
  • 1
    Check out this issue tracker issue https://issuetracker.google.com/issues/191729671 – prfarlow Jul 08 '21 at 14:49

6 Answers6

24

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

ThePBone
  • 508
  • 6
  • 13
  • 1
    If you do this, then you must manually initialize any components that use the App Startup library. This might come from 3rd party libraries. You can find them by checking your merged manifest for any added meta-data tags to the InitializationProvider entry _before_ adding `tools:node="remove"` – prfarlow Jul 08 '21 at 17:05
  • 2
    I did this and it fixed my issue am using Flutter, please can you tell the disadvantage of this?? – Zionnite Nov 11 '21 at 23:17
  • I did this and it fixed my issue am using Flutter, please can you tell the disadvantage of this?? – Zionnite Nov 11 '21 at 23:17
  • 1
    I added this and got a different error: https://github.com/googleads/googleads-mobile-flutter/issues/450 – Dani Nov 28 '21 at 17:21
  • 1
    Why do we have to disable automatic initialization ?? It's should not the right way to fix this – Stevie Dec 08 '22 at 04:05
  • @ThePBone If you disabled the startup then why did u have added the implementation of it ,this is not a way to fix this issue. – pradeep Feb 22 '23 at 18:15
10

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

prfarlow
  • 3,624
  • 2
  • 15
  • 24
5

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.

JS84
  • 221
  • 3
  • 4
  • can't import MultiDexApplication. In which package it is? – Oleh Liskovych Jul 19 '23 at 08:48
  • @OlehLiskovych to extend the base class directly, you must apply the hilt gradle plugin, please check your gradle build config? https://dagger.dev/hilt/gradle-setup.html#hilt-gradle-plugin – JS84 Jul 20 '23 at 09:37
2

Cleaning and rebuilding the project worked for me.

  1. Build -> Clean Project
  2. Build -> Make Project
Samuel
  • 356
  • 2
  • 13
1

update your startup runtime lib version to 1.1.0. prfarlow's answer is included in this pull request

BekaBot
  • 480
  • 1
  • 7
  • 19
1

You may also want to make sure the specified value for the attribute android:name in the <meta-data> tag under the <provider> tag in AndroidManifest.xml is correct.

Anne
  • 259
  • 3
  • 4