What is the equivalent of Android's BuildConfig in a Java library module?
Asked Answered
C

3

18

I am using Android Studio and I have the following modules in my project:

  • Android application named "app"
  • Java library named "communication"

In my "app" module's build.gradle file, I can declare variables for the BuildConfig.java file, as follows:

buildTypes {
    release {
        ...
        buildConfigField "String", "SERVER_URL", '"my_url"'
    }
    debug {
        buildConfigField "String", "SERVER_URL", '"my_url"'
    }
}

I can then access these variables using BuildConfig.SERVER_URL.

How do I achieve this in my "communication" module's build.gradle file?

Cristoforo answered 4/6, 2016 at 20:18 Comment(2)
To be clear, are you trying to access BuildConfig values in java library code while running inside an Android app? Do you ever use intend to use this library code outside an Android app?Hippocras
Hello Antonio, did you find a solution?Upswell
I
6

I think that you are looking for this Gradle plugin. It brings BuildConfig to pure Java module.

https://github.com/mfuerstenau/gradle-buildconfig-plugin

Induration answered 19/1, 2018 at 13:19 Comment(5)
Oh, that’s interesting. I’ll try to test it on these days. Cannot do it atmCristoforo
If you are looking for Kotlin support too, I've made this one: github.com/gmazzo/gradle-buildconfig-pluginKrimmer
@Krimmer how to add release and debug flavors of configs?Aleut
Multi Flavor/Variants is an Android feature, but you may archieve something simillar by creating two SourceSets, debug and release, and making them extend from the main one. Then you may use my plugin to setup buildConfigFields on themKrimmer
is there a way to achieve this without a plugin?Mahlstick
M
1

If you search "BuildConfig" in plugins.gradle.org, you will find a number of Gradle plugins which offer this functionality.

The one I used in the past is de.fuerstenau.buildconfig. However, it hasn't been updated for a while (the last release was in February 2017) and it doesn't work so great with Gradle 7+ (see this issue) so I've recently migrated over to using com.github.gmazzo.buildconfig.

Misleading answered 28/6, 2022 at 14:36 Comment(0)
G
0

If by Java library you mean Android module then :

Set buildTypes variables inside the build.gradle file into library module like app module.

Get it by using the correct BuildConfig Class.

  • my.package.app.BuildConfig.APP_VARIABLE
  • my.package.library.BuildConfig.LIBRARY_VARIABLE

In other way, maybe you can take a look to gradle task and System Environment this answer can help you https://mcmap.net/q/80085/-is-it-possible-to-declare-a-variable-in-gradle-usable-in-java

There is no other way to get build configuration outside an Android module.

Just set/add an environment variable based on your android BuildConfig variables.

Germano answered 4/6, 2016 at 20:36 Comment(2)
Thanks for your answer, @Lionel. It is not an Android based project-module. It is a Java module that has plugin 'java'Cristoforo
This helped my for Android in this case. A simple Java class that needs a reference from a product flavor. BuildConfig.MYVALUE did the trick. great!Gittle

© 2022 - 2024 — McMap. All rights reserved.