Android:无法实例化应用程序

Android : Unable to instantiate application

提问人:heet kanabar 提问时间:10/30/2023 最后编辑:ΓDΛheet kanabar 更新时间:11/2/2023 访问量:81

问:

我有一个应用程序,我在其中实现了匕首,午餐时我的应用程序出现此错误:-

FATAL EXCEPTION: main
Process: com.heet.sleepmagic, PID: 18977
java.lang.RuntimeException: Unable to instantiate application com.heet.sleepmagic.MainApp package com.heet.sleepmagic: java.lang.ClassNotFoundException: Didn't find class "com.heet.sleepmagic.MainApp" on path: DexPathList[[zip file "/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/lib/x86_64, /system/lib64, /system_ext/lib64]]
at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1466)
at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1395)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6720)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2133)
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:7872)
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:936)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.heet.sleepmagic.MainApp" on path: DexPathList[[zip file "/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/lib/x86_64, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
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:1243)
at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1458)
at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1395) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6720) 
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2133)
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:7872) 
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:936) 
Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/heet/sleepmagic/Hilt_MainApp;
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
... 16 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.heet.sleepmagic.Hilt_MainApp" on path: DexPathList[[zip file "/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/base.apk"],nativeLibraryDirectories=[/data/app/~~vhnGOdaJiLk6ztERW8DzSg==/com.heet.sleepmagic-8TvtgzEu5imBuAIS-WXaGQ==/lib/x86_64, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 19 more

我的build.gradle是这样的:-

 plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id ("dagger.hilt.android.plugin")
}

android {
    namespace = "com.heet.sleepmagic"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.heet.sleepmagic"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.9.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
    implementation("androidx.activity:activity-compose:1.8.0")
    implementation(platform("androidx.compose:compose-bom:2023.03.00"))
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-graphics")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("androidx.compose.material3:material3")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
    androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    // Compose dependencies
    implementation ("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2")
    implementation ("androidx.navigation:navigation-compose:2.7.4")

    //Dagger - Hilt
    implementation ("com.google.dagger:hilt-android:2.42")
    annotationProcessor ("com.google.dagger:hilt-android-compiler:2.42")
    annotationProcessor ("androidx.hilt:hilt-compiler:1.0.0")
    implementation ("androidx.hilt:hilt-navigation-compose:1.0.0")

    //Coil
    implementation ("io.coil-kt:coil-compose:2.3.0")
}

我的清单是这样的:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:name=".MainApp"
        android:theme="@style/Theme.SleepMagic"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.SleepMagic">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的申请类:-

package com.heet.sleepmagic

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp 
class MainApp:Application()

的主要活动 :-

package com.heet.sleepmagic

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.rememberNavController
import com.heet.sleepmagic.ui.navigation.Navigation
import com.heet.sleepmagic.ui.theme.HattonFontFamily
import com.heet.sleepmagic.ui.theme.HattonTypography
import com.heet.sleepmagic.ui.theme.SleepMagicTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SleepMagicTheme {
                Navigation(navController = rememberNavController())
            }
        }
    }
}

在我将我的 android studio 更新到 Giraffe | 2022.3.1 后,这个错误给了我,我认为这是因为任何匕首的类问题或其他原因。帮帮我,这将是非常值得赞赏的。谢谢。

我真的很感谢你的回答。谢谢。

安卓 Kotlin Dagger-2 匕首柄

评论

0赞 Danish Ansari 10/30/2023
你的logcat是空白的,什么都想不通
0赞 heet kanabar 10/30/2023
@DanishAnsari向右滑动,看看我不知道如何解决这个问题
0赞 amtrax 10/30/2023
您是否尝试过使缓存无效?
0赞 heet kanabar 10/30/2023
@amtrax 不,该怎么做?
0赞 amtrax 10/31/2023
@heetkanabar 要清除缓存,请从 Android Studio 的主菜单中选择 文件 |使缓存失效/重新启动。

答:

0赞 heet kanabar 11/2/2023 #1

我得到了解决方案,这是因为我对 gradle 文件的错误实现,我不知道如何在 kotlin 中添加依赖项,所以我学会了它并做了一些这样的更改,它解决了我的问题:-

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    kotlin("kapt")
    id("com.google.dagger.hilt.android")
}

//Dagger - Hilt
    kapt ("androidx.hilt:hilt-compiler:1.0.0")
    implementation ("androidx.hilt:hilt-navigation-compose:1.0.0")
    implementation("com.google.dagger:hilt-android:2.44")
    kapt("com.google.dagger:hilt-android-compiler:2.44")  

我的build.gradle(应用模块):-

plugins {
    id("com.android.application") version "8.1.0" apply false
    id("org.jetbrains.kotlin.android") version "1.8.10" apply false
    id("com.google.dagger.hilt.android") version "2.44" apply false
}

而且我还遇到了另一个错误,我必须将我的 jvm 版本从 1.8 更改为 jvm 17 :-

compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }