diff --git a/bindings/nostr-sdk-ffi/.cargo/config.toml b/bindings/nostr-sdk-ffi/.cargo/config.toml index 8a8b35153..f537ed183 100644 --- a/bindings/nostr-sdk-ffi/.cargo/config.toml +++ b/bindings/nostr-sdk-ffi/.cargo/config.toml @@ -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 diff --git a/bindings/nostr-sdk-ffi/android/build-aar.sh b/bindings/nostr-sdk-ffi/android/build-aar.sh new file mode 100644 index 000000000..638d8415a --- /dev/null +++ b/bindings/nostr-sdk-ffi/android/build-aar.sh @@ -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!" diff --git a/bindings/nostr-sdk-ffi/justfile b/bindings/nostr-sdk-ffi/justfile index 909f634e2..b628c6b12 100755 --- a/bindings/nostr-sdk-ffi/justfile +++ b/bindings/nostr-sdk-ffi/justfile @@ -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]