How to fix “Program type already present” error when dynamic feature is using an external aar dependency

纵饮孤独 提交于 2020-01-06 06:46:22

问题


We have a 3rd party aar file that due to size considerations we decided to split to a separate dynamic-feature (module). Both main app and dynamic module are using com.google.code.gson:gson when they were in the same module we removed our dependency to gson, but now our main module needs it.

The project build is fine, but when we try to build bundle(s) we get a

"Program type already present: com.google.gson.FieldNamingPolicy$5" error

We tried to exclude gson from the module's gradle: both in the dependencies and android sections but with no luck. this is a 3rd party aar so we do not have any access to its code or dependencies.

Main app gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    mavenCentral()
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 91
        versionName "1.1.91"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            versionNameSuffix "-D"
            matchingFallbacks = ['debug']

        }
        release {
            matchingFallbacks = ['release']
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        pickFirst 'META-INF/license.txt'
        pickFirst 'META-INF/DEPENDENCIES'
    }

    dexOptions {
        javaMaxHeapSize "2g"
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
        targetCompatibility JavaVersion.VERSION_1_8
    }
    flavorDimensions "permissions"
    productFlavors {
    }
    dynamicFeatures = [":dynamic_feature"]
}

dependencies {
    implementation files('libs/gcm.jar')
    //...
    implementation 'com.google.code.gson:gson:2.8.2'
    //...
}

apply plugin: 'com.google.gms.google-services'

dynamic-feature gradle:

apply plugin: 'com.android.dynamic-feature'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }    
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }

    configurations {
        //DOES NOT RESOLVE THE PROBLEM
        all*.exclude group: 'com.google.gson'
        all*.exclude group: 'com.google.code.gson'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':myapp')
    //...
    implementation files('libs/sdk.3rd-party.aar')
    configurations {
        //DOES NOT RESOLVE THE PROBLEM
        all*.exclude group: 'com.google.gson'
        all*.exclude group: 'com.google.code.gson'
    }
}

Error:

org.gradle.execution.MultipleBuildFailures: Build completed with 1 failures. ... Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: Error while generating the main dex list: Error while merging dex archives: Program type already present: com.google.gson.FieldNamingPolicy$5

来源:https://stackoverflow.com/questions/56630355/how-to-fix-program-type-already-present-error-when-dynamic-feature-is-using-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!