web-dev-qa-db-ja.com

Android Studioを3.0.1に、Gradleを4.1にアップグレードした後、構成の依存関係をコピーできません

この単純なgradleタスクを使用して、「コンパイル」依存関係を特定のフォルダーにコピーしていました。

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

しかし、Android Project for gradle plugin 3.0.1 for Android Studio and Gradle tool to 4.1。をアップグレードした直後に動作しなくなりました。 https://developer.Android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations で廃止されました。これを「実装」に変更しました。ただし、 Gradleビルドエラー出力に従って、構成 '実装'を直接解決することは許可されていないため、copyLibsタスクを使用できません。

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

次のアプリモジュールの現在の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を使用していました。

22
Rafael

configuration.implementationを使用する代わりに、以下を使用するのが最適なオプションです。configurations.runtimeClasspath

次のことも考えられます:compileClasspath default

15

これはおそらく解決しないか、解決するためのより良い方法はありませんが...

次のようにして、コピーできるように依存関係を配置できます。

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/"
}

これは、compileからimplementationに変更したため、jarファイルへの依存関係の配置を停止するJarタスクがある場合にも役立ちます。

次を使用できます。

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

の代わりに:

from {configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }}
7
ViníciusPJ

構成を解決できるようにするので、依存関係のファイルを取得する例外はありません

configurations.implementation.setCanBeResolved(true)
configurations.api.setCanBeResolved(true)

println configurations.implementation.resolve()
println configurations.api.resolve()
6
act262

別の提案。

カスタム構成を作成し、configurations.customConfig

configurations {
  customConfig.extendsFrom implementation
}
5
mortalis

コメントをしたかったのですが、そうするために必要な評判がまだありません(1ポイント!!>。<)。 The Gradle ticket で言及されているimplementation依存関係とcompileClasspathのリストを取得する方法があるかのようには見えません。 'Androidで直接作業しています。私の場合のように、依存関係をエクスポートして、 nity3D がリリース用にパッケージ化できるようにします。

そのため、この場合の唯一の解決策は、非推奨のcompile型を使用することです。

5
Cornelia Xaos

Gradle 5.5から5.6にアップグレードした後にこのエラーが発生し始めました。intelliJでプロジェクトを同期しようとすると発生します。

このおかげで answer 別の質問で、 ideaplugin をすべてのプロジェクトに適用してからgradle cleanIdeaそしてその後、すべてが再び機能し始めました。

別の日、問題の別の#不可解な解決策。

0
smac89