forked from rust-nostr/nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ffi: add
android/build-aar.sh
script
Signed-off-by: Yuki Kishimoto <[email protected]>
- Loading branch information
Showing
3 changed files
with
66 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters