Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

New configurations (implementation, testImplementation, etc) do not work in the dependencies block for an Android project #803

Closed
netvl opened this issue Apr 2, 2018 · 7 comments

Comments

@netvl
Copy link

netvl commented Apr 2, 2018

Expected Behavior

implementation, testImplementation etc methods in the dependencies block should work correctly.

Current Behavior

implementation, testImplementation etc methods give the "unresolved reference" errors:

Script compilation errors:

  Line 49:     implementation("org.jetbrains.kotlin:kotlin-stdlib:1.2.31")
               ^ Unresolved reference: implementation

  Line 50:     implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.31")
               ^ Unresolved reference: implementation

Context

I'm migrating my Android project to use the Kotlin Gradle DSL for the build description. It went mostly smoothly, however, it seems that I can't use the implementation, testImplementation and other new configurations for some reason in Kotlin DSL. It was possible in the regular Groovy DSL, however.

Steps to Reproduce (for bugs)

I'm not sure how to reproduce it. Apparently, the example project "hello-android" works correctly when compile is changed to implementation.

Your Environment

I'm using Gradle 4.6 (also tried with 4.5.1), and I have the following plugins section:

plugins {
    id("com.android.application").version("3.0.1")  // also tried with 3.1.0
    kotlin("android").version("1.2.31")
    kotlin("android.extensions").version("1.2.31")
    kotlin("kapt").version("1.2.31")
}

The first plugin is made possible to be enabled by additional configuration in settings.gradle.kts:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.android.application") {
                useModule("com.android.tools.build:gradle:${requested.version}")
            }
        }
    }
}
@JLLeitschuh
Copy link
Contributor

Solution

I think you need to explicitly add java-library to your plugins block like so for implementation and testImplementation to automatically get generated for you:

plugins {
    `java-library`
}

An alternative you can use is this:

dependencies {
    "implementation"("org.jetbrains.kotlin:kotlin-stdlib:1.2.31")
    "implementation"("org.jetbrains.kotlin:kotlin-reflect:1.2.31")
}

Question for the Gradle Team

@bamboo @eskatos @eriwen:

Is there any way that we can make this error clearer to users that the problem they are experiencing originates from Gradle not knowing about the configuration?

I think that Gradle needs to provide more useful information when the compiler can't find things like implementation or testImplementation. What are your thoughts on this?

@StefMa
Copy link
Contributor

StefMa commented Apr 2, 2018 via email

@JLLeitschuh
Copy link
Contributor

Taking a second look at this it might be because the android plugin isn't designed to create the configurations up front and instead adds them later in the plugin lifecycle than the kotlin-dsl project evaluates to when generating the accessors.

@eskatos
Copy link
Member

eskatos commented Apr 3, 2018

Hi @netvl,
Thanks for the report.

If you look at the hello-android sample, accessors to compile, implementation and other configurations are available.

Could you provide a reproducer build or all your build scripts?
Could it be that you were trying to cross-configure projects in a multi-project build?

@netvl
Copy link
Author

netvl commented Apr 5, 2018

@eskatos yes, as I said, for some strange reason hello-android does have the implementation method, while my project does not.

Apparently if I extract only the build files from my project (and add a virtually empty AndroidManifest.xml) then the problem can be reproduced. Please see an example project here: https://github.com/netvl/gradle-kotlin-dsl-missing-impl-example. For me, running e.g. ./gradlew tasks results in the following error:

> ./gradlew tasks
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
e: /home/netvl/dev/lang/kotlin/projects/impl-bug-repr/app/build.gradle.kts:50:5: Unresolved reference: implementation
e: /home/netvl/dev/lang/kotlin/projects/impl-bug-repr/app/build.gradle.kts:51:5: Unresolved reference: implementation
Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.

FAILURE: Build failed with an exception.

* Where:
Build file '/home/netvl/dev/lang/kotlin/projects/impl-bug-repr/app/build.gradle.kts' line: 50

* What went wrong:
Script compilation errors:

  Line 50:     implementation("org.jetbrains.kotlin:kotlin-stdlib:1.2.31")
               ^ Unresolved reference: implementation

  Line 51:     implementation("org.jetbrains.kotlin:kotlin-reflect:1.2.31")
               ^ Unresolved reference: implementation

2 errors

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

I'm not sure what does "cross-configure" mean. I do have several projects in my build, but as far as I understand, they are not affecting each other's configuration,.

@eskatos
Copy link
Member

eskatos commented Apr 5, 2018

@netvl thanks for the reproducer, much easier to know exactly what is happening.

I noticed that you use the gradle/project-schema.json snapshot. Meaning there's no automatic discovery of the project schema and that you must manage it yourself (using tasks, not editing it by hand). And, your snapshot file was actually outdated. Removing that file fixed the build. This gradle/project-schema.json snapshot has been helpful in the early Kotlin DSL days but you should not need it anymore in the general case.

@netvl
Copy link
Author

netvl commented Apr 6, 2018

@eskatos I have no idea what that file does. I'm using IntelliJ IDEA, maybe it generated this file? Indeed, I have removed this file from the project, and it works now. Thank you very much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants