Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I´m trying to configure my gradle setup with kotlin-dsl. My project gradle looks like this:

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:

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

android {  // marked red

}

Both filenames are build.gradle.kts. Gradle version is 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. 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?

like image 851
Lemao1981 Avatar asked Mar 17 '19 07:03

Lemao1981


People also ask

What is Kotlin Gradle DSL?

Kotlin DSL brings the simplicity of the Kotlin language syntax and rich API set right into the script files on top of that code completion makes it perfect to work with Gradle script files. We will use this to manage our dependencies and project settings configurations more elegantly.

What is Gradle plugin DSL?

The plugins DSL provides a succinct and convenient way to declare plugin dependencies. It works with the Gradle plugin portal to provide easy access to both core and community plugins. The plugins DSL block configures an instance of PluginDependenciesSpec.

Which DSL does Gradle use?

Kotlin DSL is fully supported in Intellij IDEA and Android Studio. Other IDEs, such as Eclipse or NetBeans, do not yet provide helpful tools for editing Gradle Kotlin DSL files, however, importing and working with Kotlin DSL-based builds work as usual.


1 Answers

I faced the same thing as the android tag view in red. How I resolved my error, I just rebuild the project from Build option. (Build -> Rebuild Project) and I see that progruardFiles line is not as per guideline like all strings must be in double-quotes. 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.

Here is my module-level 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")
}
like image 52
Akshay Raiyani Avatar answered Oct 24 '22 20:10

Akshay Raiyani