简体   繁体   English

Gradle kotlin-dsl 配置不起作用,无法识别 android 扩展功能

[英]Gradle kotlin-dsl configuration not working, android extension function not recognized

I´m trying to configure my gradle setup with kotlin-dsl.我正在尝试使用 kotlin-dsl 配置我的 gradle 设置。 My project gradle looks like this:我的项目 gradle 是这样的:

buildscript {
    repositories {
        google()
        jcenter()
    }

dependencies {
        classpath("com.android.tools.build:gradle:3.3.2")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21")
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

and my app-level gradle:和我的应用程序级gradle:

plugins {
    id("com.android.application")
    id("kotlin-android")
}

android {  // marked red

}

Both filenames are build.gradle.kts.两个文件名都是 build.gradle.kts。 Gradle version is 5.2.1. Gradle 版本是 5.2.1。 I have the problem that the android {} part in the app-gradle file is not recognized by the IDE, I´m not able to configure things like compileSdkVersion etc. One more time I´m trying to migrate a project to kotlin-dsl and once more, nothing but failing and frustration.我遇到的问题是 IDE 无法识别 app-gradle 文件中的 android {} 部分,我无法配置 compileSdkVersion 等内容。我又一次尝试将项目迁移到 kotlin-dsl再一次,只有失败和沮丧。 No idea why this is never working and always let me keep the old way.不知道为什么这永远行不通,总是让我保持旧的方式。 What am I doing wrong, why isn´t that working?我做错了什么,为什么不起作用?

I faced the same thing as the android tag view in red.我遇到了与红色的 android 标签视图相同的事情。 How I resolved my error, I just rebuild the project from Build option.我如何解决我的错误,我只是从 Build 选项重建项目。 (Build -> Rebuild Project) and I see that progruardFiles line is not as per guideline like all strings must be in double-quotes. (Build -> Rebuild Project) 并且我看到 progruardFiles 行不像所有字符串都必须用双引号引起来那样符合准则。 I replace that with我用

proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")

After that again I rebuild the project and Gradle suggests me that "Add Dependiences in your project", after a click on that the issue is resolved.之后我再次重建项目,Gradle 建议我“在你的项目中添加依赖项”,点击后问题就解决了。

Here is my module-level Gradle这是我的模块级 Gradle

plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
    kotlin("kapt")
}
android {
    compileSdkVersion(29)
    defaultConfig {
        applicationId = "com.example.app"
        minSdkVersion(21)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")}
}
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" tolistOf("*.jar"))))
implementation ("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41")
implementation ("androidx.appcompat:appcompat:1.0.2")
implementation ("androidx.core:core-ktx:1.0.2")
implementation ("androidx.constraintlayout:constraintlayout:1.1.3")
testImplementation ("junit:junit:4.12")
androidTestImplementation ("androidx.test:runner:1.2.0")
androidTestImplementation ("androidx.test.espresso:espresso-core:3.2.0")
}

I also got the same problem, after upgrading kotlin version from 1.3.21 to 1.3.71 and also change the com.android.tools.build:gradle version from 3.3.2 to 3.5.3我也得到了同样的问题,升级后kotlin version1.3.211.3.71 ,改变它的com.android.tools.build:gradle version3.3.23.5.3

Maybe this will help you to solve the error.也许这会帮助您解决错误。

Go to step by step一步一步去

  1. Update to Gradle Wrapper 5.0 or higher更新到 Gradle Wrapper 5.0 或更高版本
  2. Rename root -> settings.gradle to root -> settings.gradle.kts and in file将 root -> settings.gradle 重命名为 root -> settings.gradle.kts 并在文件中

    include ':app' // before包括 ':app' // 之前

    include(":app") // after include(":app") // 之后

  3. Rename root -> build.gradle to root -> build.gradle.kts and same changes as you do in that file将 root -> build.gradle 重命名为 root -> build.gradle.kts 和在该文件中所做的相同更改

    buildscript {构建脚本{

     repositories { jcenter() google() } dependencies { classpath("com.android.tools.build:gradle:3.3.1") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11") } } allprojects { repositories { jcenter() google() } } tasks { val clean by registering(Delete::class) { delete(buildDir) } }
  4. Same Rename app -> build.gradle to app -> build.gradle.kts and same changes as you do in that file同样重命名应用程序-> build.gradle 到应用程序-> build.gradle.kts 和您在该文件中所做的相同更改

After 4 hours of wasting my time, finally, I solved these issues.经过4个小时的浪费时间,我终于解决了这些问题。

  1. Find gradlew in your project root folder.在您的项目根文件夹中找到gradlew
  2. Right click on it, then open in -> terminal .右键单击它,然后在 -> terminal 中打开。
  3. Write ./gradlew tasks command and enter.编写./gradlew tasks命令并输入。
  4. If showing any error in your build.gradle file then fix it and rerun ./gradlew tasks command until all errors are solved and you can see BUILD SUCCESSFUL .如果在build.gradle文件中显示任何错误,请修复它并重新运行./gradlew tasks命令,直到解决所有错误并且您可以看到BUILD SUCCESSFUL
  5. Now sync your gradle file.现在同步你的 gradle 文件。

After syncing is completed, all extension functions will be recognized by android studio and red marks will be vanished.同步完成后,所有扩展功能都会被android studio识别,红色标记会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts - gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts 如何在Kotlin-dsl中使用多种口味? - How to use multiple flavors in gradle with kotlin-dsl? Gradle Kotlin DSL中Dynatrace的配置 - Configuration of Dynatrace in Gradle Kotlin DSL 如何在 Kotlin Gradle DSL 中为多个模块重用部分 Android 配置? - How to reuse parts of Android configuration in Kotlin Gradle DSL for multiple modules? 使用 Gradle Kotlin-DSL 时,如何使用 flavorDimensions 为每个风味组合设置不同的 applicationId? - How to set different applicationId for each flavor combination using flavorDimensions when using Gradle Kotlin-DSL? 使用 Kotlin DSL 迁移 Android Gradle 插件 7.0.0 - Android Gradle Plugin 7.0.0 migration with Kotlin DSL 如何在 Gradle Kotlin DSL 中配置 Firebase 性能监控插件扩展 - How to configure Firebase Performance Monitoring plugin extension in Gradle Kotlin DSL Gradle Kotlin DSL:检查“可用的较新库版本”不起作用 - Gradle Kotlin DSL: Inspection "Newer Library Versions Available" not working Android Gradle:未找到 ID 为“XXXX”的自定义插件 - Kotlin DSL - Android Gradle: Custom Plugin with id 'XXXX' not found - Kotlin DSL Gradle Kotlin DSL:在唯一位置定义 Android 构建工具版本 - Gradle Kotlin DSL: Define Android build tools version in unique place
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM