Hilt:java.lang.ClassNotFoundException:未找到类“com.kotlin20test.Hilt_MyApp”

Ahm*_*med 8 android dependency-injection kotlin android-studio dagger-hilt

我有一个关于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
Run Code Online (Sandbox Code Playgroud)

这是我的文件:

我的应用程序


@HiltAndroidApp
class MyApp : Application()

Run Code Online (Sandbox Code Playgroud)

构建.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'


}

Run Code Online (Sandbox Code Playgroud)


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
}
Run Code Online (Sandbox Code Playgroud)

Android清单

<?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>

Run Code Online (Sandbox Code Playgroud)

我在另一个使用Kotlin 的应用程序中尝试了 hilt ,但仍然产生了相同的错误,我还在Stack Overflow上阅读了一些关于她的主题的问题,并尝试了一些答案,包括 MultiDexApplication。

编辑:抱歉,我写了错误的AndroidManifest,前一个应用程序是一个具有相同错误的应用程序(我正在尝试两个应用程序),并且在该错误出现之前我将第二个应用程序的 Application 类更改为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>

'''
Run Code Online (Sandbox Code Playgroud)

Ahm*_*med 12

我解决了,原来是我没有提供kapt,

id 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)

并使用使用的实现而不是 kapt 所以而不是:

implementation 'com.google.dagger:hilt-compiler:2.35.1'
Run Code Online (Sandbox Code Playgroud)

我提供:

kapt 'com.google.dagger:hilt-compiler:2.35.1'
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助某人

  • 即使有了 kapt 之后,我仍然面临这个问题。你能解决吗? (3认同)