From 1803fda3abdaabd4beea8c26885e2394546f5ee0 Mon Sep 17 00:00:00 2001 From: Siddarth Kumar Date: Wed, 19 Jun 2024 14:45:09 +0530 Subject: [PATCH] use nix for debug android builds --- Makefile | 2 +- ci/Jenkinsfile.nix-cache | 2 +- nix/deps/gradle/README.md | 2 +- nix/mobile/android/{release.nix => build.nix} | 7 ++-- nix/mobile/android/default.nix | 6 ++-- scripts/build-android.sh | 2 +- scripts/run-android.sh | 32 +++++++++++++++++++ scripts/run-metro.sh | 5 +++ scripts/wait-for-metro-port.sh | 14 ++++++++ 9 files changed, 63 insertions(+), 9 deletions(-) rename nix/mobile/android/{release.nix => build.nix} (96%) create mode 100644 scripts/run-android.sh create mode 100755 scripts/run-metro.sh create mode 100755 scripts/wait-for-metro-port.sh diff --git a/Makefile b/Makefile index 2e91bbec1f..0e113548a5 100644 --- a/Makefile +++ b/Makefile @@ -268,7 +268,7 @@ run-re-frisk: ##@run Start re-frisk server # TODO: Migrate this to a Nix recipe, much the same way as nix/mobile/android/targets/release-android.nix run-android: export TARGET := android run-android: ##@run Build Android APK and start it on the device - npx react-native run-android --appIdSuffix debug + @scripts/run-android.sh SIMULATOR= run-ios: export TARGET := ios diff --git a/ci/Jenkinsfile.nix-cache b/ci/Jenkinsfile.nix-cache index 43c4a78cfb..04075c0a4b 100644 --- a/ci/Jenkinsfile.nix-cache +++ b/ci/Jenkinsfile.nix-cache @@ -75,7 +75,7 @@ pipeline { steps { script { /* build/fetch things required to build jsbundle and android */ nix.build( - attr: 'targets.mobile.android.release.buildInputs', + attr: 'targets.mobile.android.build.buildInputs', sandbox: false, pure: false, link: false diff --git a/nix/deps/gradle/README.md b/nix/deps/gradle/README.md index a56037fd46..48d0854fb2 100644 --- a/nix/deps/gradle/README.md +++ b/nix/deps/gradle/README.md @@ -6,7 +6,7 @@ This directory contains the tools and the data that allows Nix to manage Gradle Simply calling `generate.sh` should result in a `deps.json` file which is used in the derivation that provides Gradle dependencies when building the Android app. -You can see in [`nix/mobile/android/release.nix`](../../mobile/android/release.nix) that it's used via the `-Dmaven.repo.local='${deps.gradle}'` Gradle flag. +You can see in [`nix/mobile/android/release.nix`](../../mobile/android/build.nix) that it's used via the `-Dmaven.repo.local='${deps.gradle}'` Gradle flag. # Files diff --git a/nix/mobile/android/release.nix b/nix/mobile/android/build.nix similarity index 96% rename from nix/mobile/android/release.nix rename to nix/mobile/android/build.nix index 6cf96dbf02..79ed562632 100644 --- a/nix/mobile/android/release.nix +++ b/nix/mobile/android/build.nix @@ -28,7 +28,10 @@ let else ".env"; # There are only two types of Gradle build targets: pr and release - gradleBuildType = if buildType == "pr" then "Pr" else "Release"; + gradleBuildType = + if buildType == "pr" then "Pr" + else if buildType == "debug" then "Debug" + else "Release"; apksPath = "./android/app/build/outputs/apk/${toLower gradleBuildType}"; @@ -138,7 +141,7 @@ in stdenv.mkDerivation rec { || exit 1 popd > /dev/null ''; - doCheck = true; + doCheck = buildType != debug; checkPhase = '' ls ${apksPath}/*.apk \ | xargs -n1 ${pkgs.unzip}/bin/unzip -qql \ diff --git a/nix/mobile/android/default.nix b/nix/mobile/android/default.nix index a7ec8e805b..053c49abcf 100644 --- a/nix/mobile/android/default.nix +++ b/nix/mobile/android/default.nix @@ -5,10 +5,10 @@ let # Import a jsbundle compiled out of clojure codebase jsbundle = callPackage ./jsbundle { }; - release = callPackage ./release.nix { inherit jsbundle status-go; }; + build = callPackage ./build.nix { inherit jsbundle status-go; }; in { # TARGETS - inherit release jsbundle; + inherit build jsbundle; shell = mkShell { buildInputs = with pkgs; [ @@ -19,7 +19,7 @@ in { ]; inputsFrom = [ - release + build androidShell ]; diff --git a/scripts/build-android.sh b/scripts/build-android.sh index a98afb9bc3..642fa30985 100755 --- a/scripts/build-android.sh +++ b/scripts/build-android.sh @@ -62,4 +62,4 @@ fi nixOpts+=("--arg" "config" "{${config}}") -"${GIT_ROOT}/nix/scripts/build.sh" targets.mobile.android.release "${nixOpts[@]}" +"${GIT_ROOT}/nix/scripts/build.sh" targets.mobile.android.build "${nixOpts[@]}" diff --git a/scripts/run-android.sh b/scripts/run-android.sh new file mode 100644 index 0000000000..2595b76008 --- /dev/null +++ b/scripts/run-android.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -euo pipefail + +GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) +ADB_INSTALL_LOG_FILE="${GIT_ROOT}/logs/adb_install.log" +ADB_SHELL_MONKEY_LOG_FILE="${GIT_ROOT}/logs/adb_shell_monkey.log" + +Generate android debug build. +export BUILD_ENV=debug +export BUILD_TYPE=debug +export BUILD_NUMBER=99999 +export ANDROID_ABI_SPLIT=false +export ANDROID_ABI_INCLUDE="armeabi-v7a;arm64-v8a;x86;x86_64" +"${GIT_ROOT}/scripts/build-android.sh" + +Install the APK on running emulator or android device. +installAndLaunchApp() { + adb install -r ./result/app-debug.apk > "${ADB_INSTALL_LOG_FILE}" 2>&1 + "${GIT_ROOT}/scripts/wait-for-metro-port.sh" 2>&1 + # connected android devices need this port to be exposed for metro + adb reverse "tcp:8081" "tcp:8081" + adb shell monkey -p im.status.ethereum.debug 1 > "${ADB_SHELL_MONKEY_LOG_FILE}" 2>&1 +} + +showAdbLogs() { + cat "${ADB_INSTALL_LOG_FILE}" >&2; + cat "${ADB_SHELL_MONKEY_LOG_FILE}" >&2; +} + +trap showAdbLogs EXIT ERR INT QUIT +installAndLaunchApp & +exec "${GIT_ROOT}/scripts/run-metro.sh" 2>&1 diff --git a/scripts/run-metro.sh b/scripts/run-metro.sh new file mode 100755 index 0000000000..37741881c6 --- /dev/null +++ b/scripts/run-metro.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +pkill -f 'react-native start' + +react-native start --reset-cache diff --git a/scripts/wait-for-metro-port.sh b/scripts/wait-for-metro-port.sh new file mode 100755 index 0000000000..67144cfe5a --- /dev/null +++ b/scripts/wait-for-metro-port.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail + +TIMEOUT=10 # Metro should not take this long to start. + +while [ "${TIMEOUT}" -gt 0 ]; do + if ! lsof -i:8081 &> /dev/null; then + echo "." + sleep 1 + ((TIMEOUT--)) + else + break + fi +done