Lemao1981
Lemao1981

Reputation: 2365

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?

Upvotes: 13

Views: 18317

Answers (5)

Dawid Czopek
Dawid Czopek

Reputation: 119

what works for me was adding:

plugins {  
    id("com.android.library")
}

and then i can use this normally:

android {
    namespace = "com.example.whatever"
}

Upvotes: 0

Mehedi Hasan
Mehedi Hasan

Reputation: 155

After 4 hours of wasting my time, finally, I solved these issues.

  1. Find gradlew in your project root folder.
  2. Right click on it, then open in -> terminal.
  3. Write ./gradlew tasks command and enter.
  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.
  5. Now sync your gradle file.

After syncing is completed, all extension functions will be recognized by android studio and red marks will be vanished.

Upvotes: 0

Mahavir Jain
Mahavir Jain

Reputation: 1581

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

Maybe this will help you to solve the error.

Upvotes: 1

Akshay Raiyani
Akshay Raiyani

Reputation: 1443

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")
}

Upvotes: 2

Shweta Chauhan
Shweta Chauhan

Reputation: 6981

Go to step by step

  1. Update to Gradle Wrapper 5.0 or higher
  2. Rename root -> settings.gradle to root -> settings.gradle.kts and in file

    include ':app' // before

    include(":app") // after

  3. Rename root -> build.gradle to root -> build.gradle.kts and same changes as you do in that file

    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

Upvotes: -1

Related Questions