Not able to copy configurations dependencies after

2020-02-03 11:21发布

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.

6条回答
小情绪 Triste *
2楼-- · 2020-02-03 12:09

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.

查看更多
爷、活的狠高调
3楼-- · 2020-02-03 12:11

I make configuration can resolved, so no exception get dependcenies's file

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

println configurations.implementation.resolve()
println configurations.api.resolve()
查看更多
地球回转人心会变
4楼-- · 2020-02-03 12:12

Another suggestion.

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

configurations {
  customConfig.extendsFrom implementation
}
查看更多
虎瘦雄心在
5楼-- · 2020-02-03 12:12

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.

查看更多
聊天终结者
6楼-- · 2020-02-03 12:16

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

you can also think about: compileClasspath default

查看更多
Evening l夕情丶
7楼-- · 2020-02-03 12:25

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) }}
查看更多
登录 后发表回答