Cyrus the Great
Cyrus the Great

Reputation: 5942

Android gradle: Exclude file from external dependencies

I added com.eftimoff:android-pathview:1.0.8@aar to my project but when i run app i got this error:

Cause: duplicate entry: META-INF/MANIFEST.MF

This is pathview file in external libraries :

enter image description here

Now i want to exclude META-INF folder so I added this in to module gradle :

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

But i still got above error.

I wrote gradle script like this suggestion

When i added library like this way : implementation ('com.eftimoff:android-pathview:1.0.8@aar').exclude("META-INF/MANIFEST.MF")

I got this error :

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

This is where tasks.create("excludeTask") << { i got error. So i changed task.create() to this:

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

And those errors gone but still i got this error again:

Cause: duplicate entry: META-INF/MANIFEST.MF

This script seems work on resources and now i want to exclude a file inside a folder.

This is complete 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
    }
}) 

This is my gradle version:

\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

and :

enter image description here

Upvotes: 2

Views: 4487

Answers (1)

Daniel Zolnai
Daniel Zolnai

Reputation: 16920

Your issue was caused by multiple problems, that's why it was harder to fix. First of all, it seems that the Gradle Android plugin version 3.5.2 has an error where you are unable to strip these extra manifest files from the apk. As a workaround, you can revert the plugin to version 3.5.1 (not Android Studio, reverting the plugin version is enough).

Then you saw the error:

Cause: duplicate entry: META-INF/MANIFEST.MF

This was because you have temporarily removed the part

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

From you build.gradle, because it did not work because of the plugin bug. Finally, you saw the error:

Duplicate class com.caverock.androidsvg.CSSParser found in modules androidsvg-1.2.1.jar (android-pathview-1.0.8.aar) and androidsvg-1.2.1.jar (com.eftimoff:android-pathview:1.0.8)

This was because you have included a modified library in your libs/ folder, but forgot to remove it from your implementation {} section, so it was included two times.

Upvotes: 2

Related Questions