英文:
I am using the latest kts build tool to learn Android hilt, but kapt seems to have some issues
问题
以下是翻译好的部分:
这是项目的根级别 build.gradle.kts 文件
// 顶层构建文件,您可以在其中添加所有子项目/模块共有的配置选项。
plugins {
id("com.android.application") version "8.2.0-alpha04" apply false
id("com.android.library") version "8.0.0" apply false
id("org.jetbrains.kotlin.android") version "1.8.20" apply false
id("com.google.dagger.hilt.android") version "2.44" apply false
}
这是项目的 app 模块的 build.gradle.kts 文件
import org.jetbrains.kotlin.kapt3.base.Kapt.kapt
plugins {
id("com.android.application")
kotlin("kapt")
id("com.google.dagger.hilt.android")
}
android {
namespace = "com.example.myapplication"
compileSdk = 33
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 29
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
viewBinding {
enable = true
}
}
dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
compileOnly("org.projectlombok:lombok:1.18.26")
annotationProcessor("org.projectlombok:lombok:1.18.26")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
}
// 允许引用生成的代码
kapt {
correctErrorTypes = true
}
这是构建项目时出现的错误
> 配置项目:app
e:C:\ Users \ 123 \ AndroidStudioProjects \ MyApplication \ app \ build.gradle.kts:52:10:类型不匹配:推断的类型为 String,但期望的是 Action<KaptExtension>
FAILURE:构建失败,发生异常。
*在哪里:
构建文件'C:\ Users \ 123 \ AndroidStudioProjects \ MyApplication \ app \ build.gradle.kts'的52行
*出了什么问题:
脚本编译错误:
第52行:kapt("com.google.dagger:hilt-android-compiler:2.44")
^ 类型不匹配:推断的类型为 String,但期望的是 Action<KaptExtension>
1个错误
*尝试:
> 使用--info或--debug选项运行以获得更多日志输出。
> 使用--scan运行以获得完整的见解。
英文:
I am using the latest kts build tool to learn Android hilt, and I have introduced dependencies according to the official tutorial, but kapt seems to have some issues:
This is the root level build.gradle.kts of the project
plugins {
id("com.android.application") version "8.2.0-alpha04" apply false
id("com.android.library") version "8.0.0" apply false
id("org.jetbrains.kotlin.android") version "1.8.20" apply false
id("com.google.dagger.hilt.android") version "2.44" apply false
}
This is the app build.gradle.kts of the project
import org.jetbrains.kotlin.kapt3.base.Kapt.kapt
plugins {
id("com.android.application")
kotlin("kapt")
id("com.google.dagger.hilt.android")
}
android {
namespace = "com.example.myapplication"
compileSdk = 33
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 29
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
viewBinding {
enable = true
}
}
dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
compileOnly("org.projectlombok:lombok:1.18.26")
annotationProcessor("org.projectlombok:lombok:1.18.26")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
}
// Allow references to generated code
kapt {
correctErrorTypes = true
}
This is an error while building the project
> Configure project :app
e: C:\Users3\AndroidStudioProjects\MyApplication\app\build.gradle.kts:52:10: Type mismatch: inferred type is String but Action<KaptExtension> was expected
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users3\AndroidStudioProjects\MyApplication\app\build.gradle.kts' line: 52
* What went wrong:
Script compilation error:
Line 52: kapt("com.google.dagger:hilt-android-compiler:2.44")
^ Type mismatch: inferred type is String but Action<KaptExtension> was expected
1 error
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> ```
</details>
# 答案1
**得分**: 3
我已解决了这个问题,因为我的项目是Java代码,所以不需要引入kapt。
请将这样写:
```java
annotationProcessor("com.google.dagger:hilt-android-compiler:2.44")
而不是
kapt("com.google.dagger:hilt-android-compiler:2.44")
现在您可以使用依赖注入。
英文:
I have resolved this issue because my project is Java code, so there is no need to introduce kapt.
Write this:
annotationProcessor("com.google.dagger:hilt-android-compiler:2.44")
instead of
kapt("com.google.dagger:hilt-android-compiler:2.44")
You can use dependency injection now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论