18

I'm using AndroidStudio and I have the following:

  • Main Android project: app
  • Java Library: communication

I want to set up my "communication" module's gradle file to achieve the following:

  • As app module's gradle, I can setup variables for BuildConfig.java file, e.g.:

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

and then I can use them using BuildConfig.SERVER_URL

The question is: How do I achieve this using a Java Library module?

Antonio
  • 11,413
  • 6
  • 34
  • 48

3 Answers3

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

Vojtěch Sázel
  • 538
  • 6
  • 22
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.

Community
  • 1
  • 1
Lionel Briand
  • 1,732
  • 2
  • 13
  • 21
  • 3
    Thanks for your answer, @Lionel. It is not an Android based project-module. It is a Java module that has `plugin 'java'` – Antonio Jun 04 '16 at 21:08
  • 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! – CaptainCrunch Jun 01 '21 at 18:05
0

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.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147