Android app environment variables

How to Use Environment Variables with Gradle

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. You can enable environment variables in Gradle and configure your CI/CD system to use these values while building.

Enabling Environment Variables in Gradle

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:

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:

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.

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:

Читайте также:  Фоторедакторы для андроид 4pda

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.

Источник

Gradle: Android Build Variables Done Right

Using Gradle to create build variables, avoid mistakes and improve continuous integration

May 8, 2018 · 6 min read

Gradle helps teams build, automate and deliver better software, faster. From mobile apps to microservices, from small startups to big enterprises.

Proposal

Present how developers can use just Gradle system to set up, develop, deliver and integrate Android projects using build variables.

How Android building works

Gradle is the standard build tool for Android Studio. So, it can do a lot of heavy work like to build classes for constants and resources, compile project, export .apk, execute tests, etc.

And, not less important, Gradle can handle build types and a team can create an environment to automate tasks like deploy.

Build variables for build variants

Here is the most important part of setting a great environment with consistent variables. For every build type such as development, staging, release (I’ll write something about build types) we can create specific configuration or a group of variables.

Common project constant variables:

#1 Base URLs (hostnames)

#2 File paths or filenames

All of these constants above could be defined as build variables. In this case, developers don’t need to concern about these definitions. Also, a member of the team who doesn’t have permissions can’t change variables for release deploy — of course, if the team creates a secure CI environment.

For example, imagine your app needs to pre-populate a SQLite database and the file is on your root project. One day a developer decides to play with this database, add ‘test’ data and forget to remove it. Boom! The data is in production and your product has a little problem. Believe me, it happens more than we think. We can solve that using a database file on deploy server and defining a build variable in Gradle with its file path to load it.

Gradle properties

Before talking about how Gradle injects variables in our Android project, we need to understand where we can store values and use them as build variables.

By default, Gradle uses a file called gradle.properties in the root directory for Android Projects. This extension file is widely used in Java projects, so it is also used for Android projects. The file has a simple key — value data structure.

What most developers don’t know is we can use gradle.properties to store constants and override them in an environment, and we can create a specific properties file for every Android module and override their parent.

By default, there area properties file in Gradle Home where Gradle system lives in your environment, they are loaded automatically by Gradle— if the filename is gradle.properties — we can use these variables in app/build.gradle just writing their names.

Below a default Gradle properties file created by Android Studio:

And the following is an example of app/gradle.properties :

Читайте также:  Как зарядить андроид если батарейка заряжена

Gradle build variables

After understanding how to store values used by Gradle, we need to understand how to use and set build variables. By default, Gradle compiles and manipulates some Android project files to inject variables.

Below are common injection/loading variables:

1 — BuildConfig.java

Gradle can add a field to BuildConfig.java file which can be easily used in the project. This file is generated automatically by Gradle system.

How Gradle inject values through its buildConfigField method to Java:

So, if you need to access variables in Java classes as a build variable now you can do it using Gradle buildConfigField . Because it’s an automatically generated file, we can see a lot of static fields there:

2 — AndroidManifest.xml

Another way to use build variables from Gradle is to inject or update a Manifest file in build time.

Imagine our app has an intent filter to open when a specific URLs is called by Android System. In this case, we can set a placeholder for the manifest file with specific hostname for each build type.

Defining a placeholder for the AndroidManifest.xml in app/build.gradle :

And using placeholder in AndroidManifest.xml :

3 — System env

Finally, the most important part of Gradle build is to get variables from system environment. It’s a typical usage with CI — Continuous integration — for release deployment type.

Common usage of system variables in Gradle:

Note that System.getEnv(«HOSTNAME») is used to load variables directly from system environment.

We need to define the variable using export (UNIX):

or add to .bash_profile :

After that, your project is ready to use variables from system environment, yet this approach is always recommended to be used via command line or with a CI tool.

Conclusion

Gradle already has a lot of features that we can use to improve our Android project. Defining build variables may not be important for solving problems without a good environment to develop, deliver and maintain a project.

There are a lot of aspects we need to consider about building apps:

  • What is the deployment environment?
  • How secure is our deployment environment?
  • Who does decide what can be a build configuration?
  • Who does maintain private information?

Below I created an image of a simple structure of build configuration:

As we can see in the image, we have 4 build types:

  • Debug: for developers who don’t need to know about build variables.
  • Internal: Testing or QA. The team also doesn’t need to know about variables, but needs to deploy different build types using CI.
  • Staging: External testing or validation.
  • Release: Production/App Store. All variables for this build type should be secure in deployment environment.

Github

Check a boilerplate repository that I’ve created using build variables as a part of a study about development environment:

Источник

Software testing tutorials and automation

A blog on Selenium tutorial, Selenium webdriver tutorial, Selenium IDE tutorial, Appium Tutorial, Selenium Grid Tutorial, Jmeter Tutorial.

Pages

Set ANDROID_HOME and Path Environment Variables For SDK In Windows

Set ANDROID_HOME windows Variable

  • Open » Environment Variables» dialog from win start menu -> Right click on My Computer -> Select properties -> Advanced system settings -> Environment Variables button from Advanced tab. You can view detailed steps to open Environment Variables dialog in THIS POST.
  • Click on New button under User Variable table. It will open New User Variable dialog.
  • Set Variable Name = ANDROID_HOME and Variable value = E:\SDK (Path of SDK folder). Path can be different for you as per your SDK folder location as described in previous post.
  • Click on OK button to close New User Variable dialog as shown in bellow image.
Читайте также:  Java android строка состояния

That means android is configured properly in your system.

Now android_home environment variable windows and Path Environment Variables are set for android SDK in windows environment. So android environment is configured and ready to use with appium to execute software automation tests. Next post will describe you how to integrate SDK with eclipse using Eclipse ADT Plugin to use android SDK with eclipse.

21 comments:

Thanks for the post its really help full for initial android user

Thank you for your help but i have an issue that is i do not find the sdk path in my computer. I want this setup for an ionic app create android platform and generate apk file. I also installed in my computer android studio.

Please help me to find the sdk path. I’m getting the error ANDROID_HOME veriable not setup, setup it manually.

u have to add JAVA_HOME variable also along with ANDROID_HOME to have proper functionality.

u have to add JAVA_HOME variable also along with ANDROID_HOME to have proper functionality.

I’m getting ANDROID_HOME variable not setup setup it manually to generate a apk from a ionic framework project. i did not find the sdk path in my computer but i also installed android studio in my computer. Please help me.

You are developing project means you are developer and asking for help regarding deploy from a testing background funny.

I am answering late but hope someone will find it useful :
If you have already installed android studio then you will find your sdk in user appdata hidden folder.

thanks for the help!

‘android’ is not recognised as internal or external command,operable program or batch file- message is displayed in command prompt. I had set the path correctly, But don’t know what is the problem can you please help me with it??

maybe you have spaces in your path, for example if your sdk in Program files folder.
you should set the correct path, use such scheme:
Progra

1 = ‘Program Files’
Progra

2 = ‘Program Files(x86)’
it is for windows x64

maybe you have some spaces in your path, for example your sdk is located in «Program files».
use such scheme:
Progra

1 = ‘Program Files’
Progra

2 = ‘Program Files(x86)’
it’s for windows x64

I have also faced the same error. But once after providing ‘SDK path’ in the System variables ‘Path’. The error is cleared

thanks for help

By far the most helpful page I have found about setting up all the variables and paths for android 🙂

I run the command prompt I got the sdk manager, but I also got «this system cannot find the specified path», let me know what I have to do solve this issue

The «android» command is depercated
For manual sdk, avd, and project management, please use android studio.
For command-line tools, use tools\bin\sdk manager.bat
And tools\bin\avdmanager.bat

Same issue, What is the solution for this ?

Источник

Оцените статью