Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a buildType from android app/library module to java only gradle module

Tags:

android

gradle

Gradle Module A is an android module which defines several custom build types

android {
  buildTypes {
    debug {
      buildConfigField 'boolean', 'DEV', 'true'
    }
    internal {
      buildConfigField 'boolean', 'INTERNAL', 'true'
    }
    external {
      buildConfigField 'boolean', 'EXTERNAL', 'true'
    }
    release {
      buildConfigField 'boolean', 'RELEASE', 'true'
    }
  }
}

dependencies {
  implementation project(":module-b")
}

Gradle Module B is a kotlin/java only module:

apply plugin: 'kotlin'
apply plugin: 'com.android.lint'

dependencies {
}

Module A depends on module B:

// Module A build.gradle
dependencies {
  implementation project(":module-b")
}

In Module B sources I need to know how exactly is it consumed by Module A, what is the current buildType of Module A: is it 'debug', 'internal', 'external' or 'release'?

Ideally I'd like to have BuildConfig.java in Module B similar to one provided by an Android Gradle Plugin, but if this is not possible I'd like to have at least some way of figuring out the build type in non-android modules.

EDIT Another example to put this into perspective: let's say there's one 'app' module and 10 java-only modules. When I execute 'app:assembleExternal' task, then in all 10 java-only modules I want to know that it is "external" build type being built.

like image 407
dimsuz Avatar asked May 20 '20 16:05

dimsuz


People also ask

What is a Buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

How do I change a variant in build?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

What is missingDimensionStrategy?

void missingDimensionStrategy ( String dimension, String requestedValue) Specifies a flavor that the plugin should try to use from a given dimension in a dependency. Android plugin 3.0. 0 and higher try to match each variant of your module with the same one from its dependencies.

How would you specify in your build Gradle file?

You can do this by adding extra properties to the ext block in the top-level build.gradle file. // of properties you can define. // You can also create properties to specify versions for dependencies.


1 Answers

One way of doing it is by implementing something that inspects the Gradle taskGraph using gradle.taskGrpah.whenReady inside Module B and checking if :module-a:assembleDebug is part of it, then setting a parameter with value DEBUG which can then be picked up by a Gradle Plugin such as com.github.gmazzo.buildconfig which will then create the BuildConfig.java that gets compiled into the Module B output artifact.

It's possible to do it without using android-library plugin, I honestly find it annoying to implement. The annoying part is that the variant is decided or resolved somewhere that is, in my opinion, not easily reachable by code.

like image 150
ahasbini Avatar answered Nov 12 '22 17:11

ahasbini