Skip to content

Commit

Permalink
Merge pull request #292 from Concordium/android-sdk
Browse files Browse the repository at this point in the history
Android sdk
  • Loading branch information
shjortConcordium authored Jan 16, 2024
2 parents 2b7b0e4 + 624a54b commit 31c2c29
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 26 deletions.
28 changes: 26 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.62
toolchain: 1.69
- name: Create native dependencies
run: make
- name: Cache cargo dependencies and targets
Expand All @@ -79,8 +79,32 @@ jobs:
key: ${{ runner.os }}-${{ env.dummy }}-rust-java-deps-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock', '**/pom.xml') }}
restore-keys: |
${{ runner.os }}-${{ env.dummy }}-rust-java-deps
- name: Initialize Parent Pom
run: mvn install -N
- name: Build and test sdk
run: cd concordium-sdk && mvn --batch-mode --update-snapshots install
- name: Build and test examples
run: cd concordium-sdk-examples && mvn --batch-mode --update-snapshots install

build-android:
# Use fixed OS version because we install packages on the system.
runs-on: ubuntu-22.04
if: ${{ !github.event.pull_request.draft }}
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: Setup JDK 8
uses: actions/setup-java@v2
with:
java-version: 8
distribution: 'adopt'
cache: maven
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.69
- name: Create native dependencies
run: make add-android-targets && make android
- name: Build android sdk
run: cd concordium-android-sdk && mvn --batch-mode --update-snapshots install
43 changes: 38 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Workflow for release a jar file with all native dependencies embedded.
# The Workflow also releases a AAR file with the native dependencies build for Android.
# The workflow runs whenever a release is published.

name: Release
Expand Down Expand Up @@ -27,7 +28,7 @@ jobs:
with:
submodules: recursive

- name: Make ubuntu native depencies
- name: Make ubuntu native dependencies
run: make

- name: Upload linux library
Expand All @@ -52,7 +53,7 @@ jobs:
with:
submodules: recursive

- name: Make macos native depencies
- name: Make macos native dependencies
run: make

- name: Upload macos library
Expand All @@ -77,7 +78,7 @@ jobs:
with:
submodules: recursive

- name: Make windows native depencies
- name: Make windows native dependencies
run: cd crypto-jni && cargo build --release

- name: Upload windows library
Expand All @@ -86,8 +87,32 @@ jobs:
name: windows-library
path: crypto-jni\target\release\crypto_jni.dll

build-aar-library:
runs-on: ubuntu-22.04
steps:
# Setup rust
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.69
# Checkout the code
- name: Checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: Make android native dependencies
run: make add-android-targets && make android
# Builds and tests the sdk
- name: Build and test android sdk
run: cd concordium-android-sdk && mvn --batch-mode --update-snapshots install
- name: Upload aar
uses: actions/upload-artifact@master
with:
name: concordium-android-sdk.aar
path: ./concordium-android-sdk/target/concordium-android-sdk.aar

build-and-release-jar:
needs: [build-native-ubuntu, build-native-macos, build-native-windows]
needs: [build-native-ubuntu, build-native-macos, build-native-windows, build-aar-library]
# Use fixed OS version because we install packages on the system.
runs-on: ubuntu-22.04
environment: release
Expand Down Expand Up @@ -125,6 +150,12 @@ jobs:
name: windows-library
path: concordium-sdk/native

- name: Download aar library
uses: actions/download-artifact@master
with:
name: concordium-android-sdk.aar
path: concordium-android-sdk/target

# Builds and tests the sdk. Delomboks code and generates a javadoc jar from the delombok'ed code
- name: Build and test sdk
run: cd concordium-sdk && mvn --batch-mode --update-snapshots install && mvn lombok:delombok -f pom.xml && mvn javadoc:jar -f pom.xml
Expand All @@ -133,7 +164,9 @@ jobs:
- name: Release
uses: softprops/action-gh-release@v1
with:
files: concordium-sdk/target/*.jar
files: |
concordium-sdk/target/*.jar
concordium-android-sdk/target/concordium-android-sdk.aar
- name: Deploy javadoc
uses: MathieuSoysal/[email protected]
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Unreleased changes
- Purge remaining usages of V1 GRPC API.

- Added support for android through an AAR artifact.

## 6.0.0
- Added method `waitUntilFinalized` for waiting until a given transaction is finalized.
- Removed deprecated V1 API from the SDK. Consumers of the (now removed) `getBlockSummary` endpoint should refer to `GetBlockTransactionEvents`, `GetBlockSpecialEvents` and `GetBlockPendingUpdates`.
Expand Down
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ PATH_CRYPTO_TARGET = $(PATH_CRYPTO)target/
PATH_JAVA_SDK := concordium-sdk/
PATH_JAVA_NATIVE_RESOURCES := $(PATH_JAVA_SDK)native/

PATH_ANDROID_SDK := concordium-android-sdk/
PATH_ANDROID_NATIVE_RESOURCES := $(PATH_ANDROID_SDK)native/

OS := $(shell uname -s)

ifeq ($(OS),Darwin)
Expand All @@ -23,5 +26,30 @@ all:
cp $(PATH_CRYPTO_TARGET)release/crypto_jni.dll $(PATH_JAVA_NATIVE_RESOURCES)
endif

MIN_ANDROID_VER := "26"

define android-command
mkdir -p $(PATH_ANDROID_NATIVE_RESOURCES)$(2)
cd $(PATH_CRYPTO) && cargo ndk --target $(1) --platform $(MIN_ANDROID_VER) -- build --release
cp $(PATH_CRYPTO_TARGET)$(1)/release/libcrypto_jni.so $(PATH_ANDROID_NATIVE_RESOURCES)$(2)/
endef

add-android-targets:
cargo install --version 3.4.0 cargo-ndk --locked
rustup target add aarch64-linux-android
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android


android:
mkdir -p $(PATH_ANDROID_NATIVE_RESOURCES)
$(call android-command,aarch64-linux-android,arm64-v8a)
$(call android-command,armv7-linux-androideabi,armeabi-v7a)
$(call android-command,x86_64-linux-android,x86_64)
$(call android-command,i686-linux-android,x86)


clean:
rm -rf $(PATH_JAVA_NATIVE_RESOURCES)*
rm -rf $(PATH_ANDROID_NATIVE_RESOURCES)*
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The Concordium Java SDK provides an interface to communicating with a Concordium
# Build and usage
To build the native library and put it into the correct folders all one has to do the following:

1. Clone this repositry and remember to update submodules, for example when cloning via
1. Clone this repository and remember to update git submodules, for example when cloning via
```bash
git clone https://github.com/Concordium/concordium-java-sdk.git --recurse-submodules
```
Expand Down Expand Up @@ -68,6 +68,38 @@ One can create a new header file by using the command: `javac -h . ED25519.java`

The output will be a header file, the contents hereof must be matched appropriately as described in the [documentation](https://docs.rs/jni/0.19.0/jni/) into the [lib.rs](crypto-jni/src/lib.rs) rust source file.

## Android
To build the android AAR package, one has to do the following:

1. Clone this repository and remember to update git submodules, for example when cloning via
```bash
git clone https://github.com/Concordium/concordium-java-sdk.git --recurse-submodules
```
2. Set the ANDROID_HOME environment variable to the path to your Android SDK installation.
3. Run `make add-android-targets` from the root of this repository.
4. Run `make android` from the root of this repository.
5. Run `mvn install` from the root of the [concordium-android-sdk](./concordium-android-sdk) folder.

`make add-android-targets` adds the rust targets that the native libraries will be built for.
`make android` builds the native libraries for various targets using cargo-ndk.
And the final step builds the aar file `concordium-android-sdk.aar` in the [target](./concordium-android-sdk/target) folder, which can be used in android projects.
Note that this uses the [Android Maven Plugin](http://simpligility.github.io/android-maven-plugin/), which is what requires the Android SDK, the specific version can be seen in its documentation.

For the library to work on android, the following packages must be added to the project (Shown in the gradle style)
```gradle
dependencies {
// gRPC
implementation 'io.grpc:grpc-okhttp:1.60.0'
implementation 'io.grpc:grpc-protobuf:1.40.1'
implementation 'io.grpc:grpc-stub:1.60.0'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
implementation 'com.fasterxml.jackson.core:jackson-core:2.10.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.10.1'
}
```

Note that the minimum android SDK version for this package is 26.

# Usage
The [`ClientV2`](./concordium-java-sdk/blob/main/concordium-sdk/src/main/java/com/concordium/sdk/ClientV2.java) is the main entrypoint for the SDK.
Expand Down
5 changes: 5 additions & 0 deletions concordium-android-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
*.iml
/target
/native/*
/delombok
8 changes: 8 additions & 0 deletions concordium-android-sdk/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.concordium.sdk"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="27" />
</manifest>
Loading

0 comments on commit 31c2c29

Please sign in to comment.