I have an android application and I would like to have different flavors. Specifically I would like to have 2 flavors and for each flavor use different strings (different strings.xml
file) and maybe have some icons different.
I have tried creating two folders in the project's root folder: flav1
and flav2
and used the following build.gradle
android {
compileSdkVersion "Google Inc.:Google APIs:15"
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
productFlavors {
flav1 {
packageName "com.ic.flav1"
}
flav2 {
packageName "com.ic.flav2"
}
}
android.sourceSets.flav2 {
res {
srcDir 'flav2'
}
resources {
srcDir 'flav2'
}
}
android.sourceSets.flav1 {
res {
srcDir 'flav1'
}
resources {
srcDir 'flav1'
}
}
}
The result of this is that none of the strings is recognized, getting multiple errors of the following type:
build FAILED :
error: Error: No resource found that matches the given name (at 'contentDescription' with value '@string/txt_addr').
Am I missing something? How should the build.gradle be?
Thanks
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. Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.
Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build. gradle which you'll learn shortly.
When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.
There is no direct way to get the current flavor; the call getGradle().
The solution was to add directory
values
under each corresponding res folder, and all the strings were recognized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With