繁体   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