summaryrefslogtreecommitdiffstats
path: root/.vars.rc.do
blob: 8cddd9eeab4032d7ac5c62b03075e9d8dd0fdd56 (plain)
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
#!/usr/bin/env bash
set -e

unset ANDROID_SDK BUILD_TOOLS BUILD_TOOLS_VER JAVAC KEYSTORE KEYSTORE_ARGS \
	KEYSTORE_PASS KOTLINC MANIFEST MAVEN_REPO MIN_SDK_VER PLATFORM \
	TARGET_SDK_VER verbose

MANIFEST=AndroidManifest.xml
redo-ifchange "$MANIFEST"
MIN_SDK_VER=$(xmllint --xpath "string(/manifest/uses-sdk/@*[local-name()='minSdkVersion'])" "$MANIFEST")
TARGET_SDK_VER=$(xmllint --xpath "string(/manifest/uses-sdk/@*[local-name()='targetSdkVersion'])" "$MANIFEST")
BUILD_TOOLS_VER=33.0.2
APKSIGNER_SIGN_ARGS=()
D8_ARGS=(--min-api "$MIN_SDK_VER")
declare -A MAVEN_REPO
MAVEN_REPO[central]=https://repo1.maven.org/maven2
MAVEN_REPO[google]=https://dl.google.com/dl/android/maven2

if [ -e config.rc ]; then
	redo-ifchange config.rc
	. ./config.rc
else
	redo-ifcreate config.rc
fi

if [[ ! ( $BUILD_TOOLS || $PLATFORM ) && ! $ANDROID_SDK ]]; then
	cat <<-"EOF" >&2
	$BUILD_TOOLS or $PLATFORM unset without setting $ANDROID_SDK.
	Either set both $BUILD_TOOLS and $PLATFORM or set $ANDROID_SDK.
	EOF
	exit 1
fi

BUILD_TOOLS=${BUILD_TOOLS-$ANDROID_SDK/build-tools/$BUILD_TOOLS_VER}
PLATFORM=${PLATFORM-$ANDROID_SDK/platforms/android-$TARGET_SDK_VER}

KEYSTORE=${KEYSTORE-$HOME/.android/debug.keystore}
KEYSTORE_PASS=${KEYSTORE_PASS-pass:android}

check() {
	what=$1
	shift
	for p do
		if command -v "$p" >/dev/null 2>&1; then
			command -v "$p"
			return
		fi
	done
	echo "$what not set or found" >&2
	return 1
}

JAVAC=$(check '$JAVAC or javac' "$JAVAC" javac) || exit
KOTLINC=$(check '$KOTLINC or kotlinc' "$KOTLINC" kotlinc) || exit

{
	d8() (
		set -e
		android=$PLATFORM/android.jar d8_jar=$BUILD_TOOLS/lib/d8.jar
		dest=$1
		shift
		redo-ifchange "$d8_jar" "$android"
		tmp=$(mktemp -d)
		trap 'rm -rf "$tmp"' EXIT
		java -Xmx2G -cp "$d8_jar" com.android.tools.r8.D8 "${D8_ARGS[@]}" \
			--classpath "$android" --output "$tmp" "$@"
		mv "$tmp/classes.dex" "$dest"
	)
	printf "# generated by %s\n" "$0"
	declare -p APKSIGNER_SIGN_ARGS BUILD_TOOLS D8_ARGS JAVAC KEYSTORE \
		KEYSTORE_PASS KOTLINC MANIFEST MAVEN_REPO PLATFORM
	declare -fp d8
} >"$3"

if [[ $verbose ]]; then
	echo "# Configuration regenerated:" >&2
	cat "$3" >&2
fi
# vim:ft=bash