Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ xcuserdata/
bindings/lni_react_native/src/generated/**
bindings/lni_react_native/cpp/generated/**

**/lni_react_native_old/**
**/lni_react_native_old/**
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let package = Package(
// .binaryTarget(name: "lniFFI", path: "bindings/swift/lniFFI.xcframework")
.binaryTarget(
name: "lniFFI",
url: "https://github.com/lightning-node-interface/lni/releases/download/v0.1.2/lniFFI.xcframework.zip",
checksum: "e81ffafc663c45626b2ea5fffb93f724beb28ac94b860b3732c59b8c99e49c6d"
url: "https://github.com/lightning-node-interface/lni/releases/download/v0.2.0/lniFFI.xcframework.zip",
checksum: "cac08ee1e25a888df8952d530c4a798850cd84537aef394435f12c2d2f0890a3"
)
]
)
22 changes: 22 additions & 0 deletions bindings/kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ node.close()

See the `example/` directory for a complete Android example project.

### Building for Android

```bash
./build.sh --release
```

This builds native libraries for all Android targets (arm64-v8a, armeabi-v7a, x86_64, x86) and copies them to the example project's `jniLibs` directory.

To skip Android builds (only generate Kotlin bindings):

```bash
./build.sh --release --no-android
```

### Important: Invalidate Caches

After updating native libraries, you may need to invalidate Android Studio caches:

**File → Invalidate Caches → Invalidate and Restart**

This ensures Android Studio picks up the updated native libraries.

### Adding to your Android project

1. Copy the generated `lni.kt` file to your project
Expand Down
121 changes: 87 additions & 34 deletions bindings/kotlin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
# This script:
# 1. Builds the lni library with uniffi feature
# 2. Uses uniffi-bindgen to generate Kotlin bindings from the shared library
# 3. Optionally builds for Android targets
# 3. Builds for Android targets using cargo-ndk (default, use --no-android to skip)
#
# Usage: ./build.sh [--release] [--android]
# ./build.sh --release --android # Build for Android in release mode
# Prerequisites:
# - cargo-ndk: cargo install cargo-ndk
# - Android NDK: Set ANDROID_NDK_HOME environment variable
# - Rust targets: rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android
#
# Usage: ./build.sh [--release] [--no-android]
# ./build.sh --release # Build for Android in release mode
# ./build.sh --release --no-android # Skip Android builds

set -e

Expand All @@ -18,19 +24,50 @@ EXAMPLE_DIR="$SCRIPT_DIR/example"

# Parse arguments
BUILD_TYPE="debug"
BUILD_ANDROID=false
BUILD_ANDROID=true

for arg in "$@"; do
case $arg in
--release)
BUILD_TYPE="release"
;;
--android)
BUILD_ANDROID=true
--no-android)
BUILD_ANDROID=false
;;
esac
done

# Check for cargo-ndk if building for Android
if [ "$BUILD_ANDROID" = true ]; then
if ! command -v cargo-ndk &> /dev/null; then
echo "Error: cargo-ndk is required for Android builds."
echo "Install it with: cargo install cargo-ndk"
echo "Or skip Android builds with: ./build.sh --no-android"
exit 1
fi

if [ -z "$ANDROID_NDK_HOME" ]; then
echo "Warning: ANDROID_NDK_HOME is not set."
echo "Attempting to find NDK automatically..."

# Try common NDK locations
if [ -d "$HOME/Library/Android/sdk/ndk" ]; then
# Find the latest NDK version
ANDROID_NDK_HOME=$(find "$HOME/Library/Android/sdk/ndk" -maxdepth 1 -type d | sort -V | tail -1)
elif [ -d "$HOME/Android/Sdk/ndk" ]; then
ANDROID_NDK_HOME=$(find "$HOME/Android/Sdk/ndk" -maxdepth 1 -type d | sort -V | tail -1)
fi

if [ -n "$ANDROID_NDK_HOME" ] && [ -d "$ANDROID_NDK_HOME" ]; then
echo "Found NDK at: $ANDROID_NDK_HOME"
export ANDROID_NDK_HOME
else
echo "Error: Could not find Android NDK. Please set ANDROID_NDK_HOME."
exit 1
fi
fi
fi

echo "Building LNI library with UniFFI feature ($BUILD_TYPE)..."

cd "$ROOT_DIR"
Expand All @@ -47,40 +84,46 @@ fi
# Build for Android targets if requested
if [ "$BUILD_ANDROID" = true ]; then
echo ""
echo "Building for Android targets..."
echo "Building for Android targets using cargo-ndk..."

# Android target configurations: (rust_target, jni_dir)
ANDROID_TARGETS=(
"aarch64-linux-android:arm64-v8a"
"armv7-linux-androideabi:armeabi-v7a"
"x86_64-linux-android:x86_64"
"i686-linux-android:x86"
)
# Ensure Android targets are installed
echo "Ensuring Rust Android targets are installed..."
rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android 2>/dev/null || true

# Create jniLibs directories
# Create jniLibs directory
JNILIBS_DIR="$EXAMPLE_DIR/app/src/main/jniLibs"
mkdir -p "$JNILIBS_DIR"

for target_pair in "${ANDROID_TARGETS[@]}"; do
RUST_TARGET="${target_pair%%:*}"
JNI_DIR="${target_pair##*:}"

echo " Building for $RUST_TARGET..."

if [ "$BUILD_TYPE" == "release" ]; then
cargo build --package lni --features uniffi --release --target "$RUST_TARGET"
ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/release/liblni.so"
else
cargo build --package lni --features uniffi --target "$RUST_TARGET"
ANDROID_LIB_PATH="$ROOT_DIR/target/$RUST_TARGET/debug/liblni.so"
fi

# Copy to jniLibs
mkdir -p "$JNILIBS_DIR/$JNI_DIR"
cp "$ANDROID_LIB_PATH" "$JNILIBS_DIR/$JNI_DIR/"
echo " Copied to $JNILIBS_DIR/$JNI_DIR/liblni.so"
done
cd "$ROOT_DIR"

# Use cargo-ndk to build for all Android targets
# cargo-ndk automatically handles:
# - Setting up correct linker
# - Linking libc++_shared.so
# - Avoiding OpenSSL cross-compilation issues (when using rustls-tls)
if [ "$BUILD_TYPE" == "release" ]; then
echo "Building release for Android targets..."
cargo ndk \
-t aarch64-linux-android \
-t armv7-linux-androideabi \
-t x86_64-linux-android \
-t i686-linux-android \
-o "$JNILIBS_DIR" \
build --package lni --features uniffi --release
else
echo "Building debug for Android targets..."
cargo ndk \
-t aarch64-linux-android \
-t armv7-linux-androideabi \
-t x86_64-linux-android \
-t i686-linux-android \
-o "$JNILIBS_DIR" \
build --package lni --features uniffi
fi

echo "Android builds complete!"
echo "Libraries copied to: $JNILIBS_DIR"
ls -la "$JNILIBS_DIR"
fi

# Find the shared library (Linux: .so, macOS: .dylib)
Expand Down Expand Up @@ -111,3 +154,13 @@ echo "Kotlin bindings generated successfully in: $OUTPUT_DIR"
echo ""
echo "Generated files:"
ls -la "$OUTPUT_DIR"

if [ "$BUILD_ANDROID" = true ]; then
echo ""
echo "============================================================"
echo "IMPORTANT: After updating native libraries, you may need to"
echo "invalidate Android Studio caches:"
echo ""
echo " File → Invalidate Caches → Invalidate and Restart"
echo "============================================================"
fi
15 changes: 12 additions & 3 deletions bindings/kotlin/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ example/
./build.sh --release
```

2. Build native libraries for your target architectures (e.g., for Android):
This will build native libraries for your target architectures (e.g., for Android). this should have been done in build.sh:
```bash
# Example for arm64-v8a
cargo build --package lni --features uniffi --release --target aarch64-linux-android
Expand All @@ -50,11 +50,20 @@ For JVM-based execution (testing on desktop):
```

For Android:
1. `cd .. && ./build.sh --release --android 2>&1`
1. `cd .. && ./build.sh --release --android 2>&1 && cd example`
2. Import the project into Android Studio and Open `lni/bindings/kotlin/example`
3. File → Sync Project with Gradle Files
4. Build → Clean Project
5. Build and run on an emulator or device
5. Build and run on an emulator or device
```
# in example dir
# list
emulator -list-avds
# start
emulator -avd EMULATOR
# run app
./gradlew installDebug && adb shell am start -n com.lni.example/.MainActivity
```

## Usage Examples

Expand Down
Loading