This topic written about build environment of an android studio application. It will discuss on how to write variables and methods at build configuration level. Such variables and functions, they will be independent of build variants and able to provide information about build variant to the application.
During our development of an android application we create multiple build variants eg, debug
, release
. Each build variant may have different values and each time when we build variant we are manually changing the values. Have you ever thought of something which can detect the build variant at application level
and leaving the overhead of switching the entire configuration values on each build variants to build system?
The the solution to the above mentioned question is going to explained in this article.
The solution is a single word, Gradle, is a intelligent build tool used by android studio IDE, which comes as a plugin. By default all android applications are gradle based. There will be build.gradle file which is the entry point to the build configuration of the application.
Create 2 Build Variants
app/build.gradle
buildTypes { debug { buildConfigField "String", "API_KEY", "\"hkjdbfkujfdbkbfsd\"" buildConfigField "String", "API_URL", '\"http://api.example.com/dev\"' buildConfigField "boolean", "LOG", "true" } release { buildConfigField "String", "API_KEY", "\"dfskdhfskdskfsdk\"" buildConfigField "String", "API_URL", '\"http://api.example.com/live\"' buildConfigField "boolean", "LOG", "false" minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
The script taken from the build.gradle file of an android studio project. Where we have two build variants, debug
and release
. In both variants there are 3 variables declared,
- API_KEY – is a string variable
- API_URL – is a string variable
- LOG – is a boolean variable
Define Methods
Here we define two methods, getMyVersionCodeValue()
and getMyVersionCodeNameValue()
android { ..... def versionCodeValue = 1; def versionCodeNameValue = "1.0"; ext.getMyVersionCodeValue = { return versionCodeValue; } ext.getMyVersionCodeNameValue = { return versionCodeNameValue; } defaultConfig { applicationId "gradlevars.devdeeds.com.gradlevariables" minSdkVersion 16 targetSdkVersion 23 versionCode versionCodeValue versionName versionCodeNameValue } ..... }
Call the Methods
Now we can the above defined method from tasks. Here we have two tasks, assembleDebug
will be called when you are building debug variant and getMyVersionCodeNameValue
will be called when you are building release variant.
assembleDebug << { getMyVersionCodeValue() getMyVersionCodeNameValue() } assembleRelease << { getMyVersionCodeValue() getMyVersionCodeNameValue() }
Choosing the build variant
Debug Build
Go to Build > Select Build Variant... > select debug > rebuild the app
Release Build
Go to Build > Select Build Variant... > select release > rebuild the app
Generated Java File
This is the generated java class file when we built the application in debug
mode.
/** * Automatically generated file. DO NOT MODIFY */ package gradlevars.devdeeds.com.gradlevariables; public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean("true"); public static final String APPLICATION_ID = "gradlevars.devdeeds.com.gradlevariables"; 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"; // Fields from build type: debug public static final String API_KEY = "hkjdbfkujfdbkbfsd"; public static final String API_URL = "http://api.example.com/dev"; public static final boolean LOG = true; }
MainActivity.java
Let's see how to access these build variables through Activity class.
package gradlevars.devdeeds.com.gradlevariables; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(), BuildConfig.API_KEY + "\n" + BuildConfig.API_URL + "\n" + Boolean.toString(BuildConfig.LOG), Toast.LENGTH_SHORT).show(); } }
Output
Debug Variant