Skip to content

Commit

Permalink
ffi: add android/build-aar.sh script
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Dec 2, 2024
1 parent f263245 commit a0fd5b1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 55 deletions.
6 changes: 3 additions & 3 deletions bindings/nostr-sdk-ffi/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[profile.release]
opt-level = 's' # Optimize for size.
opt-level = 's' # Optimize for size. The `z` level produce a smaller binary but when compressed with upx is smaller the `s` one.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
codegen-units = 1 # Reduce the number of codegen units to increase optimizations.
panic = "abort" # Abort on panic
strip = "debuginfo" # Partially strip symbols from binary
strip = "debuginfo" # Partially strip symbols from binary. If fully stripped, UniFFI can't generate foreign-language bindings
61 changes: 61 additions & 0 deletions bindings/nostr-sdk-ffi/android/build-aar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

set -exuo pipefail

CDYLIB="libnostr_sdk_ffi.so"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET_DIR="${SCRIPT_DIR}/../../../target"
ANDROID_MAIN_DIR="${SCRIPT_DIR}/lib/src/main"
ANDROID_MAIN_KOTLIN_DIR="${ANDROID_MAIN_DIR}/kotlin"
ANDROID_MAIN_JNI_LIBS_DIR="${ANDROID_MAIN_DIR}/jniLibs"
FFI_DIR="${SCRIPT_DIR}/../ffi"
FFI_KOTLIN_DIR="${FFI_DIR}/kotlin"
FFI_JNI_LIBS_DIR="${FFI_KOTLIN_DIR}/jniLibs"
FFI_ANDROID_DIR="${FFI_DIR}/android"

# Check if ANDROID_NDK_HOME env is set
if [ ! -d "${ANDROID_NDK_HOME}" ] ; then \
echo "Error: Please, set the ANDROID_NDK_HOME env variable to point to your NDK folder" ; \
exit 1 ; \
fi

# Check if ANDROID_SDK_ROOT env is set
if [ ! -d "${ANDROID_SDK_ROOT}" ] ; then \
echo "Error: Please, set the ANDROID_SDK_ROOT env variable to point to your SDK folder" ; \
exit 1 ; \
fi

# Install deps
cargo ndk --version || cargo install cargo-ndk

# Clean
rm -rf "${FFI_KOTLIN_DIR}"
rm -rf "${FFI_ANDROID_DIR}"
rm -rf "${ANDROID_MAIN_KOTLIN_DIR}"
rm -rf "${ANDROID_MAIN_JNI_LIBS_DIR}"

# Install targets
rustup target add aarch64-linux-android
rustup target add x86_64-linux-android
rustup target add armv7-linux-androideabi

# Build targets
cargo ndk -t aarch64-linux-android -t x86_64-linux-android -t armv7-linux-androideabi -o "${FFI_JNI_LIBS_DIR}" build -p nostr-sdk-ffi --lib --release

# Generate Kotlin bindings
cargo run -p nostr-sdk-ffi --features uniffi-cli --bin uniffi-bindgen generate --library "${TARGET_DIR}/x86_64-linux-android/release/${CDYLIB}" --language kotlin --no-format -o "${FFI_KOTLIN_DIR}"

# Compress libraries
upx --best --lzma --android-shlib "${FFI_JNI_LIBS_DIR}"/*/*.so

# Assemble AAR
mkdir -p "${ANDROID_MAIN_KOTLIN_DIR}"
cp -r "${FFI_JNI_LIBS_DIR}" "${ANDROID_MAIN_DIR}"
cp -r "${FFI_KOTLIN_DIR}/rust" "${ANDROID_MAIN_KOTLIN_DIR}"
"${SCRIPT_DIR}/gradlew" assembleRelease

# Copy AAR to the output dir
mkdir -p "${FFI_ANDROID_DIR}"
cp "${SCRIPT_DIR}/lib/build/outputs/aar/lib-release.aar" "${FFI_ANDROID_DIR}"

echo "Done!"
54 changes: 2 additions & 52 deletions bindings/nostr-sdk-ffi/justfile
Original file line number Diff line number Diff line change
@@ -1,58 +1,8 @@
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]

# Check if ANDROID_NDK_HOME env is set
[private]
ndk-home:
@if [ ! -d "${ANDROID_NDK_HOME}" ] ; then \
echo "Error: Please, set the ANDROID_NDK_HOME env variable to point to your NDK folder" ; \
exit 1 ; \
fi

# Check if ANDROID_SDK_ROOT env is set
[private]
sdk-root:
@if [ ! -d "${ANDROID_SDK_ROOT}" ] ; then \
echo "Error: Please, set the ANDROID_SDK_ROOT env variable to point to your SDK folder" ; \
exit 1 ; \
fi

[private]
ndk: ndk-home
@cargo ndk --version || cargo install cargo-ndk

[private]
clean-android:
rm -rf ffi/android
rm -rf ffi/kotlin

[private]
jni-libs: ndk
rustup target add aarch64-linux-android
rustup target add x86_64-linux-android
rustup target add armv7-linux-androideabi
cargo ndk -t aarch64-linux-android -t x86_64-linux-android -t armv7-linux-androideabi -o ffi/kotlin/jniLibs build --lib --release

kotlin: clean-android jni-libs
cargo run --features uniffi-cli --bin uniffi-bindgen generate --library ../../target/x86_64-linux-android/release/libnostr_sdk_ffi.so --language kotlin --no-format -o ffi/kotlin

[private]
compress-android:
upx --best --lzma --android-shlib ffi/kotlin/jniLibs/*/*.so

# Copy required modules and libs and assemble AAR
[private]
assemble-aar:
rm -rf android/lib/src/main/jniLibs
rm -rf android/lib/src/main/kotlin
cp -r ffi/kotlin/jniLibs android/lib/src/main
mkdir -p android/lib/src/main/kotlin/
cp -r ffi/kotlin/rust android/lib/src/main/kotlin/
cd android && ./gradlew assembleRelease
mkdir -p ffi/android
cp android/lib/build/outputs/aar/lib-release.aar ffi/android

# Compile and build Android Archive (AAR)
aar: sdk-root kotlin compress-android assemble-aar
aar:
@cd android && bash build-aar.sh

# Publish android bindings
[confirm]
Expand Down

0 comments on commit a0fd5b1

Please sign in to comment.