Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Gradle - set module build variant

I am setting up project that has a dependancy to one module, and I could successfully make APK file. All I did was adding

compile project(':ModuleName')

However I am wondering if I can have module dependancy with build variance. So, [Project:debug] depends on [Module:debug], and [Project:release] depends on [Module:release]

Thanks!

like image 564
SuperB Avatar asked Nov 25 '14 18:11

SuperB


People also ask

Where is build variants in Android Studio?

To change the build variant Android Studio uses, select Build > Select Build Variant in the menu bar. For projects without native/C++ code, the Build Variants panel has two columns: Module and Active Build Variant.

How do I change my Buildtoolsversion?

To download it, go to Tools > Android > SDK Manager, then click on the "SDK Tools" tab at the top of the new window. Then select the "Show Package Details" checkbox in the lower right-hand corner.

What is variant in gradle?

1. What Are Build Variants? Build variants are specific builds that you can produce from Gradle, based around shared core source code. While a standard app may have a debug and release build type, you can expand on this by adding flavor dimensions.

What is a Gradle build variant?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

How do I create and configure build types in Android Studio?

You can create and configure build types in the module-level build.gradle file inside the android block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true.

What does the Android plugin for Gradle do?

The Android plugin for Gradle provides a useful Gradle task that shows you how to organize your files for each of your build types, product flavors, and build variants. For example, the following sample from the task output describes where Gradle expects to find certain files for the "debug" build type:

Can I create a new source set in Gradle?

However, you can create new source sets to control exactly what files Gradle compiles and packages for specific build types, product flavors (and combinations of product flavors when using flavor dimensions ), and build variants.


2 Answers

An update over Marcus answer:

I don't know since what version of Gradle /Android Studio, but now it's possible to do:

Update: publishNonDefault is now deprecated and not needed anymore. Just use the config further below.

###Library's build.gradle:

android {
    ...
    publishNonDefault true
}

App's build.gradle:

dependencies {
    debugCompile project(path: ':baseApp', configuration: 'debug')
    releaseCompile project(path: ':baseApp', configuration: 'release')
}

Changing the build variant of one of them in the Android Studio will change the build variant of the other.

like image 163
Diolor Avatar answered Nov 07 '22 10:11

Diolor


At the moment the Gradle toolchain only builds libraries in the release variant by default, regardless of what you choose as your application build type. There are some suggested work arounds to that problem but they are mostly involved in your build configuration rather than anything with the dependency include.

The closest example I can think that is to what you want is to do the following;

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}

But this is achieved through build flavours rather than build variants.

like image 26
marcus.ramsden Avatar answered Nov 07 '22 10:11

marcus.ramsden