我正在使用最新的kts构建工具来学习Android Hilt,但是kapt似乎有一些问题。

huangapple go评论68阅读模式
英文:

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(&quot;com.android.application&quot;) version &quot;8.2.0-alpha04&quot; apply false
    id(&quot;com.android.library&quot;) version &quot;8.0.0&quot; apply false
    id(&quot;org.jetbrains.kotlin.android&quot;) version &quot;1.8.20&quot; apply false
    id(&quot;com.google.dagger.hilt.android&quot;) version &quot;2.44&quot; apply false
}

This is the app build.gradle.kts of the project

import org.jetbrains.kotlin.kapt3.base.Kapt.kapt

plugins {
    id(&quot;com.android.application&quot;)
    kotlin(&quot;kapt&quot;)
    id(&quot;com.google.dagger.hilt.android&quot;)
}

android {
    namespace = &quot;com.example.myapplication&quot;
    compileSdk = 33

    defaultConfig {
        applicationId = &quot;com.example.myapplication&quot;
        minSdk = 29
        targetSdk = 33
        versionCode = 1
        versionName = &quot;1.0&quot;

        testInstrumentationRunner = &quot;androidx.test.runner.AndroidJUnitRunner&quot;
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile(&quot;proguard-android-optimize.txt&quot;),
                &quot;proguard-rules.pro&quot;
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    viewBinding {
        enable = true
    }
}

dependencies {

    implementation(&quot;androidx.constraintlayout:constraintlayout:2.1.4&quot;)
    compileOnly(&quot;org.projectlombok:lombok:1.18.26&quot;)
    annotationProcessor(&quot;org.projectlombok:lombok:1.18.26&quot;)
    implementation(&quot;androidx.appcompat:appcompat:1.6.1&quot;)
    implementation(&quot;com.google.android.material:material:1.8.0&quot;)
    testImplementation(&quot;junit:junit:4.13.2&quot;)
    androidTestImplementation(&quot;androidx.test.ext:junit:1.1.5&quot;)
    androidTestImplementation(&quot;androidx.test.espresso:espresso-core:3.5.1&quot;)
    implementation(&quot;com.google.dagger:hilt-android:2.44&quot;)
    kapt(&quot;com.google.dagger:hilt-android-compiler:2.44&quot;)
}
// Allow references to generated code
kapt {
    correctErrorTypes = true
}

This is an error while building the project

&gt; Configure project :app
e: C:\Users3\AndroidStudioProjects\MyApplication\app\build.gradle.kts:52:10: Type mismatch: inferred type is String but Action&lt;KaptExtension&gt; was expected

FAILURE: Build failed with an exception.

* Where:
Build file &#39;C:\Users3\AndroidStudioProjects\MyApplication\app\build.gradle.kts&#39; line: 52

* What went wrong:
Script compilation error:

  Line 52:     kapt(&quot;com.google.dagger:hilt-android-compiler:2.44&quot;)
                    ^ Type mismatch: inferred type is String but Action&lt;KaptExtension&gt; was expected

1 error

* Try:

&gt; Run with --info or --debug option to get more log output.
&gt; Run with --scan to get full insights.
&gt; ```



</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(&quot;com.google.dagger:hilt-android-compiler:2.44&quot;)

instead of

kapt(&quot;com.google.dagger:hilt-android-compiler:2.44&quot;)

You can use dependency injection now.

huangapple
  • 本文由 发表于 2023年5月17日 17:58:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定