Nen*_*vic 9 android gradle kotlin android-room dagger-hilt
在 Android Studio 中构建我的项目时,我收到了这个信息不多的错误。我已经尝试了 stackoverflow 上的所有方法,但没有任何效果。
任务“:app:kaptDebugKotlin”执行失败。
执行 org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InitationTargetException 时发生失败(无错误消息)
我的应用程序等级:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
configurations.all {
resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
}
applicationId "com.nenad.favrecipes"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
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'
}
android {
buildFeatures {
dataBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" //1.5.21
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
// RxJava
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-scalars:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
//shimmering RV
implementation 'com.facebook.shimmer:shimmer:0.5.0'
implementation 'com.todkars:shimmer-recyclerview:0.4.1'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.datastore:datastore:1.0.0"
implementation("androidx.fragment:fragment-ktx:1.3.0")
def room_version = '2.3.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
kapt {
correctErrorTypes true
}
}
Run Code Online (Sandbox Code Playgroud)
我的项目等级:
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3' //4.1.3
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id "org.jetbrains.kotlin.android" version "1.4.20" apply false
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 Mac M1 芯片上的 Room\n请确保您使用 Room 版本 2.4.0-alpha03 或更高版本。
\n正如jetpack(版本2.4.0-alpha03)所述
\n修复了 Room\xe2\x80\x99s SQLite 本机库的问题,以支持 Apple\xe2\x80\x99s M1 芯片。\n将版本更改为 2.4.0-alpha03 或更高版本
\nimplementation "androidx.room:room-runtime:2.4.0-alpha03"\nannotationProcessor "androidx.room:room-compiler:2.4.0-alpha03"\nkapt 'androidx.room:room-compiler:2.4.0-alpha03'\nRun Code Online (Sandbox Code Playgroud)\n
对我来说,有效的是将 hilt 库更新到最新版本。
项目 gradle 中的两个类路径
classpath("com.google.dagger:hilt-android-gradle-plugin:2.48")
Run Code Online (Sandbox Code Playgroud)
并在应用程序 gradle 中
implementation("com.google.dagger:hilt-android:2.48")
kapt("com.google.dagger:hilt-android-compiler:2.48")
Run Code Online (Sandbox Code Playgroud)
确保您在项目 gradle 类路径和应用程序 gradle 中使用相同的版本。
有时 Android Studio 不会警告我们更新到最新版本的依赖项,我们可能会认为我们正在使用最新版本,但事实可能并非如此,Android Studio 会不断抛出错误并浪费我们的时间。最后更新到最新版本后问题得到解决。
要获取最新版本的 hilt 依赖项,请检查此URL。
小智 3
您的应用程序级别模块应该如下所示:
构建.gradle(:应用程序)
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3' //4.1.3
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
并且,另一个 gradle 文件应该如下所示:
构建.gradle(:应用程序)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
configurations.all {
resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
}
applicationId "com.nenad.favrecipes"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
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'
}
buildFeatures {
dataBinding true
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21" //1.5.21
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
// RxJava
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-scalars:2.9.0"
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.okhttp3:okhttp:4.9.1"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
implementation "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
//shimmering RV
implementation 'com.facebook.shimmer:shimmer:0.5.0'
implementation 'com.todkars:shimmer-recyclerview:0.4.1'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.google.dagger:hilt-android:2.40.5'
kapt 'com.google.dagger:hilt-compiler:2.40.5'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.datastore:datastore:1.0.0"
implementation("androidx.fragment:fragment-ktx:1.3.0")
def room_version = '2.3.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
kapt {
correctErrorTypes true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37055 次 |
| 最近记录: |