Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of BuildConfig in Java module? - Android

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?

like image 353
Antonio Avatar asked Jun 04 '16 20:06

Antonio


People also ask

Where is BuildConfig in Android?

BuildConfig. java is generated automatically by Android build tools, and is placed into the gen folder.

What is BuildConfig DEBUG in Android?

In recent versions of the Android Developer Tools (ADT) for Eclipse, there's a class called BuildConfig which is automatically generated by the build. This class is updated automatically by Android's build system (like the R class), and it contains a static final boolean called DEBUG, which is normally set to true.

What generates BuildConfig Java?

There's a class called BuildConfig. java which is automatically generated by the build system. This class is updated automatically by Android's build system (like the R class). It already contains a static final boolean called DEBUG, which is normally set to true.

What is gradle file in Android?

The top-level build.gradle file, located in the root project directory, defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project.


2 Answers

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

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

like image 119
Vojtěch Sázel Avatar answered Sep 20 '22 14:09

Vojtěch Sázel


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.

like image 26
Lionel Briand Avatar answered Sep 16 '22 14:09

Lionel Briand