Gradle: add dependency for a specific flavour of the library
In your library you need to tell gradle to build every time every variant:
android {
publishNonDefault true
}
Then in your application, since recently I guess, you can do this:
dependencies {
(...)
devCompile project(path: ':lib', configuration: 'devDebug') // or 'devRelease'
storeCompile project(path: ':lib', configuration: 'storeRelease') // or 'storeDebug'
}
Found in the official documentation under Library Publication.
Edit:
Since version 0.14.3 (2014/11/18), you can now have Flavor-buildType-Compile directive as well:
In your build.gradle before the android {} scope add the following:
configurations {
devDebugCompile
devReleaseCompile
storeDebugCompile
storeReleaseCompile
}
Then you can declare and use different versions of your library per Flavor-BuildType:
dependencies {
(...)
devDebugCompile project(path: ':lib', configuration: 'devDebug')
devReleaseCompile project(path: ':lib', configuration: 'devRelease')
storeDebugCompile project(path: ':lib', configuration: 'storeDebug')
storeReleaseCompile project(path: ':lib', configuration: 'storeRelease')
}
Edit:
Dependency management between modules has changed since Android Gradle Plugin 3.0.0. It automatically tries to matches flavours between your app and the libraries/modules it depends on.
See the documentation for the whole explanation!
Use gradle module as a dependency with specific flavor
You should define missingDimensionStrategy
for both app1
and app2
.
For example:
// app1/build.gradle
defaultConfig {
missingDimensionStrategy 'library', 'flavor1'
}
// app2/build.gradle
defaultConfig {
missingDimensionStrategy 'library', 'flavor2'
}
Android Gradle 3.0.0 - add library dependency for specific flavors
I think you can achieve it by using something like this:
dependencies {
// use only mylib for debug and release
releaseImplementation project(path: ':mylib')
debugImplementation project(path: ':mylib')
// this will be used by all the flavor
implementation "com.android.support:appcompat-v7:27.0.2'
}
Import a specific flavour from a library module
For the future record, I found a solution to my problem by declaring the java.srcDirs
and res.srcDirs
according to the feature state.
I declare a gradle external property declaring if the feature is enabled or not.
project.ext {
hasLottie = true
}
More info on this blog post
And on my library gradle file:
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
publishNonDefault true
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
sourceSets {
main {
if (hasLottieSupport.toBoolean()) {
java.srcDirs += 'lottieYes/java'
} else {
java.srcDirs += 'lottieNo/java'
res.srcDirs += 'lottieNo/res'
}
}
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
if (hasLottieSupport.toBoolean()) {
implementation 'com.airbnb.android:lottie:2.5.6'
}
}
Link dependencies for specific flavors with dimensions
I was going by this section of the Android Studio manual which states that you need to declare explicitly any configurations that for flavor/build type combinations. In this case, that means you need to add the following to your build script before the dependencies {}
block:
configurations {
freeLightweightImplementation
freeHeavyImplementation
premiumLightweightImplementation
premiumHeavyImplementation
}
Related Topics
Programmatically Lock into Portrait Mode for Certain Operations
How to Find The Amount of Free Storage (Disk Space) Left on Android
End Incoming Call Programmatically
Creating Animation on Imageview While Changing Image Resource
Changing Numberpicker Divider Color
How to Create a Persistent Alarmmanager
How to Change Android:Windowsoftinputmode Value from Java Class
Detecting Outgoing Call and Call Hangup Event in Android
Android Get Application's 'Home' Data Directory
Backup/Restore Sqlite Db in Android
How to Set Status Bar Background as Gradient Color or a Drawable in Android
Android Webview Utf-8 Not Showing