android - 将 Android Studio 的 Gradle 插件升级到 3.0.1 和 Gradle 到 4.1 后无法复制配置依赖项

标签 android android-studio android-gradle-plugin build.gradle gradle-plugin

我曾经使用这个简单的 gradle 任务将“编译”依赖项复制到特定文件夹:

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

但在使用 gradle plugin 3.0.1 for Android Studio 和 Gradle 工具升级我的 Android 项目到 4.1 后,它就停止了工作。由于依赖配置“编译”现在已被 https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations 弃用。我将其更改为“实现”。但是,我无法使用我的 copyLibs 任务,因为根据 Gradle 构建错误输出,不允许直接解析配置“实现”:

$ ./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

如果我使用“编译”,它可以工作,但我希望遵守有关此插件使用的最新建议。

我需要帮助来升级我的 copyLibs 任务,以便像升级我的环境之前一样工作。 我正在为 Android Studio 和 Gradle 工具 2.14.1 使用 gradle 插件 2.2.3。

最佳答案

最好的选择是使用configurations.runtimeClasspath,而不是使用configurations.implementation

您还可以考虑: 编译类路径 默认

关于android - 将 Android Studio 的 Gradle 插件升级到 3.0.1 和 Gradle 到 4.1 后无法复制配置依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47910578/

相关文章:

java - 更改两个布局之间的背景颜色?

Android 在收到短信时唤醒并通知

android-studio - Android Studio 3.0 预览缺少 'use the same device for future launch' 复选框

android - 在 Windows 10 家庭版上使用 Android Studio 模拟器

android - 构建签名 APK 时 com.googlecode.libphonenumber :libphonenumber:8. 8.2 中缺少元数据

Android - 努力设置帐户选择器的样式

android - 如果不希望操作被来电、AsyncTask 或服务停止,我应该使用什么?

android-studio - Android Studio 不显示导入建议

Android/Cordova 与 google-play-services 构建依赖关系错误(多个库的包名称为 'com.google.android.gms' )

java - Android注释处理-为不同的构建风格生成不同的代码