Not able to copy configurations dependencies after upgrading Gradle plugin for Android Studio to 3.0.1 and Gradle to 4.1

AndroidAndroid StudioAndroid Gradle-Pluginbuild.gradleGradle Plugin

Android Problem Overview


I used to copy 'compile' dependencies to a specific folder using this simple gradle task :

task copyLibs(type: Copy) {
	from configurations.compile
	into "$project.rootDir/reports/libs/"
}

But it stopped working just after upgrading my Android project using gradle plugin 3.0.1 for Android Studio and Gradle tool to 4.1. As the dependency configuration 'compile' is now deprecated by https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations I changed it to 'implementation'. However, I am not able to use my copyLibs task as resolving configuration 'implementation' directly is not allowed as per Gradle build error output :

$ ./gradlew.bat clean build

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:copyLibs'.
> Resolving configuration 'implementation' directly is not allowed

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

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

BUILD FAILED in 1s

See following my current build.gradle file for app module : apply plugin: 'com.android.application'

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

dependencies {
	implementation fileTree(dir: 'libs', include: ['*.jar'])
	implementation 'com.android.support:appcompat-v7:26.1.0'
	implementation 'com.android.support:support-v4:26.1.0'
	implementation 'com.android.support:design:26.1.0'
	implementation 'com.android.support.constraint:constraint-layout:1.0.2'
	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'
}

task copyLibs(type: Copy) {
	from configurations.implementation
	into "$project.rootDir/reports/libs/"
}
build.dependsOn copyLibs

If I use 'compile' it works but I would like to be compliant with the latest recommendation on this plugin the usage.

I need help to upgrade my copyLibs task in order to work as before upgrading my enviromment. I was using gradle plugin 2.2.3 for Android Studio and Gradle tool 2.14.1.

Android Solutions


Solution 1 - Android

instead of using configurations.implementation, the best option is to use configurations.runtimeClasspath.

You can also think about: compileClasspath default

Solution 2 - Android

I make the configuration resolvable, so there is no exception when getting the dependencies

configurations.implementation.setCanBeResolved(true)
configurations.api.setCanBeResolved(true)

println configurations.implementation.resolve()
println configurations.api.resolve()

Solution 3 - Android

Another suggestion.

I created my custom config and then used it as configurations.customConfig:

configurations {
  customConfig.extendsFrom implementation
}

The copy task must be correspondingly edited:

task copyLibs(type: Copy) {
    from configurations.customConfig
    into "$project.rootDir/reports/libs/"
}

Solution 4 - Android

I wanted to make a comment, but I'm still lacking the reputation required to do so (by one point!! >.< ). It doesn't look as if there's a way to acquire the list of implementation dependencies and the compileClasspath mentioned at the Gradle ticket Rafael posted won't work if you're working with Android directly, like my case where I need the dependencies to be exported so that Unity3D can package them up for release.

So.. it looks like the only solution in this case is to use the deprecated compile type.

Solution 5 - Android

This probably won't help or have a better way to solve but...

You can put your dependencies in a way that is possible to copy doing the following:

android { ... }

// Add a new configuration to hold your dependencies
configurations {
    myConfig
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    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'

    // Now you have to repeat adding the dependencies you want to copy in the 'myConfig'
    myConfig fileTree(dir: 'libs', include: ['*.jar'])
    myConfig 'com.android.support:appcompat-v7:26.1.0'
    myConfig 'com.android.support:support-v4:26.1.0'
    ...
}

task copyLibs(type: Copy) {
    // Now you can use 'myConfig' instead of 'implementation' or 'compile' 
    from configurations.myConfig 
    into "$project.rootDir/reports/libs/"
}

This also helps if you have a Jar task that stops placing the dependencies in to the jar file because you changed from compile to implementation.

You can use:

from {configurations.myConfig.collect { it.isDirectory() ? it : zipTree(it) }}

Instead of:

from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}

Solution 6 - Android

I started getting this error after upgrading from gradle 5.5 to 5.6, and it happens when I try to sync the project in intelliJ.

Thanks to this answer on another question, I solved it by applying the idea plugin to all projects and then running gradle cleanIdea and after that everything started working again.

Another day, another #inexplicable solution to a problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRafaelView Question on Stackoverflow
Solution 1 - Androidthe anonymous userView Answer on Stackoverflow
Solution 2 - Androidact262View Answer on Stackoverflow
Solution 3 - AndroidmortalisView Answer on Stackoverflow
Solution 4 - AndroidCornelia XaosView Answer on Stackoverflow
Solution 5 - AndroidViníciusPJView Answer on Stackoverflow
Solution 6 - Androidsmac89View Answer on Stackoverflow