dimsuz
dimsuz

Reputation: 9207

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

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.

Upvotes: 11

Views: 1466

Answers (2)

AksLolCoding
AksLolCoding

Reputation: 157

I think this would work:

api project(":module-b")

Upvotes: 0

ahasbini
ahasbini

Reputation: 6901

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.

Upvotes: 1

Related Questions