我有一个关于 Hilt 的错误,我一直在尝试注入我使用 Hilt 创建的改造界面,
这里是错误:
java.lang.ClassNotFoundException: Didn't find class "com.kotlin20test.Hilt_MyApp" on path: DexPathList[[zip file "/data/app/com.example.kotlin20test-hKFhgE2D6vBE-1ZkVY-UOA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.kotlin20test-hKFhgE2D6vBE-1ZkVY-UOA==/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)
... 16 more
这是我的文件:
我的应用
@HiltAndroidApp
class MyApp : Application()
build.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'dagger.hilt.android.plugin'
}
android {
//View Binding
buildFeatures {
viewBinding true
}
compileSdkVersion 30
defaultConfig {
applicationId "com.example.kotlin20test"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
multiDexEnabled true
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled 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'
}
dexOptions {
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/gradle/incremental.annotation.processors'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//Room
def room_version = "2.2.6"
def lifecycle_version = "2.3.1"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
apply plugin: 'kotlin-kapt'
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation 'androidx.room:room-ktx:2.3.0'
annotationProcessor "androidx.room:room-compiler:2.3.0"
//Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3'
def multidex_version = "2.0.1"
implementation "androidx.multidex:multidex:$multidex_version"
//Activity Result API
implementation "androidx.activity:activity-ktx:1.3.0-alpha07"
implementation 'androidx.fragment:fragment-ktx:1.3.3'
//Retrofit and Gson
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
//Dagger Hilt
implementation 'com.google.dagger:hilt-android:2.35.1'
implementation 'com.google.dagger:hilt-compiler:2.35.1'
implementation 'com.google.android.gms:play-services:12.0.1'
implementation 'androidx.multidex:multidex:2.0.0'
}
buildscript {
ext.kotlin_version = "1.3.50"
repositories {
google()
jcenter()
}
ext.hilt_version = '2.35'
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.35.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kotlintest">
<application
tools:remove="android:appComponentFactory"
android:name=".Hilt_MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KotlinTest">
<activity android:name=".MainActivity2"
></activity>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ExampleBroadcast"/>
</application>
</manifest>
我在另一个使用 Kotlin 的应用程序中尝试了 hilt,但仍然产生相同的错误,我也读过 关于她在 Stack Overflow 上的几个问题,并尝试了一些答案,包括 MultiDex 应用程序。
编辑:抱歉我写错了AndroidManifest,上一个应用程序是 有相同错误的应用程序(我正在尝试两个应用程序),我更改了第二个应用程序 应用程序类到 Hilt_MyApp 类之前出现该错误希望 试图解决错误。
这是与此应用相关的真实清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kotlin20test">
<application
android:name="com.kotlin20test.MyApp"
tools:remove="android:appComponentFactory"
tools:targetApi="p"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Kotlin20Test">
<activity android:name="com.kotlin20test.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'''