Unable to get provider androidx.lifecycle.ProcessLifecycleOwnerInitializr

99封情书 提交于 2020-07-20 07:21:28

问题


Getting error while running app which have dependencies 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03' and 'androidx.lifecycle:lifecycle-viewmodel:2.1.0-alpha03' on device with Android 4.4.

Works fine in device with Android 6

App getting crashed with error

java.lang.RuntimeException: Unable to get provider androidx.lifecycle.ProcessLifecycleOwnerInitializer: java.lang.ClassNotFoundException: Didn't find class "androidx.lifecycle.ProcessLifecycleOwnerInitializer" on path: DexPathList[[zip file "/data/app/**********.apk"],nativeLibraryDirectories=[/data/app-lib/***********, /vendor/lib, /system/lib]]
    at android.app.ActivityThread.installProvider(ActivityThread.java:5052)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:4623)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4563)
    at android.app.ActivityThread.access$1500(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1402)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.lifecycle.ProcessLifecycleOwnerInitializer" on path: DexPathList[[zip file "/data/app/**********.apk"],nativeLibraryDirectories=[/data/app-lib/********, /vendor/lib, /system/lib]]

回答1:


Thanks Andrews Alves

From post

It was a Dex problem. in android 4.4

1 - in app gradle, inside defaultConfig enable multidex

defaultConfig{
  ....
  multiDexEnabled true
}

2 - to support multidex in other versions, add this dependency to your app gradle

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

3 - if you have a class that extends Application, make it extend

MultiDexApplication

if you don't, add this to your manifest file inside application tag

android:name="android.support.multidex.MultiDexApplication" 

Besides, if you created a new application, dont forget to add the following to your app/build.gradle

android {
    defaultConfig {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

that's it. worked for me




回答2:


In my case, Somehow android architecture lifecycle files getting omitted with Android bundle on Pie(Android9). So what I did was added keep statement for the same in proguard-rules.pro

For AndroidX :

-keep class androidx.lifecycle.** {*;}

For Support :

-keep class android.arch.lifecycle.** {*;}



回答3:


You can find the official docs about Multidex here. Use this link to check the latest multidex gradle dependency version.

if you're using AndroidX

dependencies {
    def multidex_version = "2.0.1" // Check this from the link
    implementation 'androidx.multidex:multidex:$multidex_version'
}

otherwise use

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



回答4:


For kotlin add this to your gradle :

kotlinOptions {
        jvmTarget = '1.8'
}
compileOptions {
        targetCompatibility = "8"
        sourceCompatibility = "8"
}


来源:https://stackoverflow.com/questions/55177386/unable-to-get-provider-androidx-lifecycle-processlifecycleownerinitializr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!