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

时间:2023-02-01
本文介绍了将 Android Studio 的 Gradle 插件升级到 3.0.1 和 Gradle 到 4.1 后无法复制配置依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

task copyLibs(type: Copy) {来自configurations.compile进入$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 清理构建FAILURE:构建失败并出现异常.* 什么地方出了错:无法确定任务 ':app:copyLibs' 的依赖关系.>不允许直接解析配置实施"* 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪.使用 --info 或 --debug 选项运行以获得更多日志输出.* 在 https://help.gradle.org 获得更多帮助1 秒内构建失败

请参阅我当前的 app 模块的 build.gradle 文件:apply plugin: 'com.android.application'

<代码>android {compileSdkVersion 26默认配置 {applicationId "newgradle.com.testingnewgradle"minSdkVersion 21targetSdkVersion 26版本代码 1版本名称1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}构建类型 {发布 {缩小启用假proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}依赖{实现文件树(目录:'libs',包括:['*.jar'])实施 'com.android.support:appcompat-v7:26.1.0'实施 'com.android.support:support-v4:26.1.0'实施 'com.android.support:design:26.1.0'实施 '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'}任务copyLibs(类型:复制){来自configurations.implementation进入$project.rootDir/reports/libs/"}build.dependsOn 复制库

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

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

解决方案

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

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

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.

解决方案

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

You can also think about: compileClasspath default

这篇关于将 Android Studio 的 Gradle 插件升级到 3.0.1 和 Gradle 到 4.1 后无法复制配置依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Android Gradle 找不到符号类 Gson 下一篇:为每个 Build Variant 使用不同的 manifestPlaceholder

相关文章

最新文章