Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将Android Studio的Gradle插件升级到3.0.1并将Gradle升级到4.1后,无法复制配置依赖项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我曾经使用这个简单的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’现在已被@L_618_5@弃用,我将其更改为’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) }}

大佬总结

以上是大佬教程为你收集整理的将Android Studio的Gradle插件升级到3.0.1并将Gradle升级到4.1后,无法复制配置依赖项全部内容,希望文章能够帮你解决将Android Studio的Gradle插件升级到3.0.1并将Gradle升级到4.1后,无法复制配置依赖项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。