Loop all SourceSets - Groovy -> Kotlin DSL - android

How to loop all source-sets in the kotlin DSL? The below groovy code loops all sourceSets including 'multiplicative' like androidTestFoobarProdDebug
flavorDimensions("brand", "releaseType")
productFlavors {
create("prod") {
dimension = "releaseType"
}
create("foobar") {
dimension = "brand"
}
}
sourceSets.all { com.android.build.api.dsl.AndroidSourceSet sourceSet ->
// Also prints foorbarProd
println("0 "+sourceSet.name)
}
sourceSets.all {
println("1 "+it.name)
}
but this kotlin-code does not loop the multiplicative concatenated flavor-dimensions like foobarProdDebug
sourceSets.all { sourceSet ->
println(sourceSet.name) // does not print foobarProd
true // seems its a predicate in kotlin
}

People at Google were so kind to help me out here:
Sadly this seems like the mixing of kotlin's all extension method on collection (which is not lazy) and gradle's DomainObjectContainer.all (which is lazy).
If you change
sourceSets.all { sourceSet -> //: com.android.build.api.dsl.AndroidSourceSet ->
println("sourceSet.name = \"src/${name}/svg\"")
true // seems its a predicate in kotlin
}
to
sourceSets.all {// this: com.android.build.api.dsl.AndroidSourceSet ->
println("sourceSet.name = \"src/${name}/svg\"")
// Not a kotlin.sequences.all call anymore so nothing to return
}
it works.
Here's the full kotlin solution:
flavorDimensions("brand", "releaseType")
productFlavors {
create("prod") {
dimension = "releaseType"
}
create("foobar") {
dimension = "brand"
}
}
sourceSets.all {
println(name)
}

Related

gradle kotlin dsl: how to create a shared function which uses a plugin class?

A simplified child module build.gradle.kts:
plugins {
id("com.android.library")
kotlin("android")
}
android {
androidComponents.beforeVariants { it: com.android.build.api.variant.LibraryVariantBuilder ->
it.enabled = run {
// logic to calculate if
it.productFlavors[0].second == "flavor" && it.buildType == "debug"
}
}
}
Is it possible to extract function for calculation of enabled state of buildVariant?
fun calculateIsEnabled(lvb: com.android.build.api.variant.LibraryVariantBuilder): Boolean {
return lvb.productFlavors[0].second == "flavor" && lvb.buildType == "debug"
}
I tried to declare the function in the root build.gradle.kts but I don't know how to access it from submodule and if it is possible at all
I tried to declare it in buildSrc module, but com.android.build.api.variant.LibraryVariantBuilder is undefined here because the plugin com.android.library is not present here and I think it is not allowed and/or meaningless
So, the question is: where to declare a shared function that uses types defined in a gradle plugin and need to be accessible in all submodules of type android library?
After several tries I solved it:
buildSrc/build.gradle.kts
repositories {
google()
mavenCentral()
}
plugins {
`kotlin-dsl`
}
dependencies {
// important: dependency only in simple string format!
implementation("com.android.tools.build:gradle:7.2.0-alpha03")
}
buildSrc/src/main/kotlin/Flavors.kt
import com.android.build.api.variant.LibraryVariantBuilder
import com.android.build.api.variant.ApplicationVariantBuilder
private fun isFlavorEnabled(flavor1: String, buildType: String): Boolean {
return flavor1 == "flavor" && buildType == "debug"
}
fun isFlavorEnabled(lvb: LibraryVariantBuilder): Boolean {
// productFlavors are pairs of flavorType(dimension) - flavorName(selectedFlavor)
return lvb.run { isFlavorEnabled(productFlavors[0].second, buildType ?: "") }
}
fun isFlavorEnabled(avb: ApplicationVariantBuilder): Boolean {
return avb.run { isFlavorEnabled(productFlavors[0].second, buildType ?: "") }
}
In library/build.gradle.kts and app/build.gradle.kts
android {
androidComponents.beforeVariants {
it.enabled = isFlavorEnabled(it)
}
}

How to get Current Build Type in Gradle

My Question is very direct and easy to understand.
Question
In Gradle, is there any way I can get the current build type at runtime. For example, when running an assembleDebug task, can tasks within the build.gradle file make decisions based on the fact that this task is related to the debug build variant?
Sample Code
apply plugin: 'com.android.library'
ext.buildInProgress = ""
buildscript {
repositories {
maven {
url = url_here
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
configurations {
//get current build in progress here e.g buildInProgress = this.getBuildType()
}
android {
//Android build settings here
}
buildTypes {
release {
//release type details here
}
debug {
//debug type details here
}
anotherBuildType{
//another build type details here
}
}
}
dependencies {
//dependency list here
}
repositories{
maven(url=url2_here)
}
task myTask{
if(buildInProgress=='release'){
//do something this way
}
else if(buildInProgress=='debug'){
//do something this way
}
else if(buildInProgress=='anotherBuildType'){
//do it another way
}
}
In Summary
Is there a way for me to get exactly the build type in progress within myTask{}?
You can get the exact build type by parsing your applicationVariants:
applicationVariants.all { variant ->
buildType = variant.buildType.name // sets the current build type
}
A implementation could look like the following:
def buildType // Your variable
android {
applicationVariants.all { variant ->
buildType = variant.buildType.name // Sets the current build type
}
}
task myTask{
// Compare buildType here
}
Also you can check this and this similar answers.
Update
This answer by this question helped the questioner to settle the problem.
This worked for me
applicationVariants.all { variant ->
def variantType = variant.buildType.name
println "Variant type: $variantType"
if (variantType == "debug") {
// do stuff
}
}
You should getBuildConfigFields().get("MY_BUILD_TYPE").getValue())
https://stackoverflow.com/a/59994937/5279996
GL
If you want to suffix the buildtype name to the versionname (like me) just add this line to the version name:
debug {
versionNameSuffix "-debug"
}
This way you can identify the build type in the build name. And it works without declaring anything else.
Correct way for getting the current buildType being used during build in Kotlin programming language for android platform (logic is the same for Java)
project.afterEvaluate {
this.android().variants().all {
this.assembleProvider.configure {
this.doLast{
val variant = this#all
variant.outputs
.map
.forEach{
//do something with current buildType, or build flavor or whatever
println(variant.flavorName)
println(variant.buildType)
}
}
}
}
}
I'm getting build type in this way:
BuildConfig.BUILD_TYPE
If you need to check what is the current build type, create an enum class in your utils package and use it in your if statement:
enum class Environment(val value: String) {
RELEASE("release"),
LOCAL("local"),
STAGING("staging"),
DEBUG("debug")
}
Your if/when statement:
if (BuildConfig.BUILD_TYPE == Environment.RELEASE.value) {
//TODO
} else if(...)
or through when:
when(BuildConfig.BUILD_TYPE) {
Environment.RELEASE.value -> { //TODO }
Environment.LOCAL.value -> { // TODO }
// etc.
}
I checked other answers, nothing works.
What's below will help.
In your build.gradle (:app):
tasks.all { Task task ->
if (task.name == "preDebugBuild") {
doFirst {
//for debug build
}
} else if (task.name == "preReleaseBuild") {
doFirst {
//for release build
}
}
}
dependencies {
...
}
Be aware, the code that you put inside will not be executed when you change the build variant, but when you build app.
Try like this in your gradle : It works fine for me
//get current build all params as string
def buildParams = getGradle().getStartParameter().toString().toLowerCase();
applicationVariants.all { variant ->
variant.outputs.all {
def typename = variant.buildType.name.toLowerCase();
//and check build state in all variants
if(buildParams.contains(typename)) {
// finally, you get what you want.
}
}
}
You can get the exact build type by parsing your applicationVariants:
applicationVariants.all { variant ->
buildType = variant.buildType.name // sets the current build type
}
Here's the approach I used to detect the runtime build type without declaring any variables at build time.
def isCurrentBuildType(buildType) {
return gradle.getStartParameter().taskNames.find({ it.endsWith(buildType) }) != null
}
print(isCurrentBuildType("Release")) //prints true if the current build type is release.
Note that the first letter should be capital (e.g. assembleRelease, bundleRelease)

Avoid code duplication - How to create functions (and call them) in gradle?

I've been looking for a few minutes over the internet on how to create functions and call them inside build.gradle without success. Since I found nothing I'm not sure if I'm searching for the right concept-keywords or if that's even possible.
I have two buildTypes:
release {
}
debug {
}
And I woud like to call this snippet() below inside both of them without duplicating it, or in other words, to create a function:
def propsFile = rootProject.file('properties')
def M_PROP = "mProp"
if (propsFile.exists()) {
//Math
}
Generating something like:
buildTypes {
release {
snippet()
}
}
debug {
snippet()
}
}
is that possible and how am I able to do this?
Perhaps you want
buildTypes {
[release, debug].each { buildType ->
if (foo) {
buildType.doStuff()
}
}
}
Or maybe
ext.snippet = { buildType ->
if (foo) {
buildType.doStuff()
}
}
buildTypes {
snippet(release)
snippet(debug)
}
Note: There's also the with { ... } method in groovy so
buildType.doStuff1()
buildType.doStuff2()
buildType.doStuff3()
can be written as
buildType.with {
doStuff1()
doStuff2()
doStuff3()
}

How to filter and execute specific Gradle tasks?

My Android project has different product flavors configured:
productFlavors {
devtracking {
dimension trackingFlavorDimension
}
livetracking {
dimension trackingFlavorDimension
}
stagingwebservice {
dimension webserviceFlavorDimension
}
livewebservice {
dimension webserviceFlavorDimension
}
playstore {
dimension brandDimension
}
fdroid {
dimension brandDimension
}
}
How can I write a task which only builds release build for the devtracking flavor? The "filter" is ready - how can I actually execute the filtered tasks? Note, that right now the "filter" is already executed at configuration time.
task assembleDevtrackingRelease {
description "Assembles all Devtracking release builds."
group = "Build"
android.applicationVariants.all { variant ->
if (variant.name.contains("DevtrackingRelease")) {
println(variant.name)
// TODO Execute this variant
}
}
}
With great help of Selvin and mirceanis I finally come up with this solution:
// Creates a parent task for a set of sub tasks.
// Sub tasks are collected based on the given task name part.
def createVariantBatchTask(taskNamePart, taskPrefix = "assemble", taskGroup = "Build") {
String taskName = "${taskPrefix}${taskNamePart.capitalize()}"
Task task = tasks.create(taskName)
task.description "${taskPrefix.capitalize()}s all $taskName builds."
task.group taskGroup
android.applicationVariants.all { variant ->
if (variant.name.toLowerCase().contains(taskNamePart.toLowerCase())) {
Task variantTask = tasks.findByName("${taskPrefix}${variant.name.capitalize()}")
if (variantTask != null) {
task.dependsOn variantTask
}
}
}
}
createVariantBatchTask("devtrackingRelease")
createVariantBatchTask("devtrackingDebug")
Possible improvement: Move into a custom task class in buildSrc.
task installDevtrackingReleaseFlavors() {
android.applicationVariants.all { v ->
if (v.name.endsWith('DevtrackingRelease')) {
String name = v.name.capitalize()
dependsOn "install$name"
}
}
}

Gradle dynamic flavor

I would like to create dynamic flavors from the directory tree.
It works great!
But Android Studio uses gradle in its tmp file like:
/home/svirch_n/.IntelliJIdea14/system/compile-server
and my script doesn't work anymore because it uses relative paths like this:
Closure getFlavors = { rootDir, basePackage ->
def result = [:]
new File("$rootDir").eachDir() { dir ->
def name = dir.getName()
if ("$name" != "main")
result.put("$name", "$basePackage.$name")
}
return result
}
// This is an ugly closure.
// If I can get rid of this, my problem will be solved
Closure getSrcPath = {
if (System.getProperty("user.dir").split("/").last() == "app") {
return "src"
} else {
return "app/src"
}
}
android {
...
def myFlavors = getFlavors(getSrcPath(), "com.example.app")
productFlavors {
myFlavors.each { flavorName, flavorPackage ->
"$flavorName" {
applicationId "$flavorPackage"
}
}
}
}
Do you have an idea how to solve this?
Thanks in advance for your help
P.S: I want dynamic flavors cause my git project has public and private repositories and not everyone can have all the flavors but I want them to compile anyway.
Assuming I am in the subproject 'app', I can use:
project(":app").getProjectDir().getPath()

Categories

Resources