gpt4 book ai didi

android - Hilt : java. lang.ClassNotFoundException: 找不到类 "com.kotlin20test.Hilt_MyApp"

转载 作者:行者123 更新时间:2023-12-05 00:10:27 25 4
gpt4 key购买 nike

我有一个关于 Hilt 的错误,我一直在尝试注入(inject)我使用 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 但仍然产生相同的错误,我也读过
上关于这个主题的几个问题堆栈溢出 ,并尝试一些答案,包括
MultiDex应用程序。
编辑:对不起,我写错了 AndroidManifest ,以前的应用程序是一个
具有相同错误的应用程序(我正在尝试两个应用程序),我更改了第二个
申请类别至 Hilt_MyApp 在那个错误出现之前上课,希望
试图解决错误。
这是与此应用相关的真实 list :

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

'''

最佳答案

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

id 'kotlin-kapt'
并使用了使用的实现而不是 kapt 所以而不是:
implementation 'com.google.dagger:hilt-compiler:2.35.1'
我提供:
kapt 'com.google.dagger:hilt-compiler:2.35.1'
希望这可以帮助某人

关于android - Hilt : java. lang.ClassNotFoundException: 找不到类 "com.kotlin20test.Hilt_MyApp",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67650422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com