11

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?

Lemao1981
  • 2,225
  • 5
  • 18
  • 30
  • I have the same problem. Did you find a solution? – Henning Jan 07 '20 at 12:54
  • 1
    We faced the same problem.. All we had to do is "Apply Context" that showed in top ribbon (like "Sync Now" option comes up when you change gradle file). – HBB20 Mar 03 '20 at 22:09

5 Answers5

2

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")
}
Akshay Raiyani
  • 1,243
  • 9
  • 21
1

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.

Mahavir Jain
  • 1,448
  • 10
  • 23
1

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.

Mehedi Hasan
  • 137
  • 1
  • 5
0

what works for me was adding:

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

and then i can use this normally:

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

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

Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
  • 1
    I did all that already and in the meanwhile it´s working, its behaviour is just very weird, fragile, unreliable. Sometimes you do changes, do gradle sync and stuff turns red. Although everything is totally fine, you need to comment out certain lines in a specific way, try resyncs, sometimes need to wait until a message pops up with a link 'Apply dependencies' after doing changes in buildSrc folder (or for whatever reason), but this message might have quite some delay etc. Just very annoying and awkward to work with. No idea why they gave us such a pain in the ass – Lemao1981 Mar 18 '19 at 05:41
  • I need to add that I work a lot with kotlin object {} constants, nearly every string or int value, e.g. all the dependencies + versions, are referenced from buildSrc folder to each gradle file. I got the feeling that that increases the fragility. Sometimes only replacing the constants with the real value helped and got the sync through. Only then could use the constant reference again successfully. Lot of work and anger – Lemao1981 Mar 18 '19 at 05:44