Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different dependencies for debug and release in gradle and Android Studio

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?

like image 892
Meteorite Avatar asked Mar 16 '14 16:03

Meteorite


People also ask

What are dependencies in Gradle?

Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path. Publications means the outcomes of the project, such as test class files, build files and war files.

Where are the dependencies in Gradle?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file).

Why there are two Gradle files in Android Studio?

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build. gradle file for build settings specific to that module.

How many types of Gradle build files do we have in Android?

Each module has its own build file, so every Android Studio project contains two kinds of Gradle build files.


2 Answers

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.

like image 61
athor Avatar answered Sep 22 '22 22:09

athor


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.

like image 34
takecare Avatar answered Sep 23 '22 22:09

takecare