Kotlin-dsl-samples: Applieng *kts from groovy does not work correctly

Created on 27 Apr 2018  路  2Comments  路  Source: gradle/kotlin-dsl-samples

I have an android project where I have a separate dependencies.gradle file.
I've just shifted that to dependencies.gradle.kts. Now Gradle don't recognize any implementation configuration anymore :/

Expected Behavior

I want that the dependencies.gradle.kts works like the dependencies.gradle before.

Current Behavior

If I try to run it prints:

Script compilation errors:

  Line 2:     implementation("com.android.support:appcompat-v7:27.1.1")
              ^ Unresolved reference: implementation

  Line 3:     implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
              ^ Unresolved reference: implementation

  Line 4:     implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
              ^ Unresolved reference: implementation

  Line 5:     testImplementation("junit:junit:4.12")
              ^ Unresolved reference: testImplementation

  Line 6:     androidTestImplementation("com.android.support.test:runner:1.0.1")
              ^ Unresolved reference: androidTestImplementation

  Line 7:     androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.1")
              ^ Unresolved reference: androidTestImplementation

Context

Don't know if the "interop" between *kts and groovy is broken or something different ...
Maybe related to #803 ?

Steps to Reproduce (for bugs)

This is my code:


build.gradle

apply plugin: 'com.android.application'
apply from: 'dependencies.gradle.kts'

android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies.gradle.kts

dependencies {
    implementation("com.android.support:appcompat-v7:27.1.1")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
    testImplementation("junit:junit:4.12")
    androidTestImplementation("com.android.support.test:runner:1.0.1")
    androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.1")
}

Your Environment

Gradle: 4.7

question

Most helpful comment

Thanks for the very well done report @StefMa!

This is expected. The Gradle Kotlin DSL can't know at script compilation time that these configurations will be available when the script is applied, it could be applied by any other script. Hence the Kotlin extensions for these configuration not being available at script compilation time. You have to reference the configurations by name at runtime.

Either by string:

// dependencies.gradle.kts
dependencies {
    "implementation"("com.android.support:appcompat-v7:27.1.1")
    "implementation"("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
    "implementation"("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
    "testImplementation"("junit:junit:4.12")
    "androidTestImplementation"("com.android.support.test:runner:1.0.1")
    "androidTestImplementation"("com.android.support.test.espresso:espresso-core:3.0.1")
}

or by bringing them into scope:

// dependencies.gradle.kts
val implementation by configurations
val testImplementation by configurations
val androidTestImplementation by configurations
dependencies {
    implementation("com.android.support:appcompat-v7:27.1.1")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
    testImplementation("junit:junit:4.12")
    androidTestImplementation("com.android.support.test:runner:1.0.1")
    androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.1")
}

All 2 comments

Thanks for the very well done report @StefMa!

This is expected. The Gradle Kotlin DSL can't know at script compilation time that these configurations will be available when the script is applied, it could be applied by any other script. Hence the Kotlin extensions for these configuration not being available at script compilation time. You have to reference the configurations by name at runtime.

Either by string:

// dependencies.gradle.kts
dependencies {
    "implementation"("com.android.support:appcompat-v7:27.1.1")
    "implementation"("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
    "implementation"("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
    "testImplementation"("junit:junit:4.12")
    "androidTestImplementation"("com.android.support.test:runner:1.0.1")
    "androidTestImplementation"("com.android.support.test.espresso:espresso-core:3.0.1")
}

or by bringing them into scope:

// dependencies.gradle.kts
val implementation by configurations
val testImplementation by configurations
val androidTestImplementation by configurations
dependencies {
    implementation("com.android.support:appcompat-v7:27.1.1")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.30")
    testImplementation("junit:junit:4.12")
    androidTestImplementation("com.android.support.test:runner:1.0.1")
    androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.1")
}

Thank you for the answer @eskatos

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mkobit picture mkobit  路  3Comments

jaredsburrows picture jaredsburrows  路  3Comments

iNikem picture iNikem  路  3Comments

deeprim picture deeprim  路  4Comments

jaredsburrows picture jaredsburrows  路  3Comments