HOW TO USE ENVIRONMENT VARIABLES IN ANDROID APPLICATIONS WITH GRADLE
Published: 2021-07-02 14:51:50

<a href="https://appcircle.io/blog/how-to-use-environment-variables-in-android-applications-with-gradle/" link rel="canonical" ><img src="https://appcircle.io/wp-content/uploads/2020/04/Android-Env-Vars-Gradle-845x321.png.webp"></a>
Reading Time: 3 minutes

Environment variables are the variables set outside the projects and typically embedded into the operating system or the service platform the project will be built on.

Developers constantly feel the need to use environment variables in their applications for various reasons. Some of these reasons can be:

-   Using different values for the same variables depending on the project "environments" like development, testing, staging and production
-   Keeping sensitive values and secrets such as API keys and passwords secure so that they are not revealed within the source code
-   Limit the number of modification to the source code by keeping some information outside the project code and change those values when needed, suitable for things like feature flags and A/B testing

For mobile applications, where building the apps is required with every change, environment variables are even more crucial for high configurability.

Enabling Environment Variables in Android Projects
--------------------------------------------------

Android developers can use environment variables using Gradle's module-level build configuration. This module-level Gradle configuration file lets you specify build settings for that application module.

To use environment variables in your Android app project, specify a new `buildConfigField` method in the Android block of your build.gradle file, as shown below:

`build.gradle file:`

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "com.example.appcircle_sample_android"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "APPCIRCLE_API_URL", "\"${System.env.AC_API_URL}\""
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

During the build, Gradle will generate the `buildConfig` class and these variables will be accessible within your application in the runtime.

You can now use this variable in your application. Here is an example showing how to use the defined environment variable in a view:

public class SampleFragmentDetail extends Fragment {
MainActivity activity;

public SampleFragmentDetail() {
// Required empty public constructor
}

public void setActivity(MainActivity activity) {
this.activity = activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sample_detail, container, false);

       activity.showHomeItem(true);
       TextView appVersionTextView = view.findViewById(R.id.appVersionTextView);
       appVersionTextView.setText("Api URL: " + BuildConfig.APPCIRCLE_API_URL);
       return view;
}

@Override
public void onResume() {
super.onResume();
}
}

Setting Environment Variables in Appcircle
------------------------------------------

[Appcircle allows you to create groups of environment variables to be used during your builds](https://appcircle.io/features). You can create environment variable groups for different environments of your project like development, staging and production or for different app configurations.

Like the Android sample above, you may want to use different API endpoints for development, staging and production.

To create different values of the same variable, first create an environment variable group for each environment/configuration:

[![](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars1-845x321.png.webp)](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars1.png)

Then, create an environment variable with the same name in each group and set the proper values for each branch.

Tip

You can duplicate variable groups for easy environment creation

[![](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars2-845x321.png.webp)](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars2.png)

Once the variable groups are set, you can specify which variable group to use in the build configuration for the specific branch. You can set different groups for different branches.

[![](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars3-845x321.png.webp)](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars3.png)

During the build, the build agent will get the values for the variables from the designated group for the branch from which you are building your application. Then the build.gradle file in your module will consume the environment variable values for your application to use them during the runtime:

[![](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars4-845x321.png.webp)](https://appcircle.io/wp-content/uploads/2020/04/Appcircle_Android_Env_Vars4.png)

With Appcircle, you can easily utilize environment variables in your Android and iOS projects from a single source of truth and keep your secrets secure. You can change environments easily and receive automated builds for different branches utilizing different environment configurations with instant iOS and Android app previews in your browser.

[Start Using Appcircle Now](https://appcircle.io/start)