Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking a specific build type in a dependency

Suppose I have an Android app with three build types:

buildTypes {
    release {
        ....
    }
    optRelease {
        ....
    }
    debug {
        ....
    }
}

And I have a dependent module:

dependencies {
    implementation project(':myDependency')
}

Suppose this dependency only has two build types (let's say debug and release), and I want full control over which of my app's build types uses which of the dependency's build types. For example, I'd like my app's optRelease to use the library's release, and the app's release the use the library's debug.

That used to be possible before Android Studio 3.0, but the new build variant system doesn't seem to allow for that anymore.

How can I explicitly state which build type to use? Let's say that I have no control over the dependency and cannot modify its gradle configuration.

like image 279
EboMike Avatar asked Jun 09 '18 06:06

EboMike


People also ask

How do I get the current build type in Gradle?

In your build. gradle (:app): tasks. all { Task task -> if (task.name == "preDebugBuild") { doFirst { //for debug build } } else if (task.name == "preReleaseBuild") { doFirst { //for release build } } } dependencies { ... }

What are the different build types?

In the above code, there are three build types i.e. debug, release, and minifiedDebug. The debug and release are the same generated by Android Studio with some additional properties and minifiedDebug is the new build type that is a combination of debug + proguard.

What is matchingFallbacks?

matchingFallbacks. Specifies a sorted list of build types that the plugin should try to use when a direct variant match with a local module dependency is not possible. abstract Int. renderscriptOptimLevel. Optimization level to use by the renderscript compiler.

What is build type user?

Build Type refers to build and packaging settings like signing configuration for a project. For example, debug and release build types. The debug will use android debug certificate for packaging the APK file. While, release build type will use user-defined release certificate for signing and packaging the APK.


2 Answers

You can use matchingFallback

buildTypes {
    release {
       matchingFallbacks = ['debug']
       ... 
    }
    optRelease {
       matchingFallbacks = ['release']  
       ...
    }
    debug {
       matchingFallbacks = ['debug']  
       ...
    }
}

You can also specify list of fallback as follows:

optRelease {
    matchingFallbacks = ['release','debug']  
}

This will specify a sorted list of fallback build types that the plugin should try to use when a dependency does not include a "optRelease" build type. You may specify as many fallbacks as you like, and the plugin selects the first build type that's available in the dependency.

You can refer to official document for more details.

However if you want to use variant-specific configurations when targeting external dependencies. You can do it as follows:

debugImplementation project(':myDependency')

This will make myDependency as a dependency to only the "debug" version of your module.

like image 122
Sagar Avatar answered Oct 21 '22 08:10

Sagar


Thanks a lot ahasbini for your solution. Copying ahasbini's answer from this thread for easy reference to the solution.

You'll need to specify matchingFallback with the Android Gradle Plugin 3.0.0 for the plugin to know which fallback build type of library to use when being compiled with app code in case a certain build type defined in your app is not found in the library.

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        applicationIdSuffix '.debug'
    }
    staging {
        initWith release
        applicationIdSuffix '.staging'
        matchingFallbacks = ['release']
    }
}

More info here: Migrate to Android Plugin for Gradle 3.0.0.

like image 36
Mohit Ajwani Avatar answered Oct 21 '22 07:10

Mohit Ajwani