A Gradle plugin helps to proxy all the BuildConfig fields in the Android project.

Overview

Readme

A Gradle plugin helps to proxy all the BuildConfig fields in the Android project.

Background

In android BuildConfig, We might have different configurations to initial some components like network server url. We However, This requires us to assemble different packages and usually is not a problem, But if we do not have the ability to assemble the package, this is a problem. So here, we might want to change the BuildConfig programmatically without changing the build settings.

How to use it

  1. Initial the Gradle plugin: buildconfig and apply the plugin: 'build-config-delegate'

  2. Initial the BuildConfig configurations.

override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase)
    //Initial all the build config configurations.
    BuildConfigDelegate.initialModuleBuildConfig(newBase)
}
  1. Change the build flavor programmatically
# Get all the flavors.
Set
   
     flavorSet = BuildConfigDelegate.getFlavorSet()
BuildConfigDelegate.setCurrentFlavor(flavor)

   

More details.

override fun onItemStateChanged(key: Long, selected: Boolean) {
    super.onItemStateChanged(key, selected)
    if (selected) {
        //Current flavor
        val flavor = buildFlavorAdapter[key.toInt()]
        BuildConfigDelegate.setCurrentFlavor(flavor)
        //Initial the text again and check the value.
        binding.textBuildConfig.text = "ServerUrl:${BuildConfig.SERVER_URL}\n" +
                "FlavorName:${BuildConfig.FLAVOR_NAME}"
    }
}

Here is the picture about change the build flavor.

image

How it works

  1. First We add gradle task for each of the build flavor e.g., androidDev in group: buildconfig
collectAndroidDevDebugBuildConfig
collectAndroidDevReleaseBuildConfig

generateAndroidDevDebugBuildConfigDelegate
generateAndroidDevReleaseBuildConfigDelegate
  1. Add metadata to the original build fields.
SERVER_URL=http://www.server.com-> `BuildConfig#(app/library)http://www.server.com`
  1. Generate a delegate class for the BuildConfig.
private static final String TEST_TEXT = new StringBuilder().append("This is server:") .append(BuildConfigDelegate.getString("library","SERVER_URL","default value") .append(" and this is the token:") .append(BuildConfigDelegate.getString("library","ACCESS_TOKEN","default value").toString() ">
private static final String SERVER_URL = BuildConfig.SERVER_URL -> 
private static final String SERVER_URL = BuildConfigDelegate.getString("app/library/...","SERVER_URL","default value")


private static final String TEST_TEXT = "This is server:"+BuildConfig.SERVER_URL+" and this is the token:"+BuildConfig.ACCESS_TOKEN; ->
private static final String TEST_TEXT = new StringBuilder().append("This is server:")
                            .append(BuildConfigDelegate.getString("library","SERVER_URL","default value")
                            .append(" and this is the token:")
                            .append(BuildConfigDelegate.getString("library","ACCESS_TOKEN","default value").toString()

So we can handle all the custom BuildConfig fields.

  1. Manage all the BuildConfig fields in runtime.
//All the BuildConfig fields. The key means the module name, the value means the all the BuildConfig fields.
public static final Map
   
    > getFlavorClassFields() {
    return buildConfigDelegate.flavorClassFields;
}
//The current build flavor. 
public static final String getCurrentFlavor() {
    return buildConfigDelegate.currentFlavor;
}
//The initial package build flavor.
public static final String getPackageFlavor() {
    return buildConfigDelegate.packageFlavor;
}

//All the build flavors
public static final Set
    
      getFlavorSet() {
    return buildConfigDelegate.flavorSet;
}
//Change the current flavor programmatically.
public static final void setCurrentFlavor(String flavor) {
    buildConfigDelegate.currentFlavor = flavor;
}

    
   
You might also like...
Gradle plugin for running Android tests with emulator.wtf

emulator.wtf Gradle Plugin Emulator.wtf is an Android cloud emulator laser-focused on performance to deliver quick feedback to your PRs. With this Gra

A Gradle Plugin for Android BundleTool.
A Gradle Plugin for Android BundleTool.

A Gradle Plugin for Android BundleTool.

Gradle plugin which analyzes the size of your Android apps

Scope is a Gradle plugin which helps you analyze the size of your Android apps. Motivation App size is an important metric which directly correlates w

The core Gradle plugin and associated logic used for Slack's Android app

slack-gradle-plugin This repository contains the core Gradle plugin and associated logic used for Slack's Android app. This repo is effectively read-o

 Gradle Plugin for Continuous Integration of AppSweep App Testing.
Gradle Plugin for Continuous Integration of AppSweep App Testing.

This Gradle plugin can be used to continuously integrate app scanning using AppSweep into your Android app build process

Gradle plugin which validates the licenses of your dependency graph match what you expect

Gradle plugin which validates the licenses of your dependency graph match what you expect

Artifactory is a gradle plugin to assist in developing Minecraft mods that can target different modloaders.

Artifactory Artifactory is a gradle plugin to assist in developing Minecraft mods that can target different modloaders. Currently, Fabric and Forge ar

Graphfity is a Gradle Plugin which creates a dependency node diagram graph about your internal modules dependencies, specially useful if you are developing a multi-module application
Graphfity is a Gradle Plugin which creates a dependency node diagram graph about your internal modules dependencies, specially useful if you are developing a multi-module application

Graphfity creates a dependency nodes diagram graph about your internal modules dependencies, specially useful if you are developing a multi-module app

A Gradle Plugin to determine which modules were affected by a set of files in a commit.
A Gradle Plugin to determine which modules were affected by a set of files in a commit.

A Gradle Plugin to determine which modules were affected by a set of files in a commit. One use case for this plugin is for developers who would like to only run tests in modules which have changed in a given commit.

Owner
Jack Chen
Jack Chen
Gradle plugin to ease Kotlin IR plugin development and usage in multimodule gradle projects

Gradle plugin to ease Kotlin IR plugin development and usage in multimodule gradle projects. Former: kotlin-ir-plugin-adapter-gradle

null 2 Mar 8, 2022
Gradle Plugin to automatically upgrade your gradle project dependencies and send a GitHub pull request with the changes

Gradle Plugin to automatically upgrade your gradle project dependencies and send a GitHub pull request with the changes

Dipien 142 Dec 29, 2022
A Gradle plugin that helps you speed up builds by excluding unnecessary modules.

?? Focus A Gradle plugin that generates module-specific settings.gradle files, allowing you to focus on a specific feature or module without needing t

Dropbox 314 Jan 2, 2023
Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms.

CompleteKotlin Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms. What this plugin provides This zero-conf

Louis CAD 235 Jan 3, 2023
Gradle Plugin that allows you to decompile bytecode compiled with Jetpack Compose Compiler Plugin into Java and check it

decomposer Gradle Plugin that allows you to decompile bytecode compiled with Jetpack Compose Compiler Plugin into Java and check it How to use Run bui

Takahiro Menju 56 Nov 18, 2022
Grazel is a Gradle plugin to automate generation of valid Bazel files for a given Android/Kotlin/Java project.

Grazel Grazel stands for Gradle to Bazel. It is a Gradle plugin that enables you to migrate Android projects to Bazel build system in an incremental a

Grab 228 Jan 2, 2023
Gradle plugin for updating a project version catalog

Version catalog update plugin This plugin helps to keep the versions in a Gradle version catalog toml file up to date. The version updates are determi

Little Robots 307 Dec 26, 2022
Gradle plugin for adds support for integrating Carthage dependencies into a KMM project

Gradle plugin for adds support for integrating Carthage dependencies into a KMM project

Wire Swiss GmbH 34 Sep 27, 2022
A Gradle plugin that improves the experience when developing Android apps, especially system tools, that use hidden APIs.

A Gradle plugin that improves the experience when developing Android apps, especially system tools, that use hidden APIs.

Rikka apps 124 Dec 31, 2022
Android Gradle Plugin -- Auto Check big image and compress image in building.

McImage I will continue to update, please rest assured to use 中文文档 Android优雅的打包时自动化获取全部res资源 McImage is a Non-invasive plugin for compress all res in

smallSohoSolo 1.1k Dec 28, 2022