Skip to main content

How to Disable BuildConfig Generation

This article was translated from Japanese by Claude Code.

TL;DR
#

android {
  ...
  libraryVariants.all {
    it.generateBuildConfig.enabled = false
  }
}

About Auto-Generated BuildConfig.java
#

In Android app development and similar projects, the benefits of structuring projects as multi-module grow increasingly. Normally, when you build an Android project, a file called BuildConfig.java is automatically generated. (A file called R.java that holds a list of resource IDs is also generated.)

BuildConfig.java typically contains entries like this:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.shaunkawano.blog.sample";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

How to Disable BuildConfig Generation
#

You can add custom constants to the BuildConfig file through configuration. However, one BuildConfig file holding such constants per project should be sufficient. Also, it’s rare for BuildConfig files in library modules that make up multi-module projects to have dynamically defined constant values. In other words, BuildConfig files in library modules are often unnecessary.

To prevent generating BuildConfig files for library modules, add the following configuration to the build.gradle of the library module:

android {
  ...

  libraryVariants.all {
    it.generateBuildConfig.enabled = false
  }
}

References
#

Just a note, but that’s all.