簡體   English   中英

E/LoadedApk:無法僅在 Android Q (API 29) 上實例化 appComponentFactory

[英]E/LoadedApk: Unable to instantiate appComponentFactory only on Android Q (API 29)

我已經到處搜索了這個問題,但仍然沒有解決方案。

gradle 有 minSdkVersion 21 和 targetSdkVersion 29

我只在 API 29 中遇到錯誤,然后應用程序無法加載並顯示空白屏幕。 當我看到 logcat 時,它給出了這樣的錯誤:

E/LoadedApk: Unable to instantiate appComponentFactory
java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/com.packagename.appname-BxS7Zs-h0IWwJAVhbjx7aQ==/lib/x86, /data/app/com.packagename.appname-BxS7Zs-h0IWwJAVhbjx7aQ==/base.apk!/lib/x86, /system/lib, /system/product/lib]]
    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 android.app.LoadedApk.createAppFactory(LoadedApk.java:256)
    at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:370)
    at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5951)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1941)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at com.android.server.SystemServer.run(SystemServer.java:541)
    at com.android.server.SystemServer.main(SystemServer.java:349)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:908)

我認為它似乎來自 multidex,然后我嘗試將以下代碼添加到 gradle app

multiDexKeepFile 文件('multidex-config.txt')

multiDexKeepProguard 文件('multidex-config.pro')

我的 multidex-config.txt

com/packagename/appname/androidx.class

我的 multidex-config.pro

-keep class androidx.core.app.CoreComponentFactory { *; }

我的 App 類擴展 MultiDexApplication

public class App extends MultiDexApplication {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

我的清單

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/ic_logo"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_logo"
    android:supportsRtl="true"
    android:theme="@style/main"
    android:usesCleartextTraffic="true"
    tools:ignore="GoogleAppIndexingWarning">

我的 gradle.properties

android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m

我的畢業/應用

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.packagename.appname"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 7
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexKeepFile file('multidex-config.txt')
            multiDexKeepProguard file('multidex-config.pro')
            minifyEnabled true
        }
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexKeepFile file('multidex-config.txt')
            multiDexKeepProguard file('multidex-config.pro')
            minifyEnabled true
        }
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.libraries.places:places:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'

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

    implementation 'com.github.vipulasri:timelineview:1.1.0'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.yanzhenjie.zbar:camera:1.0.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'

    implementation 'com.amitshekhar.android:android-networking:1.0.2'

    implementation 'com.google.firebase:firebase-core:17.2.2'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

    implementation 'com.github.kenglxn.QRGen:android:2.5.0'

    implementation 'com.facebook.android:facebook-android-sdk:4.36.0'

    implementation 'com.jsibbold:zoomage:1.2.0'

    implementation 'androidx.arch.core:core-common:2.1.0'
    implementation 'androidx.arch.core:core-runtime:2.1.0'
}

我真的需要一個解決方案。 請幫幫我,非常感謝!

該錯誤指向使用androidx CoreComponentFactory類(使用 AndroidX),但您使用的是舊包com.android.support:multidex

所以使用androidx依賴為:

implementation 'androidx.multidex:multidex:2.0.1'

代替

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

並確保在應用程序類中使用androidx導入作為

import android.content.Context;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

public class YourApp extends MultiDexApplication {

    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }
}

另外:在應用加載期間,無論您是否在清單中使用,android Q 都會在內部使用CoreComponentFactory實例,這會導致問題。

另外,添加multiDexEnabled true作為defaultConfig{multiDexEnabled true }


更新:在 RCA 之后,通過androidx lib 導入解決了componentfactory導入問題,盡管版本4.36.0的 facebook sdk 依賴項存在另一個問題,該問題已通過將版本降級到4.35.0 (或使用最新版本集成)解決並且也張貼在這里

您可以嘗試添加 Java 1.8 兼容性

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}

將該代碼添加到您的android { }中的 app/build.gradle

感謝 Pavneet_Singh 回答:

我有兩條線

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

我刪除了附加行:

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

這個錯誤消失了。

首先,我認為您對MultiDex.install(this)進行了兩次調用,因為子類化MultiDexApplication類會為您調用 this,然后您也顯式調用它。 所以這可能是我猜的問題的一部分。

但是,我遇到了類似的問題,上述答案均未解決。

我們的應用程序已經存在了一段時間(在 API 21 / Android 5 之前),並且有一個 Multidex 的顯式實現。

class MyApplication : SomeOtherApplication() {

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }
}

刪除 mutliDex 實現和 gradle 依賴項,

dependencies {
  implementation "androidx.multidex:multidex:2.0.1"
}

然后重建應用程序立即解決了問題。

文檔指出,從 minSdkVersion 21 multiDex 默認啟用,因此不再需要像問題或我們的實現中的MultiDexApplication類的子類化。

因此,看起來您也可以刪除與 multiDex 相關的實現,也許也可以解決問題。

作為記錄,雖然它在這里似乎並不相關,但也可以刪除一些與 multiDex 相關的其他 gradle 配置,因為它們默認啟用。 看到這個答案

我不確定為什么會發生此錯誤。 但是我遇到了類似的情況,我的解決方案是刪除清單中的兩行。

<application
...
tools:replace="android:appComponentFactory"
android:appComponentFactory="androidx"
>

我不知道這是否是最聰明的解決方案,但它對我有用。

更新“build.gradle”中的“buildToolsVersion”,然后進行“重建”解決了我的問題

android { ... ... ... buildToolsVersion "29.0.3" ... ... ... }

盡量不要手動配置 multidex

應用類

//Remove unused imports
public class App extends Application { //change MultiDexApplication 
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //MultiDex.install(this); //comment this
    }
}

等級:應用程序

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.packagename.appname"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 7
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //multiDexKeepFile file('multidex-config.txt') //comment this
            //multiDexKeepProguard file('multidex-config.pro') //comment this
            minifyEnabled true
        }
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //multiDexKeepFile file('multidex-config.txt') //comment this
            //multiDexKeepProguard file('multidex-config.pro') //comment this
            minifyEnabled true
        }
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.libraries.places:places:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'

    //implementation 'com.android.support:multidex:2.0.1' //comment this

    implementation 'com.github.vipulasri:timelineview:1.1.0'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.yanzhenjie.zbar:camera:1.0.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'

    implementation 'com.amitshekhar.android:android-networking:1.0.2'

    implementation 'com.google.firebase:firebase-core:17.2.2'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

    implementation 'com.github.kenglxn.QRGen:android:2.5.0'

    implementation 'com.facebook.android:facebook-android-sdk:4.36.0'

    implementation 'com.jsibbold:zoomage:1.2.0'

    implementation 'androidx.arch.core:core-common:2.1.0'
    implementation 'androidx.arch.core:core-runtime:2.1.0'
}

我遇到的情況是我的應用程序使用了系統簽名,這個問題在Android Q中也遇到過,我怎么改都改不了。 稍后更改簽名即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM