-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid-build.sh
executable file
·101 lines (90 loc) · 4.36 KB
/
android-build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# --- Bassie Android Build Script v1.3 ---
# The default gradle Android build toolchain is so slow and produces bloated apks,
# so I use this nice build script to get the job done!
# - Install OpenJDK JDK 17 or 21
# - Install Android SDK with packages: platform-tools platforms;android-35 build-tools;35.0.0
# - Set $ANDROID_HOME, $name, $package, $version, $password, $main_activity
min_sdk_version=21
target_sdk_version=35
################# BUILD SCRIPT #################
set -e
PATH=$PATH:$ANDROID_HOME/platform-tools:$(find "$ANDROID_HOME/build-tools" -name "$target_sdk_version*")
platform=$ANDROID_HOME/platforms/android-$target_sdk_version/android.jar
if [ -z "$name" ]; then
if [ "$1" = "vscode" ]; then
mkdir -p .vscode
echo "{\"editor.formatOnSave\":true,\"java.project.sourcePaths\":[" > .vscode/settings.json
for dir in $(find . -name "src" ! -path "*/target/*") $(find . -name "src-gen"); do
echo "\"$dir\"," >> .vscode/settings.json
done
echo "],\"java.project.referencedLibraries\":[\"$platform\"]," >> .vscode/settings.json
echo "\"java.compile.nullAnalysis.mode\":\"automatic\"," >> .vscode/settings.json
echo "\"java.compile.nullAnalysis.nullable\":[\"javax.annotation.Nullable\"]," >> .vscode/settings.json
echo "\"java.compile.nullAnalysis.nonnull\":[\"javax.annotation.Nonnull\"]," >> .vscode/settings.json
echo "\"java.compile.nullAnalysis.nonnullbydefault\":[\"javax.annotation.ParametersAreNonnullByDefault\"]," >> .vscode/settings.json
echo "\"rust-analyzer.linkedProjects\":[" >> .vscode/settings.json
for file in $(find . -name "Cargo.toml"); do
echo "\"$file\"," >> .vscode/settings.json
done
echo "]}" >> .vscode/settings.json
exit
fi
echo "You haven't set all the required variables!"
exit 1
fi
if [ "$1" = "clean" ]; then
rm -rf target
exit
fi
if [ "$1" = "key" ]; then
keytool -genkey -validity 7120 -keystore keystore.jks -keyalg RSA -keysize 4096 -storepass "$password" -keypass "$password"
exit
fi
if [ "$1" = "log" ]; then
adb logcat -c
adb logcat "*:E"
exit
fi
if [ "$1" = "clear" ]; then
echo "Clearing app data and opening application..."
adb shell pm clear "$package"
adb shell am start -n "$package/$main_activity"
exit
fi
echo "Compiling resources..."
mkdir -p target/res && rm -f target/res/*
aapt2 compile --dir res --no-crunch -o target/res
IFS="." read -r version_major version_minor version_patch <<< "$version"
version_code=$((version_major * 10000 + version_minor * 100 + version_patch))
if [ -e assets ]; then
aapt2 link target/res/*.flat --manifest AndroidManifest.xml -A assets --java target/src-gen \
--version-name "$version" --version-code "$version_code" --min-sdk-version "$min_sdk_version" \
--target-sdk-version "$target_sdk_version" -I "$platform" -o "target/$name-unaligned.apk"
else
aapt2 link target/res/*.flat --manifest AndroidManifest.xml --java target/src-gen \
--version-name "$version" --version-code "$version_code" --min-sdk-version "$min_sdk_version" \
--target-sdk-version "$target_sdk_version" -I "$platform" -o "target/$name-unaligned.apk"
fi
echo "Compiling java..."
rm -rf target/src
find src target/src-gen -name "*.java" > target/sources.txt
javac -Xlint -cp "$platform" -d target/src @target/sources.txt
echo "Compiling dex..."
find target/src -name "*.class" ! -path "*/javax/annotation/*" > target/classes.txt
if [ -e "$ANDROID_HOME/build-tools/d8.bat" ]; then
d8.bat --release --lib "$platform" --min-api "$min_sdk_version" --output target/ @target/classes.txt
else
d8 --release --lib "$platform" --min-api "$min_sdk_version" --output target/ @target/classes.txt
fi
echo "Packing and signing application..."
zip -j "target/$name-unaligned.apk" target/classes.dex > /dev/null
zipalign -f -p 4 "target/$name-unaligned.apk" "target/$name.apk"
if [ -e "$ANDROID_HOME/build-tools/apksigner.bat" ]; then
apksigner.bat sign --v4-signing-enabled false --ks keystore.jks --ks-pass "pass:$password" --ks-pass "pass:$password" "target/$name.apk"
else
apksigner sign --v4-signing-enabled false --ks keystore.jks --ks-pass "pass:$password" --ks-pass "pass:$password" "target/$name.apk"
fi
echo "Installing and opening application..."
adb install -r "target/$name.apk"
adb shell am start -n "$package/$main_activity"