android import library dependency based on build type and flavor combinations

Viewed 312

I have some external libraries that are different versions to match all my build variants. In this example I have 4 different versions of the same library. I cannot figure out how to do this in my build.gradle file. I have two different flavors that combined with release and debug. In the example below, the commented out code works and the other code is what i'm trying to get working.

  android {
      buildTypes {
          release {
              minifyEnabled true
          }
          debug {
              minifyEnabled false
          }
      }

      flavorDimensions "base"
      productFlavors {
          flavor1 {
              dimension "base"
              applicationIdSuffix ".flavor1"

          }
          flavor2 {
              dimension "base"
              applicationIdSuffix ".flavor2"
          }
      }
  }

  dependencies {
      // this works but cannot specify the build type
      flavor1Implementation files('libs/mylib-release.aar')

      // this combination of buildType+Flavor is not working
      // flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
      // flavor1DebugImplementation   files('libs/mylib-flavor1-debug.aar')

      // this works but cannot specify the build type
      flavor2Implementation files('libs/mylib-flavor2-release.aar')

      // flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
      // flavor2DebugImplementation   files('libs/mylib-flavor2-debug.aar')
  }
1 Answers

Add the following code to your build.gradle file to initialize a placeholder for dependency configurations

// Initializes a placeholder for these dependency configurations
configurations {
    flavor1DebugImplementation {}
    flavor1ReleaseImplementation {}
    flavor2DebugImplementation {}
    flavor2ReleaseImplementation {}
}

Example of the full solution in the build.gradle

android {
    buildTypes {
        release {
            minifyEnabled true
        }
        debug {
            minifyEnabled false
        }
    }

    flavorDimensions "base"
    productFlavors {
        flavor1 {
            dimension "base"
            applicationIdSuffix ".flavor1"

        }
        flavor2 {
            dimension "base"
            applicationIdSuffix ".flavor2"
        }
    }
}


// Initializes a placeholder for these dependency configurations
configurations {
    flavor1DebugImplementation {}
    flavor1ReleaseImplementation {}
    flavor2DebugImplementation {}
    flavor2ReleaseImplementation {}
}


dependencies {
    //flavor1Implementation files('libs/mylib-release.aar')

    flavor1ReleaseImplementation files('libs/mylib-flavor1-release.aar')
    flavor1DebugImplementation   files('libs/mylib-flavor1-debug.aar')

    //flavor2Implementation files('libs/mylib-flavor2-release.aar')
    
    flavor2ReleaseImplementation files('libs/mylib-flavor2-release.aar')
    flavor2DebugImplementation   files('libs/mylib-flavor2-debug.aar')
}
Related