I've experimented gradle-kotlin-dsl in an android project. I'm managed to make it work, but I'm stuck in how to define productFlavors
android {
compileSdkVersion(Config.Android.compileSdkVersion)
buildToolsVersion(Config.Android.buildToolsVersion)
defaultConfig {
minSdkVersion(Config.Android.minSdkVersion)
targetSdkVersion(Config.Android.targetSdkVersion)
versionCode = Config.Version.code
versionName = Config.Version.name
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
flavorDimensions("dimension")
productFlavors {
//product flavors here
}
}
After some investigation I've got the solution, just use the create method:
productFlavors {
create("flavor1") {
//flavor configurations here
}
create("flavor2") {
//flavor configurations here
}
}
After some investigation I've got the solution, just use the create method, adding here in case someone need it:
productFlavors {
create("flavor1") {
//flavor configurations here
}
create("flavor2") {
//flavor configurations here
}
}
Related
In the main build.gradle I have
android {
……
defaultConfig {
……
}
buildTypes {
debug {……}
release {……}
}
productFlavors {
lite {
apply from: 'lite_api_config.gradle'
……
}
pro {
apply from: 'pro_api_config.gradle'
……
}
}
}
lite_api_config.gradle
android {
defaultConfig {
buildConfigField('String', 'url', '"https://lite.com/"')
}
buildTypes {
debug {
buildConfigField('String', 'url', '"https://debug.lite.com/"')
}
release {
buildConfigField('String', 'url', '"https://release.lite.com/"')
}
}
}
pro_api_config.gradle
android {
defaultConfig {
buildConfigField('String', 'url', '"https://pro.com/"')
}
buildTypes {
debug {
buildConfigField('String', 'url', '"https://debug.pro.com/"')
}
release {
buildConfigField('String', 'url', '"https://release.pro.com/"')
}
}
}
But when I build this, using a build variant of liteDebug, in the BuildConfig.url I get "https://debug.pro.com/", which is wrong and looks like both the child gradle files are included somehow
Is it even possible to have separate files for different product flavors and include them in the build.gradle based on which flavor is being built.
It is in android gradle build scripts. I have multiple flavors in gradle:
flavorDimensions "brand", "appVariant"
productFlavors {
stage {
dimension "appVariant"
}
prod {
dimension "appVariant"
}
brand1 {
dimension "brand"
}
brand2 {
dimension "brand"
}
}
How can I rewrite that using kotlin-dsl?
This will work:
flavorDimensions("brand", "appVariant")
productFlavors {
create("var1") {
setDimension("appVariant")
}
create("var2") {
setDimension("appVariant")
}
create("brand1") {
setDimension("brand")
}
create("brand2") {
setDimension("brand")
}
}
Can I do that?
I have following setup:
buildTypes {
debug {
// ...
}
release {
// ...
}
productFlavors {
flavorDimensions "buildType", "versionType"
fastBuild {
minSdkVersion 21
dimension "buildType"
}
regular {
minSdkVersion setup.minSdk
dimension "buildType"
}
free {
dimension "versionType"
}
pro {
applicationIdSuffix ".premium"
dimension "versionType"
}
}
This combines to 8 build variants. Actually I only need following 3:
fastBuildDebugPro (my test build)
regularBuildFreeRelease
regularBuildProRelease
Can I somehow exclude the other 5 auto generated build variants?
Like above, my build.gradle file cannot sync because it could not find property "jni" on source sets "main". I'm using gradle-experimental:0.7.0.
I wan't to use Android.mk file in compilation, but i cannot set srcDirs = [].
My build.gradle:
model {
android {
def globalConfiguration = rootProject.extensions.getByName("ext")
compileSdkVersion = globalConfiguration.getAt("androidCompileSdkVersion")
buildToolsVersion = globalConfiguration.getAt("androidBuildToolsVersion")
defaultConfig {
applicationId "com.example.ndk"
minSdkVersion.apiLevel globalConfiguration.getAt("androidMinSdkVersion")
targetSdkVersion.apiLevel globalConfiguration.getAt("androidTargetSdkVersion")
versionCode globalConfiguration.getAt("androidVersionCode")
versionName globalConfiguration.getAt("androidVersionName")
}
buildTypes {
release {
minifyEnabled false
proguardFiles.add(file('proguard-android.txt'))
// signingConfig signingConfigs.release
}
}
sourceSets {
def commonTestDir = 'src/commonTest/java'
test {
java.srcDir commonTestDir
}
androidTest {
java.srcDir commonTestDir
}
main {
jni.srcDirs = []
}
}
}
android.ndk {
moduleName = 'mymodule'
}
}
Take a look into the plugin Experimental Plugin User Guide, according to it, to specify the source directory, you have to do it like so:
model {
android {
...
sources {
main {
jni {
source {
srcDir "src"
}
}
}
}
}
}
It's not the way you did it. Take a look, here is the sources property used, but not the sourceSets
How can I override the resConfigs per build types? I read that flavors would allow that, but I don't use them. I just want for my debug build another set of supported languges.
Here is what I tried:
buildTypes {
debug {
resConfigs "de", "en" // allow also german in debug builds
}
release {
signingConfig signingConfigs.release
resConfigs "en" // english only releases
}
}
Any simple idea how I can achieve that?
For some reason the individual buildType configs doesn't support the resConfigs command as you point out, but defaultConfig does and then you can use this trick to manipulate it per build type even without flavors configured:
android {
defaultConfig {
resConfigs "en"
}
applicationVariants.all { variant ->
if (variant.buildType.name.equals("debug")) {
variant.mergedFlavor.resourceConfigurations.add("de")
}
}
}
The accepted answer did not work for me. de wasn't succesfully added. Doing everything inside the applicationVariants.all { ... } lambda works though:
android {
defaultConfig {
// No resConfigs here!
// resConfigs "en"
}
applicationVariants.all { variant ->
if (variant.buildType.name.equals("debug")) {
variant.mergedFlavor.resourceConfigurations.add("de")
} else {
variant.mergedFlavor.resourceConfigurations.add("en", "de")
}
}
}
or in Kotlinscript:
android {
defaultConfig {
// no resConfigs here!
}
android.applicationVariants.all {
val resConfigs = when {
name.equals("debug") -> listOf("en", "de")
else -> "en"
}
(mergedFlavor as DefaultProductFlavor).addResourceConfigurations(resConfigs)
}
}