Skip to content

Commit 67d69b4

Browse files
committed
better build imp
1 parent 1803aae commit 67d69b4

File tree

3 files changed

+105
-28
lines changed

3 files changed

+105
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies {
4444
* Done.
4545

4646
* Using `Visual Studio Code`: (Simple)
47-
* Setup your Android SDK with ENV variable. (names `ANDROID_HOME` / `ANDROID_SDK_HOME` / `ANDROID_SDK_ROOT` are all OK).
47+
* Setup ENV variable `ANDROID_HOME` to your Android SDK installation directory.
4848
* Open the repo with `Visual Studio Code`
4949
* Press `⌘ + shift + B` (Mac) or `ctrl + shift + B` (Win/Linux), choose the option `Enable CMake And Build Project With CMake`.
5050
* Done.

utils/platform_utils.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
if ! command -v realpath &>/dev/null; then
4+
function realpath() {
5+
if [[ $# -ne 0 ]]; then
6+
pushd . >/dev/null
7+
cd $@ && pwd
8+
popd >/dev/null
9+
fi
10+
}
11+
fi
12+
13+
REPO_DIR=$(git rev-parse --show-toplevel)
14+
15+
function isWsl() {
16+
[[ -d "/mnt/c" ]] || command -v wslpath &>/dev/null
17+
}
18+
19+
function isMingW() {
20+
[[ -d "/c" ]]
21+
}
22+
23+
function isCygwin() {
24+
[[ -d "/cygdrive/c" ]]
25+
}
26+
27+
function isWindows() {
28+
# check mingw and cygwin
29+
isWsl || isMingW || isCygwin
30+
}
31+
32+
function isMacOSX() {
33+
[[ "$(uname -s)" == "Darwin" ]]
34+
}
35+
36+
function isLinux() {
37+
[[ "$(uname -s)" == "Linux" ]]
38+
}
39+
40+
function getEnvironmentVariable() {
41+
VAR_NAME="$@"
42+
RET_VALUE=
43+
if isWindows && command -v cmd &>/dev/null; then
44+
# Detect native var first
45+
RET_VALUE=$(cmd /C set "$VAR_NAME" | sed 's/.*=//')
46+
fi
47+
48+
if [[ -z "$RET_VALUE" ]]; then
49+
RET_VALUE=${VAR_NAME}
50+
fi
51+
52+
if [[ -n "$RET_VALUE" ]]; then
53+
echo $RET_VALUE
54+
fi
55+
}
56+
57+
if isWindows && ! command -v cmd &>/dev/null; then
58+
if [[ -f "/mnt/c/Windows/system32/cmd.exe" ]]; then
59+
function cmd() {
60+
/mnt/c/Windows/system32/cmd.exe $@
61+
}
62+
elif [[ -f "/c/Windows/system32/cmd.exe" ]]; then
63+
function cmd() {
64+
/c/Windows/system32/cmd.exe $@
65+
}
66+
elif [[ -f "/cygdrive/c/Windows/system32/cmd.exe" ]]; then
67+
function cmd() {
68+
/cygdrive/c/Windows/system32/cmd.exe $@
69+
}
70+
fi
71+
fi
72+
73+
function runGradleCommand() {
74+
75+
if isWindows && [[ -f "$REPO_DIR/local.properties" ]] && [[ -n "$(grep -i "sdk.dir=" "$REPO_DIR/local.properties" | grep -E ':(\\|/)')" ]]; then
76+
# Run with Windows Native
77+
echo "Perform Windows Native Gradle Func: "
78+
cmd /C gradlew $@ && exit 0
79+
# Go ahead if failed.
80+
echo "Perform Windows Native Gradle Failed, Go with current shell env"
81+
export GRADLE_RUN_WIN_FAILED=true
82+
fi
83+
84+
if ! ./gradlew $@ && [[ -z "$GRADLE_RUN_WIN_FAILED" ]]; then
85+
# Try again with Windows Native
86+
command -v cmd &>/dev/null && cmd /C gradlew $@
87+
fi
88+
}

vscode_tasks.sh

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,13 @@ THIS_DIR="$(pwd)"
99
PROJECT_DIR="$THIS_DIR"
1010
ADB_COMMAND="$PROJECT_DIR/utils/adb_command.sh"
1111

12+
source "$PROJECT_DIR/utils/platform_utils.sh"
13+
1214
export PACKAGE_NAME="org.wysaid.cgeDemo"
1315
export LAUNCH_ACTIVITY="MainActivity"
1416
export GRADLEW_RUN_TASK="installDebug"
1517
export ANDROID_BUILD_TYPE="assembleDebug"
1618

17-
if ! command -v cmd &>/dev/null && [[ -f "/mnt/c/Windows/system32/cmd.exe" ]]; then
18-
function cmd() {
19-
/mnt/c/Windows/system32/cmd.exe $@
20-
}
21-
fi
22-
23-
function runGradleCommand() {
24-
if ! ./gradlew $@; then
25-
command -v cmd &>/dev/null && cmd /C gradlew $@
26-
fi
27-
}
28-
2919
function setupProject() {
3020
if [[ -f "$PROJECT_DIR/local.properties" ]] && grep -E '^usingCMakeCompile=true' "$PROJECT_DIR/local.properties"; then
3121
echo "Using cmake, skip ndk build..."
@@ -87,21 +77,20 @@ function buildProject() {
8777
fi
8878
}
8979

90-
if [[ ! -f "local.properties" ]]; then
91-
if [[ -n "$ANDROID_HOME" ]]; then
92-
echo "sdk.dir=$ANDROID_HOME" >>local.properties
93-
elif [[ -n "$ANDROID_SDK_ROOT" ]]; then
94-
echo "sdk.dir=$ANDROID_SDK_ROOT" >>local.properties
95-
elif [[ -n "$ANDROID_SDK_HOME" ]]; then
96-
if [[ -d "$ANDROID_SDK_HOME/platform-tools" ]]; then
97-
echo "sdk.dir=$ANDROID_SDK_HOME" >>local.properties
98-
elif [[ -d "$ANDROID_SDK_HOME/../platform-tools" ]]; then
99-
echo "sdk.dir=$(realpath $ANDROID_SDK_HOME/../platform-tools)" >>local.properties
100-
fi
101-
elif [[ -n "$ANDROID_SDK" ]]; then
102-
echo "sdk.dir=$ANDROID_SDK" >>local.properties
103-
else
104-
echo "Can't find ANDROID_SDK, Please setup 'local.properties'" >&2
80+
function patchAndroidSDKLocation() {
81+
VAR_ENV_VALUE=$(getEnvironmentVariable $@)
82+
if [[ -n "$VAR_ENV_VALUE" ]]; then
83+
VAR_ENV_VALUE=$(echo $VAR_ENV_VALUE | tr '\\' '/')
84+
echo "sdk.dir=$VAR_ENV_VALUE" >>local.properties
85+
return 0
86+
fi
87+
return 1
88+
}
89+
90+
if [[ ! -f "local.properties" ]] && ! grep "sdk.dir=" local.properties &>/dev/null; then
91+
if ! (patchAndroidSDKLocation ANDROID_HOME ||
92+
patchAndroidSDKLocation ANDROID_SDK_ROOT); then
93+
echo "sdk.dir is missing in 'local.properties' and env var ANDROID_HOME is not defined." >&2
10594
fi
10695
fi
10796

0 commit comments

Comments
 (0)