SoFunction
Updated on 2025-04-07

Guide to how to get and set system environment variables on Android

Preface

In the process of Android source code analysis, we often see ("xxx") and getenv ("xxx") and ("xxx") obtaining the environment variable values ​​of the Android system. The focus of this article is not to analyze the code calling logic, but to explain how to add/view system environment variables in the Android development source code environment and in the Android terminal environment. Don't say much nonsense!

1. Add environment variables in the Android source code environment

Children's boots who have experience in Linux development should know that adding environment variables is usually displayed through export and then echo, and the same is true in Android. Through global search, we found that the default file set by the Android source code environment variable is in system/core/rootdir/. This does not mean that it can only be added in this rc. In fact, it is OK to add it in other rcs at the appropriate triggering time. The file under my source code is as follows:

# set up the global environment
on init
 export ANDROID_BOOTLOGO 1
 export ANDROID_ROOT /system
 export ANDROID_ASSETS /system/app
 export ANDROID_DATA /data
 export ANDROID_STORAGE /storage
 export EXTERNAL_STORAGE /sdcard
 export ASEC_MOUNTPOINT /mnt/asec
 export BOOTCLASSPATH %BOOTCLASSPATH%
 export SYSTEMSERVERCLASSPATH %SYSTEMSERVERCLASSPATH%
 %EXPORT_GLOBAL_ASAN_OPTIONS%
 %EXPORT_GLOBAL_GCOV_OPTIONS%

So if we want to add environment variables, it is OK. We won’t actually operate it here. If you are interested, you can operate it yourself.

2. View and modify environment variables in Android terminal

Viewing and modifying environment variables in the Android terminal must be through adb. Let’s actually do it below.

2.1 View system environment variables

Here we still have to borrow export and echo commands, and the operation is as follows:

λ adb shell
msm8953_64:/ # export
ANDROID_ASSETS
ANDROID_BOOTLOGO
ANDROID_DATA
ANDROID_ROOT
ANDROID_SOCKET_adbd
ANDROID_STORAGE
ASEC_MOUNTPOINT
BOOTCLASSPATH
DOWNLOAD_CACHE
EXTERNAL_STORAGE
HOME
HOSTNAME
LOGNAME
PATH
SHELL
SYSTEMSERVERCLASSPATH
TERM
TMPDIR
USER
msm8953_64:/ # echo $ANDROID_BOOTLOGO
1
msm8953_64:/ # echo $ANDROID_DATA
/data
msm8953_64:/ #

2.2 Set system environment variables

Here we still have to borrow export and echo commands, and the operation is as follows:

130|msm8953_64:/ # export HELLOWORD=Android
msm8953_64:/ # echo $HELLOWORD
Android
msm8953_64:/ #

summary

Okay, the chapter on Android getting and setting system environment variables has come to an end. After learning this trick, we can directly view the relevant values ​​of system environment variables through the adb command in the analysis of Android source code, so that we can analyze it easily. See you all!

Summarize

This is the end of this article about Android obtaining and setting system environment variables. For more related content about Android obtaining and setting system environment variables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!