【问题标题】:Android gradle: Exclude file from external dependenciesAndroid gradle:从外部依赖项中排除文件
【发布时间】:2019-11-11 06:24:27
【问题描述】:

我将com.eftimoff:android-pathview:1.0.8@aar 添加到我的项目中,但是当我运行应用程序时出现此错误:

原因:重复条目:META-INF/MANIFEST.MF

这是外部库中的pathview 文件:

现在我想排除 META-INF 文件夹,所以我将它添加到模块 gradle 中:

android {
...
    packagingOptions {
        exclude 'META-INF/*'
    }
}

但我仍然遇到上述错误。

我写了像this suggestion这样的gradle脚本

当我以这种方式添加库时: implementation ('com.eftimoff:android-pathview:1.0.8@aar').exclude("META-INF/MANIFEST.MF")

我收到了这个错误:

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\Projects\Android\Mvvm\Kotlin\MovieDb\app\build.gradle' line: 80

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find method leftShift() for arguments [build_37vlhf9la1wtl8koroxp1kll7$_run_closure4@b32cc6a] on task ':app:excludeTask' of type org.gradle.api.DefaultTask.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

CONFIGURE FAILED in 0s
Could not find method leftShift() for arguments [build_37vlhf9la1wtl8koroxp1kll7$_run_closure4@b32cc6a] on task ':app:excludeTask' of type org.gradle.api.DefaultTask.
Open File

这是tasks.create("excludeTask") << { 我出错的地方。 所以我把task.create()改成了这个:

tasks.create("excludeTask")  {
    doLast{
        exclusions.each {
            File file = file("${buildDir}/intermediates/exploded-aar/${it}")
            println("Excluding file " + file)
            if (file.exists()) {
                file.delete()
            }
        }
    }
}

这些错误消失了,但我仍然再次收到此错误:

原因:重复条目:META-INF/MANIFEST.MF

这个脚本似乎在 resources 上工作,现在我想排除文件夹中的一个文件。

这是完整的 gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {

    // Enables data binding.
    dataBinding {
        enabled = true
    }

    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.t.moviedb"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

//    packagingOptions {
//        exclude '/META-INF/*'
//    }
//    aaptOptions {
//        ignoreAssetsPattern "!META-INF/MANIFEST.MF"
//        ignoreAssetsPattern "META-INF/MANIFEST.MF"
//    }
}


final List<String> exclusions = [];

Dependency.metaClass.exclude = { String[] currentExclusions ->
    currentExclusions.each {
        exclusions.add("${getGroup()}/${getName()}/${getVersion()}/${it}")
    }
    return thisObject
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Support libraries
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.fragment:fragment:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    // Android KTX
    implementation 'androidx.core:core-ktx:1.1.0'

    // Testing
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    //other
    implementation ('com.eftimoff:android-pathview:1.0.8@aar').exclude("META-INF/*")
}

tasks.create("excludeTask")  {
    doLast{
        exclusions.each {
            File file = file("${buildDir}/intermediates/exploded-aar/${it}")
            println("Excluding file " + file)
            if (file.exists()) {
                file.delete()
            }
        }
    }
}

tasks.whenTaskAdded({
    if (it.name.matches(/^process.*Resources$/)) {
        it.dependsOn excludeTask
    }
}) 

这是我的 gradle 版本:

\MovieDb>gradlew --version

------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time:   2019-04-26 08:14:42 UTC
Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_231 (Oracle Corporation 25.231-b11)
OS:           Windows 10 10.0 amd64

和:

【问题讨论】:

  • 这不行吗? packagingOptions { exclude 'META-INF/MANIFEST.MF' } 注意路径的开头没有前导 /
  • 我使用了exclude 'META-INF/MANIFEST.MF,但我得到了Cause: duplicate entry: META-INF/MANIFEST.MF 错误@DanielZolnai
  • 您使用的是 Gradle Android 插件 3.5.2 吗?如果是,您可以尝试降级到 3.5.1 吗? (不是 Studio,只是插件版本)
  • 是的,我正在使用 gradle 3.5.2 classpath 'com.android.tools.build:gradle:3.5.2' 并且还更新了我的帖子@DanielZolnai
  • 从您的 libs/ 中删除 android-pathview-1.0.8.aar 并重试。

标签: java android kotlin


【解决方案1】:

您的问题是由多个问题引起的,因此更难修复。 首先,Gradle Android 插件版本 3.5.2 似乎有一个错误,您无法从 apk 中删除这些额外的清单文件。 作为一种解决方法,您可以将插件恢复到版本3.5.1(不是Android Studio,恢复插件版本就足够了)。

然后你看到了错误:

原因:重复条目:META-INF/MANIFEST.MF

这是因为您暂时删除了该部分

packagingOptions {
    exclude 'META-INF/MANIFEST.MF'
}

来自您的 build.gradle,因为插件错误导致它无法正常工作。 最后,你看到了错误:

在模块 androidsvg-1.2.1.jar (android-pathview-1.0.8.aar) 和 androidsvg-1.2.1.jar (com.eftimoff:android-pathview: 1.0.8)

这是因为您在 libs/ 文件夹中包含了一个修改后的库,但忘记将其从您的 implementation {} 部分中删除,所以它被包含了两次。

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多