2022-08-03 15:52:16 +03:00
#!/usr/bin/env sh
# Safety measures
set -eu
u = " $USER "
tmp = $( mktemp -d -t)
2022-12-08 11:55:37 +03:00
folder = " $tmp /simplex-chat "
2022-09-02 00:32:57 +03:00
commands = "nix git curl gradle zip unzip zipalign"
2022-08-03 15:52:16 +03:00
2022-08-04 11:36:36 +03:00
nix_install( ) {
2022-08-03 15:52:16 +03:00
# Pre-setup nix
[ ! -d /nix ] && sudo sh -c " mkdir -p /nix && chown -R $u /nix "
# Install nix
2023-03-25 21:51:27 +00:00
nix_ver = "nix-2.14.1"
2022-08-04 13:20:58 +03:00
nix_url = " https://releases.nixos.org/nix/ $nix_ver /install "
2023-03-25 21:51:27 +00:00
nix_hash = "565974057264f0536f600c68d59395927cd73e9fc5a60f33c1906e8f7bc33fcf"
2022-08-04 13:20:58 +03:00
curl -sSf " $nix_url " -o " $tmp /nix-install "
printf "%s %s" " $nix_hash " " $tmp /nix-install " | sha256sum -c
2022-08-03 15:52:16 +03:00
chmod +x " $tmp /nix-install " && " $tmp /nix-install " --no-daemon
. " $HOME /.nix-profile/etc/profile.d/nix.sh "
}
2022-08-04 11:36:36 +03:00
nix_setup( ) {
2022-12-23 16:10:36 +03:00
printf "sandbox = true\nmax-jobs = auto\nexperimental-features = nix-command flakes\n" > " $tmp /nix.conf "
2022-08-04 13:20:58 +03:00
export NIX_CONF_DIR = " $tmp / "
2022-08-04 11:36:36 +03:00
}
2022-08-03 15:52:16 +03:00
git_setup( ) {
2023-03-25 21:51:27 +00:00
[ " $folder " != "." ] && {
git clone --depth= 1 https://github.com/simplex-chat/simplex-chat " $folder "
}
2022-08-03 15:52:16 +03:00
# Switch to nix-android branch
2022-12-08 11:55:37 +03:00
git -C " $folder " checkout " $commit "
2022-08-03 15:52:16 +03:00
# Create missing folders
2022-12-08 11:55:37 +03:00
mkdir -p " $folder /apps/android/app/src/main/cpp/libs/arm64-v8a "
2023-03-25 21:51:27 +00:00
mkdir -p " $folder /apps/android/app/src/main/cpp/libs/armeabi-v7a "
2022-08-03 15:52:16 +03:00
}
checks( ) {
set +u
2022-08-05 10:14:32 +03:00
2022-08-04 11:36:36 +03:00
for i in $commands ; do
case $i in
nix)
if ! command -v " $i " > /dev/null 2>& 1 || [ ! -f " $HOME /.nix-profile/etc/profile.d/nix.sh " ] ; then
nix_install
fi
nix_setup
; ;
*)
if ! command -v " $i " > /dev/null 2>& 1; then
commands_failed = " $i $commands_failed "
fi
; ;
esac
done
2022-08-03 15:52:16 +03:00
if [ -n " $commands_failed " ] ; then
commands_failed = ${ commands_failed % * }
printf "%s is not found in your \$PATH. Please install them and re-run the script.\n" " $commands_failed "
exit 1
fi
set -u
}
build( ) {
# Build simplex lib
2022-12-08 11:55:37 +03:00
nix build " $folder #hydraJobs.aarch64-android:lib:simplex-chat.x86_64-linux "
unzip -o " $PWD /result/pkg-aarch64-android-libsimplex.zip " -d " $folder /apps/android/app/src/main/cpp/libs/arm64-v8a "
2023-03-25 21:51:27 +00:00
nix build " $folder #hydraJobs.armv7a-android:lib:simplex-chat.x86_64-linux "
unzip -o " $PWD /result/pkg-armv7a-android-libsimplex.zip " -d " $folder /apps/android/app/src/main/cpp/libs/armeabi-v7a "
2022-08-03 15:52:16 +03:00
# Build android suppprt lib
2022-12-08 11:55:37 +03:00
nix build " $folder #hydraJobs.aarch64-android:lib:support.x86_64-linux "
unzip -o " $PWD /result/pkg-aarch64-android-libsupport.zip " -d " $folder /apps/android/app/src/main/cpp/libs/arm64-v8a "
2022-08-03 15:52:16 +03:00
2023-03-25 21:51:27 +00:00
nix build " $folder #hydraJobs.armv7a-android:lib:support.x86_64-linux "
unzip -o " $PWD /result/pkg-armv7a-android-libsupport.zip " -d " $folder /apps/android/app/src/main/cpp/libs/armeabi-v7a "
2022-12-08 11:55:37 +03:00
sed -i.bak 's/${extract_native_libs}/true/' " $folder /apps/android/app/src/main/AndroidManifest.xml "
2023-03-25 21:51:27 +00:00
sed -i.bak '/android {/a lint {abortOnError false}' " $folder /apps/android/app/build.gradle "
2022-09-02 00:32:57 +03:00
2022-12-08 11:55:37 +03:00
gradle -p " $folder /apps/android/ " clean build assembleRelease
2022-09-02 00:32:57 +03:00
2023-03-25 21:51:27 +00:00
mkdir -p " $tmp /android-aarch64 "
unzip -oqd " $tmp /android-aarch64/ " " $folder /apps/android/app/build/outputs/apk/release/app-arm64-v8a-release-unsigned.apk "
( cd " $tmp /android-aarch64 " && zip -rq5 " $tmp /simplex-chat-aarch64.apk " . && zip -rq0 " $tmp /simplex-chat-aarch64.apk " resources.arsc res)
zipalign -p -f 4 " $tmp /simplex-chat-aarch64.apk " " $PWD /simplex-chat-aarch64.apk "
2022-09-02 00:32:57 +03:00
2023-03-25 21:51:27 +00:00
mkdir -p " $tmp /android-armv7 "
unzip -oqd " $tmp /android-armv7/ " " $folder /apps/android/app/build/outputs/apk/release/app-armeabi-v7a-release-unsigned.apk "
( cd " $tmp /android-armv7 " && zip -rq5 " $tmp /simplex-chat-armv7.apk " . && zip -rq0 " $tmp /simplex-chat-armv7.apk " resources.arsc res)
zipalign -p -f 4 " $tmp /simplex-chat-armv7.apk " " $PWD /simplex-chat-armv7.apk "
2022-08-03 15:52:16 +03:00
}
final( ) {
printf "Simplex-chat was successfully compiled: %s/simplex-chat.apk\nDelete nix and gradle caches with 'rm -rf /nix && rm \$HOME/.nix* && \$HOME/.gradle/caches' in case if no longer needed.\n" " $PWD "
}
main( ) {
2022-12-08 11:55:37 +03:00
while getopts ":s" opt; do
case $opt in
s) folder = "." ; ;
*) printf "Flag '-%s' doesn't exist.\n" " $OPTARG " ; exit 1 ; ;
esac
done
shift $(( $OPTIND - 1 ))
commit = " $1 " ; shift 1
2022-08-03 15:52:16 +03:00
checks
git_setup
build
final
}
2022-12-08 11:55:37 +03:00
main " $@ "