j3g
j3g

Reputation: 426

android import library dependency based on build type and flavor combinations

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')
  }

Upvotes: 4

Views: 1806

Answers (2)

Mahdi Zareei
Mahdi Zareei

Reputation: 2028

This way will work in multi-module projects

create a build gradle file (for example flavor_config.gradle) and define your config in it like this

android {
    flavorDimensions 'resource_type'
    productFlavors {
        create("flavor1") {
            dimension 'resource_type'
            versionName "$app_version_name - flavor1"
        }
        create("flavor2") {
            dimension 'resource_type'
            versionName "$app_version_name - flavor2"
        }
    }
}

and apply this gradle file for every module that you want for example app module or a feature module like this:

apply from: rootProject.file("flavor_config.gradle")

after sync project you can access to specific of implementation of each flavor like this:

flavor1Implementation("flavor1Library")
flavor2Implementation("flavor2Library")

Upvotes: 0

j3g
j3g

Reputation: 426

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')
}

Upvotes: 3

Related Questions