[英]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 version
从1.3.21
到1.3.71
,改变它的com.android.tools.build:gradle version
从3.3.2
至3.5.3
Maybe this will help you to solve the error.也许这会帮助您解决错误。
Go to step by step
一步一步去
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") // 之后
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) } }
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个小时的浪费时间,我终于解决了这些问题。
gradlew
in your project root folder.gradlew
。./gradlew tasks
command and enter../gradlew tasks
命令并输入。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 。 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.