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?

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? Commented Jun 6, 2016 at 17:07
  • Hello Antonio, did you find a solution? Commented Jan 5, 2018 at 22:40

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, that’s interesting. I’ll try to test it on these days. Cannot do it atm
If you are looking for Kotlin support too, I've made this one: github.com/gmazzo/gradle-buildconfig-plugin
@gmazzo how to add release and debug flavors of configs?
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 them
is there a way to achieve this without a plugin?
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.

Comments

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://stackoverflow.com/a/25714617/4681367

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.

2 Comments

Thanks for your answer, @Lionel. It is not an Android based project-module. It is a Java module that has plugin 'java'
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.