Different dependencies for debug and release in gradle and Android Studio

JavaConfigurationDependenciesGradleAndroid Studio

Java Problem Overview


I have an Android project, that depends on pure Java project. Both of them depend on another Java library, also in my multiproject gradle set in Android Studio. I have two versions of that library and want both Android and Java projects to depend on one of them in debug mode, and another - in release.

Is it possible for Android project? For pure Java project? How?

Java Solutions


Solution 1 - Java

Build Types (debug, release, or custom) can have their own dependencies.

To specify a dependency specific to a build type, do the following:

dependencies {
    debugCompile "mydebugdependency"
    releaseCompile "myreleasedependency"
}

If your java project and android project are both using gradle, you can do the above in both of their build.gradle files.

Solution 2 - Java

My buildDebug dependency was also getting ignored. My setup is the app module and a library module, and I have the need to propagate the build type from the app to the library modules, i.e., when I compile the debug type on the app I want to get the library debug type too.

As mentioned, I tried having a specific dependency for each build type on the app gradle file, but to no avail:

buildTypes {
    debug {
        debuggable true
        applicationIdSuffix ".debug"
    
        dependencies {
            debugCompile project(":library")
        }
    }
}

Ultimately what did the trick for me was this: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication

So, now the library dependency is managed (as usual) in the global dependencies scope in the app gradle file:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    releaseCompile project(path: ':library', configuration: 'release')
    debugCompile project(path: ':library', configuration: 'debug')
}

and had to add this to the library's gradle build file:

android {
    publishNonDefault true
}

This publishes all of the dependencies' build types. Note that if it takes a lot of time to compile your dependency, this solution might not be right for you.

Solution 3 - Java

You are able to do this using next pattern

build_variant_name dependency_configurations "dependency"
build_variant_name dependency_configurations project(path: ':libName', configuration: 'build_variant_name of libName')

For example

dependencies {

    FreeDebugImplementation "dependency"
    PaidReleaseApi project(path: ':libName', configuration: 'release')
}

You can read more about build variants - https://developer.android.com/studio/build/build-variants dependency configurations - https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#new_configurations

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMeteoriteView Question on Stackoverflow
Solution 1 - JavaathorView Answer on Stackoverflow
Solution 2 - JavatakecareView Answer on Stackoverflow
Solution 3 - JavayoAlex5View Answer on Stackoverflow