Skip to content

Commit c523686

Browse files
committed
nostr-ffi: fix uniffi and add bindings-android
1 parent e58a9e1 commit c523686

19 files changed

+570
-47
lines changed

bindings/nostr-ffi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ffi/

bindings/nostr-ffi/Cargo.toml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
name = "nostr-ffi"
33
version = "0.1.0"
44
edition = "2021"
5-
authors = ["Yuki Kishimoto <yukikishimoto@proton.me>"]
5+
authors = ["Yuki Kishimoto <yukikishimoto@protonmail.com>"]
66
homepage.workspace = true
77
repository.workspace = true
88
license.workspace = true
99
rust-version.workspace = true
10-
keywords = ["nostr", "rust", "ffi"]
10+
keywords = ["nostr", "ffi"]
1111

1212
[lib]
13+
name = "nostr_ffi"
1314
crate-type = ["lib", "cdylib", "staticlib"]
1415

16+
[[bin]]
17+
name = "uniffi-bindgen"
18+
path = "uniffi-bindgen.rs"
19+
1520
[dependencies]
1621
nostr = { path = "../../crates/nostr" }
17-
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "b50a66fb95ca1f9cf69b25e020f7c3382c0ce528" }
18-
uniffi_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "b50a66fb95ca1f9cf69b25e020f7c3382c0ce528" }
22+
uniffi = { version = "0.23", features = ["cli"] }
23+
uniffi_bindgen = "0.23"
24+
uniffi_macros = "0.23"
1925

2026
[build-dependencies]
21-
uniffi_build = { git = "https://github.com/mozilla/uniffi-rs", rev = "b50a66fb95ca1f9cf69b25e020f7c3382c0ce528", features = ["builtin-bindgen"] }
27+
uniffi = { version = "0.23", features = ["build"] }

bindings/nostr-ffi/Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
SOURCES=$(sort $(wildcard ./src/*.rs ./src/**/*.rs))
2+
3+
.PHONY: init
4+
init:
5+
rustup target add aarch64-apple-ios x86_64-apple-ios
6+
rustup target add aarch64-apple-darwin x86_64-apple-darwin
7+
rustup target add aarch64-apple-ios-sim
8+
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
9+
@if [ $$(uname) == "Darwin" ] ; then cargo install cargo-lipo ; fi
10+
cargo install cbindgen
11+
cargo install cargo-ndk
12+
13+
.PHONY: ndk-home
14+
ndk-home:
15+
@if [ ! -d "${ANDROID_NDK_HOME}" ] ; then \
16+
echo "Error: Please, set the ANDROID_NDK_HOME env variable to point to your NDK folder" ; \
17+
exit 1 ; \
18+
fi
19+
20+
.PHONY: sdk-root
21+
sdk-root:
22+
@if [ ! -d "${ANDROID_SDK_ROOT}" ] ; then \
23+
echo "Error: Please, set the ANDROID_SDK_ROOT env variable to point to your SDK folder" ; \
24+
exit 1 ; \
25+
fi
26+
27+
kotlin: android
28+
cargo run --features=uniffi/cli --bin uniffi-bindgen generate src/nostr.udl --language kotlin --no-format -o ffi/kotlin
29+
30+
android: aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
31+
32+
aarch64-linux-android: $(SOURCES) ndk-home
33+
cargo ndk -t aarch64-linux-android -o ffi/kotlin/jniLibs build --release
34+
35+
armv7-linux-androideabi: $(SOURCES) ndk-home
36+
cargo ndk -t armv7-linux-androideabi -o ffi/kotlin/jniLibs build --release
37+
38+
i686-linux-android: $(SOURCES) ndk-home
39+
cargo ndk -t i686-linux-android -o ffi/kotlin/jniLibs build --release
40+
41+
x86_64-linux-android: $(SOURCES) ndk-home
42+
cargo ndk -t x86_64-linux-android -o ffi/kotlin/jniLibs build --release
43+
44+
bindings-android: sdk-root kotlin
45+
cp -r ffi/kotlin/jniLibs bindings-android/lib/src/main
46+
cp -r ffi/kotlin/nostr bindings-android/lib/src/main/kotlin/
47+
cd bindings-android && ./gradlew assemble
48+
mkdir -p ffi/android
49+
cp bindings-android/lib/build/outputs/aar/lib-release.aar ffi/android

bindings/nostr-ffi/README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
# Nostr FFI
22

3-
## Usage
3+
## Prerequisites
4+
* When building for Android:
5+
* Set the `ANDROID_SDK_ROOT` env variable
6+
* Set the `ANDROID_NDK_HOME` env variable
47

5-
- Kotlin: https://github.com/mozilla/rust-android-gradle
6-
- Swift: https://mozilla.github.io/uniffi-rs/swift/overview.html
8+
## Build
9+
10+
On first usage you will need to run:
11+
12+
```
13+
make init
14+
```
15+
16+
### Kotlin
17+
18+
### Libraries and Bindings
19+
20+
This command will build libraries for different platforms in `target/` folder and copy them to `ffi/kotlin/jniLibs`.
21+
In addition it will generate Kotlin bindings in `ffi/kotlin/nostr`.
22+
23+
```
24+
make kotlin
25+
```
26+
27+
### Android Archive (AAR)
28+
29+
This command will build an AAR file in `ffi/android/lib-release.aar`:
30+
31+
```
32+
make bindings-android
33+
```
34+
35+
See [Add your AAR or JAR as a dependency](https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency) in Android's docs for more information on how to integrate such an archive into your project.
36+
37+
## License
38+
39+
This project is distributed under the MIT software license - see the [LICENSE](../../LICENSE) file for details
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/android
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=android
3+
4+
### Android ###
5+
# Gradle files
6+
.gradle/
7+
build/
8+
9+
# Local configuration file (sdk path, etc)
10+
local.properties
11+
12+
# Log/OS Files
13+
*.log
14+
15+
# Android Studio generated files and folders
16+
captures/
17+
.externalNativeBuild/
18+
.cxx/
19+
*.apk
20+
output.json
21+
22+
# IntelliJ
23+
*.iml
24+
.idea/
25+
misc.xml
26+
deploymentTargetDropDown.xml
27+
render.experimental.xml
28+
29+
# Keystore files
30+
*.jks
31+
*.keystore
32+
33+
# Google Services (e.g. APIs or Firebase)
34+
google-services.json
35+
36+
# Android Profiling
37+
*.hprof
38+
39+
### Android Patch ###
40+
gen-external-apklibs
41+
42+
# Replacement of .externalNativeBuild directories introduced
43+
# with Android Studio 3.5.
44+
45+
# End of https://www.toptal.com/developers/gitignore/api/android
46+
47+
lib/src/main/jniLibs/
48+
lib/src/main/kotlin/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
buildscript {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath("com.android.tools.build:gradle:7.1.2")
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
org.gradle.jvmargs=-Xmx1536m
2+
android.useAndroidX=true
3+
android.enableJetifier=true
4+
kotlin.code.style=official
5+
libraryVersion=0.0.1
58.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)