Skip to content

Commit

Permalink
New Android Build Script (#2096)
Browse files Browse the repository at this point in the history
* New Android Build Script

* Clean up + Works for CI now

* Simplify android build.sh
 - Fix /var/home/vitor fallback for Linux systems
 - Run a single cargo ndk for all targets (not parallel build, but a bit faster)
 - Fix android target s/x86/x86_64/
 - Format setup.sh
 - Minor improvements to rust mobile targets installation step in setup.sh

* Add notice to CONTRIBUTING that only Java <= 17 is supported for building android
 - Make prettier ignore some mobile build artifacts

* When in CI, Fix build android core for host architecture

---------

Co-authored-by: Vítor Vasconcellos <vasconcellos.dev@gmail.com>
  • Loading branch information
Rocky43007 and HeavenVolkoff committed Feb 20, 2024
1 parent 19b2243 commit 2a28347
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# built product/cache
target/
dist/
apps/mobile/android/app/build
apps/mobile/modules/sd-core/android/build

# macOS/iOS product/cache
.build/
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Make sure to read the [guidelines](https://spacedrive.com/docs/developers/prereq

To run the mobile app:

- Install Java JDK <= 17 for Android
- Java 21 is not compatible: https://github.com/react-native-async-storage/async-storage/issues/1057#issuecomment-1925963956
- Install [Android Studio](https://developer.android.com/studio) for Android and [Xcode](https://apps.apple.com/au/app/xcode/id497799835) for iOS development.
- Run `./scripts/setup.sh mobile`
- This will set up most of the dependencies required to build the mobile app.
Expand Down
45 changes: 5 additions & 40 deletions apps/mobile/modules/sd-core/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ android {
withSourcesJar()
}
}
sourceSets {
main {
jniLibs.srcDirs = ['../../../../../target/release']
}
}
}

repositories {
Expand All @@ -98,41 +93,11 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
}

apply plugin: 'org.mozilla.rust-android-gradle.rust-android'

def cargoTargets = ["x86_64"]

if (System.getenv('SPACEDRIVE_CI') != "1") {
cargoTargets = ["arm", "arm64", "x86"]
}

cargo {
final String osName = System.getProperty("os.name").toLowerCase();

module = "./crate"
libname = "sd_mobile_android"
pythonCommand = 'python3'
profile = 'release' // 'debug'
targets = cargoTargets
targetDirectory = "../../../../../target" // Monorepo moment
exec { spec, toolchain ->

def dir = "${android.ndkDirectory}/toolchains/llvm/prebuilt/${osName.contains("mac") ? "darwin" : osName}-x86_64/bin/llvm-ranlib"

spec.environment("RANLIB_armv7-linux-androideabi", "${dir}")
spec.environment("RANLIB_aarch64-linux-android", "${dir}")
spec.environment("RANLIB_i686-linux-android", "${dir}")
spec.environment("RANLIB_x86_64-linux-android", "${dir}")
}
// Run the ./build.sh script to build the Rust code
task buildRustCode(type: Exec) {
commandLine "./build.sh"
}

tasks.whenTaskAdded { task ->
// Require cargo to be run before copying native libraries.
if ((task.name == 'mergeDebugJniLibFolders' || task.name == 'mergeReleaseJniLibFolders')) {
task.dependsOn 'cargoBuild'
}
// Require "clean builds" to avoid issues with build caches.
if (task.name == 'assembleDebug' || task.name == 'assembleRelease') {
task.dependsOn 'clean'
}
tasks.named('preBuild').configure {
dependsOn buildRustCode
}
72 changes: 72 additions & 0 deletions apps/mobile/modules/sd-core/android/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env sh

set -eu

if [ "${CI:-}" = "true" ]; then
set -x
fi

err() {
for _line in "$@"; do
echo "$_line" >&2
done
exit 1
}

if [ -z "${HOME:-}" ]; then
case "$(uname)" in
"Darwin")
HOME="$(CDPATH='' cd -- "$(osascript -e 'set output to (POSIX path of (path to home folder))')" && pwd -P)"
;;
"Linux")
HOME="$(CDPATH='' cd -- "$(getent passwd "$(id -un)" | cut -d: -f6)" && pwd -P)"
;;
*)
err "Your OS ($(uname)) is not supported by this script." \
'We would welcome a PR or some help adding your OS to this script.' \
'https://github.com/spacedriveapp/spacedrive/issues'
;;
esac

export HOME
fi

echo "Building 'sd-mobile-android' library..."

__dirname="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"

# Ensure output dir exists
OUTPUT_DIRECTORY="${__dirname}/../../../../../apps/mobile/android/app/src/main/jniLibs"
mkdir -p "$OUTPUT_DIRECTORY"

# Required for CI and for everyone I guess?
export PATH="${CARGO_HOME:-"${HOME}/.cargo"}/bin:$PATH"

# Set the targets to build
# If CI, then we build x86_64 else we build all targets
if [ "${CI:-}" = "true" ]; then
# TODO: This need to be adjusted for future mobile release CI
case "$(uname -m)" in
"arm64" | "aarch64")
ANDROID_BUILD_TARGET_LIST="arm64-v8a"
;;
"x86_64")
ANDROID_BUILD_TARGET_LIST="x86_64"
;;
*)
err 'Unsupported architecture for CI build.'
;;
esac
else
ANDROID_BUILD_TARGET_LIST="arm64-v8a armeabi-v7a x86_64"
fi

# Configure build targets CLI arg for `cargo ndk`
echo "Building targets: $ANDROID_BUILD_TARGET_LIST"
set --
for _target in $ANDROID_BUILD_TARGET_LIST; do
set -- "$@" -t "$_target"
done

cd "${__dirname}/crate"
cargo ndk --platform 34 "$@" -o "$OUTPUT_DIRECTORY" build --release
47 changes: 29 additions & 18 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ if [ "${1:-}" = "mobile" ]; then
# Android targets
echo "Installing Android targets for Rust..."

rustup target add armv7-linux-androideabi # for arm
rustup target add aarch64-linux-android # for arm64
rustup target add i686-linux-android # for x86
rustup target add x86_64-linux-android # for x86_64
rustup target add x86_64-unknown-linux-gnu # for linux-x86-64
rustup target add aarch64-apple-darwin # for darwin arm64 (if you have an M1 Mac)
rustup target add x86_64-apple-darwin # for darwin x86_64 (if you have an Intel Mac)
rustup target add x86_64-pc-windows-gnu # for win32-x86-64-gnu
rustup target add x86_64-pc-windows-msvc # for win32-x86-64-msvc
if [ "${CI:-}" = "true" ]; then
# TODO: This need to be adjusted for future mobile release CI
rustup target add x86_64-linux-android
else
rustup target add \
aarch64-linux-android \
armv7-linux-androideabi \
x86_64-linux-android
fi

echo
else
Expand All @@ -106,12 +106,9 @@ fi
# Install system deps
case "$(uname)" in
"Darwin")
if [ "$(uname -m)" = 'x86_64' ]; then (
if [ "${CI:-}" = "true" ]; then
export NONINTERACTIVE=1
fi
if [ "$(uname -m)" = 'x86_64' ] && ! [ "${CI:-}" = "true" ]; then
brew install nasm
); fi
fi

# Install rust deps for iOS
if [ $MOBILE -eq 1 ]; then
Expand All @@ -123,9 +120,17 @@ case "$(uname)" in

echo "Installing iOS targets for Rust..."

rustup target add aarch64-apple-ios
rustup target add aarch64-apple-ios-sim
rustup target add x86_64-apple-ios # for CI
case "$(uname -m)" in
"arm64" | "aarch64") # M series
rustup target add aarch64-apple-ios aarch64-apple-ios-sim
;;
"x86_64") # Intel
rustup target add x86_64-apple-ios aarch64-apple-ios
;;
*)
err 'Unsupported architecture for CI build.'
;;
esac

echo
fi
Expand Down Expand Up @@ -236,7 +241,13 @@ esac

if [ "${CI:-}" != "true" ]; then
echo "Installing Rust tools..."
cargo install cargo-watch

_tools="cargo-watch"
if [ $MOBILE -eq 1 ]; then
_tools="$_tools cargo-ndk" # For building Android
fi

echo "$_tools" | xargs cargo install
fi

echo 'Your machine has been setup for Spacedrive development!'

0 comments on commit 2a28347

Please sign in to comment.