首页 文章

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

提问于
浏览
8

我曾经使用这个简单的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 .

4 回答

  • 5

    另一个建议 .

    我创建了自定义配置,然后将其用作 configurations.customConfig

    configurations {
      customConfig.extendsFrom implementation
    }
    
  • 2

    这可能无济于事或有更好的解决方法,但......

    您可以按照可执行以下操作的方式放置依赖项:

    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文件,因为您从 compile 更改为 implementation ,这也会有所帮助 .

    您可以使用:

    from {configurations.myConfig.collect { it.isDirectory() ? it : zipTree(it) }}
    

    代替:

    from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
    
  • 4

    我想发表评论,但我看起来好像有办法获得 implementation 依赖关系列表和the Gradle ticket提到的 compileClasspath Rafael发布了赢得't work if you'直接与Android合作,就像我需要依赖关系的情况一样导出,以便Unity3D可以将它们打包发布 .

    所以..在这种情况下,看起来唯一的解决方案是使用已弃用的 compile 类型 .

  • 2

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

    你也可以考虑:compileClasspath默认

相关问题