title: How to fix "Program type already present" error when dynamic feature is using an external aar dependency
body: <p>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 <code>com.google.code.gson:gson</code>
  when they were in the same module we removed our dependency to <code>gson</code>, but now our main module needs it. </p>
  
  <p>The project build is fine, but when we try to build bundle(s) we get a </p>
  
  <blockquote>
    <p>"Program type already present: com.google.gson.FieldNamingPolicy$5"
    error</p>
  </blockquote>
  
  <p>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. </p>
  
  <p>Main app gradle:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>dynamic-feature gradle:</p>
  
  <pre><code>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'
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>