Different dependencies for debug and release in gradle and Android Studio
Asked Answered
S

3

64

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?

Shearer answered 16/3, 2014 at 16:34 Comment(0)
S
69

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.

Schematic answered 16/3, 2014 at 20:41 Comment(13)
I used releaseCompile project(':myLib:releaseVersion') and debugCompile project(':myLib:debugVersion'), but Android Studio says: Build script error, unsupported Gradle DSL method found: 'debugCompile()'! Possible causes could be: - you are using Gradle version where the method is absent - you didn't apply Gradle plugin which provides the method - or there is a mistake in a build scriptShearer
Android project seems to work well, but pure Java project generates an error, stated above.Shearer
what gradle plugin (if any) are you applying to the java project?Schematic
I apply only java plugin: apply plugin: 'java'Shearer
You could try specifying your dependencies inside of the build type: buildTypes { debug { dependencies { debugCompile "dependency" } }Schematic
Unfortunately, as far as I can judge from here, java plugin doesn't support build types at all. Only android and android-library plugins do support them. I'm very sad of it. Unfortunaely, I can't convert my pure java project to an android library, because another not android project depends on it (also in my multiproject set under gradle). The only workaround, I have found, is to create another android-library project referencing the same source with pure java project: java.srcDirs = ['../src']Shearer
Of course, workaround, proposed above is rather unconvinient, and leads to large build scripts, like I used to have in ant (that was one of the major reasons, why I decided to move to gradle). The fact, that 'java' plugin doesn't support build types (i.e. it has only one default build type) is a pitty... I think it worth creating a feature request for gradle team. Or, may be, some more cofmortable workaround exists?Shearer
If I specify a new buildType, (i.e. beta), I can't do betaCompile "mybetadependency", is there a way to add this? In debug I want to use the debug one and in beta and release use the release one. Is that possible?Moralez
has anyone gotten this to work? My debugCompile is plainly ignored it seems.Cerated
How about if I also have flavors? I want to different dependencies such as flavor1Debug flavor2Debug flavor1Release flavor2ReleaseArdeb
I get an error: Could not find method debugCompile() for arguments [project ':jni-code']. jni-code is the name of my library project.Seamy
does this work with android library dependencies as well?Scrambler
debugCompile is deprecated and should now be replaced with debugImplementationCharacharabanc
T
38

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.

Tannie answered 13/3, 2015 at 11:46 Comment(0)
H
7

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

Haustellum answered 13/6, 2018 at 10:22 Comment(1)
In order to specify these Flavor+BuildType implementations, you also have to add a configurations block to your gradle file. For example: configurations { freeDebugImplementation {} } As illustrated here: developer.android.com/studio/build/dependenciesBombay

© 2022 - 2024 — McMap. All rights reserved.