如何摆脱 App 在启动时崩溃?

Posted

技术标签:

【中文标题】如何摆脱 App 在启动时崩溃?【英文标题】:How get rid from App getting crashed on launch? 【发布时间】:2022-01-17 19:53:29 【问题描述】:

启动时应用崩溃, 将 Gradle 插件 4.x.x 更新到 7.0.4 并将 Dagger2 迁移到 Hilt。 并将 Kotlin 更新到 1.6.0。 在 Application 类上添加了 @@HiltandroidApp。 应用 Multidex 但不起作用。 尝试其他一些解决方案,但没有帮助。

请查看日志。

tech.xyz.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: tech.xyz.android, PID: 14295
    java.lang.RuntimeException: Unable to instantiate application tech.xyz.android.base.BaseApplication package tech.xyz.android: java.lang.ClassNotFoundException: Didn't find class "tech.xyz.android.base.BaseApplication" on path: DexPathList[[zip file "/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk"],nativeLibraryDirectories=[/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/lib/arm64, /data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64, /product/lib64]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1364)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6689)
        at android.app.ActivityThread.access$1500(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2057)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "tech.xyz.android.base.BaseApplication" on path: DexPathList[[zip file "/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk"],nativeLibraryDirectories=[/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/lib/arm64, /data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
        at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1177)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1356)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6689) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:247) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2057) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7842) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Ltech/xyz/android/base/Hilt_BaseApplication;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
                ... 15 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "tech.xyz.android.base.Hilt_BaseApplication" on path: DexPathList[[zip file "/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk"],nativeLibraryDirectories=[/data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/lib/arm64, /data/app/~~GzEkuyUrT68SI_tbzjNEGg==/tech.xyz.android-puNWBMfhZZkc5IKFz18ZHA==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 18 more

更新 gradle 后,kotlin 并将 hilt 添加到根级 Gradle 中:

buildscript 
    ext
        kotlin_version = "1.6.0"
        apollo_version = '2.5.10'
    

    repositories 
        google()
        jcenter()
        mavenCentral()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
        classpath "com.apollographql.apollo:apollo-gradle-plugin:$apollo_version"
    


allprojects 

    repositories 
        google()
        jcenter()
        maven  url 'https://jitpack.io' 
    



task clean(type: Delete) 
    delete rootProject.buildDir

这是应用程序类:

@HiltAndroidApp
class BaseApplication : Application(), LifecycleObserver 
    private val TAG = "BaseApplication"
    companion object 
        lateinit var instance: BaseApplication
            private set
    

    override fun attachBaseContext(base: Context?) 
//        MultiDex.install(base)
        super.attachBaseContext(base)
    
    override fun onCreate() 
        ActivityLifecycleCallback.register(this)
        super.onCreate()
        instance = this
        userAgent = Util.getUserAgent(this, "AdaptiveExoplayer")
        FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)

        ProcessLifecycleOwner.get()
            .lifecycle
            .addObserver(this)
    

这是AndroidManifest.xml<application>标签

<application
    android:name=".base.BaseApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:requestLegacyExternalStorage="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:networkSecurityConfig="@xml/network_config"
    android:largeHeap="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

大多数解决方案都说添加 Multidex 或清理并重建项目或使缓存无效并重新启动。尝试了大多数解决方案,但没有帮助。

【问题讨论】:

添加你的 BaseApplication 类,这里没有足够的信息。 @Booger 请看我已经更新了帖子。 TIA。 仍然没有足够的信息,因为我们看不到 Application 类的包...我会将完整的包添加到清单中。 【参考方案1】:

问题出在我的模块级 gradle 文件中。我使用的是annotationProcessor 而不是kapt

annotationProcessor 'com.google.dagger:hilt-compiler:2.40.5'

然后改成

kapt 'com.google.dagger:hilt-compiler:2.40.5'

【讨论】:

【参考方案2】:

尝试将清单更改为:

android:name=".base.BaseApplication"

到:

android:name="tech.xyz.android.base.BaseApplication"

【讨论】:

试过但没用。 此问题已解决。将annotationProcessor 替换为kapt

以上是关于如何摆脱 App 在启动时崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

react-native app在启动屏幕上崩溃

应用程序在启动时崩溃

UnsatisfiedLinkError,Cocos2D-x app在启动时崩溃

Apple App Store 中的“应用程序在启动时崩溃”,但它在 Debug/Adhock 中运行良好?

作为系统应用程序时,应用程序在启动时崩溃

calabash-ios物理设备测试,app启动但崩溃