Android Gradle:从外部依赖项中排除文件

时间:2019-11-11 06:24:27

标签: java android kotlin

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

  

原因:重复输入:META-INF / MANIFEST.MF

这是外部库中的pathview文件:

enter image description here

现在我想排除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上运行,现在我要排除文件夹中的文件。

这是完整的方法:

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

和:

enter image description here

1 个答案:

答案 0 :(得分: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)中找到重复的com.caverock.androidsvg.CSSParser类: 1.0.8)

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