Android, Gradle and duplicate files error during p

2020-07-22 19:54发布

问题:

I want to use the json-schema-validator in my Android project like this:

dependencies { compile 'com.github.fge:json-schema-validator:2.1.8' }

Unfortunately Gradle stops packaging due to this file duplicate error:

Path in archive: draftv3/schema Origin 1: /Users/andrej/.gradle/caches/modules-2/files-2.1/com.github.fge/json-schema-validator/2.1.8/4c2a5be8ce86c2338561a651d7d22fb4c4a8763d/json-schema-validator-2.1.8.jar Origin 2: /Users/andrej/.gradle/caches/modules-2/files-2.1/com.github.fge/json-schema-core/1.1.9/4ead9ba3fb3bde69d93f738042d12a9e60e41645/json-schema-core-1.1.9.jar

I know I can ignore the file like this:

packagingOptions { exclude 'draftv3/schema' }

But the file is used by json-schema-validator and json-validator-core, so it is required in the resulting APK.

How can I force Gradle to proceed packaging while keeping one of the two versions of this file (they are equal)?

Thanks, Andrej

回答1:

For others, here a quick workaround until Gradle will add packaging options with proper duplicate strategies:

android.applicationVariants.all { variant-> variant.assemble.doFirst { exec { executable "sh" args "-c", "find ~/.gradle/caches/ -iname 'json-schema-validator*.jar' -exec zip -d '{}' 'draftv3/schema' \\;" args "-c", "find ~/.gradle/caches/ -iname 'json-schema-validator*.jar' -exec zip -d '{}' 'draftv4/schema' \\;" } } }



回答2:

By explicitly mentioning the draftv/schema files in the build.graddle file we can resolve this issue.

    android {

    ...

        packagingOptions {

            ...

            pickFirst 'draftv3/schema'
            pickFirst 'draftv4/schema'

        }
    }


回答3:

Try the following to only exclude draftv3/schema from your dependency:

dependencies {
   compile('com.github.fge:json-schema-validator:2.1.8') {
       exclude 'draftv3/schema'
   }
}