DEV Community

Sophie @Appcircle for Appcircle.io

Posted on

HOW TO USE ENVIRONMENT VARIABLES IN ANDROID APPLICATIONS WITH GRADLE


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;
Enter fullscreen mode Exit fullscreen mode

}

@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. 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:

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

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.

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:

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

Top comments (0)