11

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.

dimsuz
  • 8,969
  • 8
  • 54
  • 88
  • Is there any reason why the module needs to be kotlin/java only? – Nicolas May 20 '20 at 17:11
  • 1
    There's a few good answers to this on [how to use BuildConfig in an android-library with a flag that gets set in an app](https://stackoverflow.com/questions/21365928), some of them might apply to a java library module. – Nicolas May 20 '20 at 17:28
  • Yeah, it's not a problem with android-library, but I am interested if this could be done in kotlin/java only. A lot of modules do not need android gradle plugin applied to them, this should also considerably speed up the build of a multi-module project. – dimsuz May 20 '20 at 22:19
  • Looks like a chicken or egg problem. Why not create a another gradle project which just contains the buildconfig stuff and have both modules `A` and `B` depend on that one? – smac89 May 22 '20 at 17:56
  • Let's say there's one 'app' module and 10 java-only modules. When I execute 'assembleExternal', then in all 10 java-only modules I want to know that it is "external" build type being built. Your suggestion wouldn't solve this, if I understand it correctly. – dimsuz May 22 '20 at 22:53

2 Answers2

1

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.

ahasbini
  • 6,761
  • 2
  • 29
  • 45
  • Thanks I guess this will do, I didn't know about that buildconfig plugin, it will help here and `whenReady` is a nice way to hook it once. I will try this soon. – dimsuz May 25 '20 at 20:07
0

I think this would work:

api project(":module-b")
AksLolCoding
  • 157
  • 10