我曾经使用这个简单的gradle任务将’compile’依赖项复制到特定文件夹:
task copyLibs(type: Copy) { from configurations.compile into "$project.rootDir/reports/libs/" }
但是在使用gradle plugin 3.0.1 for Android Studio和Gradle工具升级到4.1后,它刚刚升级我的Android项目后停止工作.由于依赖配置’compile’现在已被https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations弃用,我将其更改为’implementation’.但是,我无法使用我的copyLibs任务,因为根据Gradle构建错误输出不允许直接解析配置’implementation’:
$./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
请参阅以下我对app模块的当前build.gradle文件: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
如果我使用’compile’它可以工作,但我希望符合这个插件的最新推荐用法.
我需要帮助来升级我的copyLibs任务,以便在升级我的环境之前工作.
我正在使用gradle插件2.2.3 for Android Studio和Gradle工具2.14.1.
解决方法
这可能无济于事或有更好的解决方法,但……
您可以以可以复制的方式放置依赖项,执行以下操作:
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/" }
如果您有一个Jar任务停止将依赖项放入jar文件,因为您从编译更改为实现,这也会有所帮助.
您可以使用:
from {configurations.myConfig.collect { it.isDirectory() ? it : zipTree(it) }}
代替:
from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}