Commit 7c948d8e authored by Robert Mourning's avatar Robert Mourning Committed by Geoffrey Métais

compile: robustly initialize

If exists but has been modified since last build,
it might need to be changed to contain appropriate android sdk/ndk paths.
For example, Android Studio will automatically modify
if the project is opened in an environment with a different sdk location.

Supports the following operations:
 - create a new if none exists, same as previous commit.
 - append sdk.dir and ndk.dir lines if none are found in existing file.
 - if existing file has incorrect sdk/ndk values, or has multiple entries
 for either, those lines will be replaced with a single one for each of
 sdk.dir and ndk.dir.
Signed-off-by: default avatarGeoffrey Métais <>
parent d3d594c4
......@@ -153,10 +153,60 @@ if [ ! -f ]; then
echo storepwd=android >>
if [ ! -f ]; then
echo sdk.dir=$ANDROID_SDK >
echo ndk.dir=$ANDROID_NDK >>
init_local_props() {
# initialize the file,
# or fix it if it was modified (by Android Studio, for example).
echo_props() {
echo "sdk.dir=$ANDROID_SDK"
echo "ndk.dir=$ANDROID_NDK"
# first check if the file just needs to be created for the first time
if [ ! -f "$1" ]; then
echo_props > "$1"
return 0
# check for lines setting the SDK directory
total_sdk_count=`grep -c "${sdk_line_start}" "$1"`
good_sdk_count=`grep -c "${sdk_line_start}${ANDROID_SDK}\$" "$1"`
# check for lines setting the NDK directory
total_ndk_count=`grep -c "${ndk_line_start}" "$1"`
good_ndk_count=`grep -c "${ndk_line_start}${ANDROID_NDK}\$" "$1"`
# if one of each is found and both match the environment vars, no action needed
if [ "$total_sdk_count" -eq "1" -a "$good_sdk_count" -eq "1" \
-a "$total_ndk_count" -eq "1" -a "$good_ndk_count" -eq "1" ]
return 0
# if neither property is set they can simply be appended to the file
if [ "$total_sdk_count" -eq "0" -a "$total_ndk_count" -eq "0" ]; then
echo_props >> "$1"
return 0
# if a property is set incorrectly or too many times,
# remove all instances of both properties and append correct ones.
replace_props() {
while IFS= read -r LINE || [ -n "$LINE" ]; do
if [ "x$line_sdk_dir" = "x$LINE" -a "x$line_ndk_dir" = "x$LINE" ]; then
echo "$LINE"
done <"$1" >"$temp_props"
echo_props >> "$temp_props"
mv -f -- "$temp_props" "$1"
echo " Contains incompatible sdk.dir and/or ndk.dir properties. Replacing..."
replace_props "$1"
echo " Finished replacing sdk.dir and/or ndk.dir with current environment variables."
init_local_props || { echo "Error initializing"; exit $?; }
if [ ! -d "$ANDROID_SDK/licenses" ]; then
mkdir "$ANDROID_SDK/licenses"
echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_SDK/licenses/android-sdk-license"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment