Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android support milestone2 #73

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4ef2d34
Merge branch 'master' into android_support_milestone2
nschwermann Sep 6, 2021
9d6127b
add android aar artifact for schnorrkel supporting arm, arm64, x86 an…
nschwermann Sep 8, 2021
a8259e8
publish android artifact
nschwermann Sep 10, 2021
e881bc8
add missing test impl
nschwermann Sep 10, 2021
811323d
remove java 9 api
nschwermann Sep 11, 2021
bb710c2
Add friendly dependency reminder
nschwermann Sep 11, 2021
d984310
Merge branch 'master' into android_support_milestone2
nschwermann Sep 11, 2021
83449c9
remove java 9 and 11 apis
nschwermann Sep 11, 2021
6fcd5f7
Add okhttp rpc adaper
nschwermann Sep 13, 2021
1a61420
fix test
nschwermann Sep 13, 2021
7301ace
fix parameter list and improve test coverage
nschwermann Sep 15, 2021
c76208e
add android platform tests
nschwermann Sep 17, 2021
013c8cb
use v2 java setup
nschwermann Sep 17, 2021
dd8fa02
remove java 12 api
nschwermann Sep 17, 2021
8656daa
add java 8 tests
nschwermann Sep 17, 2021
6870051
resolve github action errors
nschwermann Sep 17, 2021
31ddfcc
remove java 9 api
nschwermann Sep 17, 2021
c4d276d
fix CI issues
nschwermann Sep 18, 2021
315c66a
more ci fixes
nschwermann Sep 19, 2021
b16f352
add java 8 target to compile options
nschwermann Sep 19, 2021
f035094
more CI fixes
nschwermann Sep 19, 2021
8de4767
remove release target and add local properties file to ci
nschwermann Sep 19, 2021
dc7af2d
remove java 9 api from test
nschwermann Sep 19, 2021
4e0f4ac
remove api 24 arm test from github it can not connect to the emulator
nschwermann Sep 19, 2021
30ec4b0
remove unsupported test configuration
nschwermann Sep 19, 2021
38d0f59
Merge branch 'master' into android_support_milestone2
nschwermann Sep 19, 2021
762093b
use google_apis instead of default target for api 30 testing default …
nschwermann Sep 19, 2021
d5f7825
fix invalid action syntax
nschwermann Sep 19, 2021
575ac22
update matrix config
nschwermann Sep 19, 2021
fe61cae
remove failing exclude
nschwermann Sep 19, 2021
ce6a001
grokking the matrix better
nschwermann Sep 19, 2021
63963d6
make target an array
nschwermann Sep 19, 2021
1cc6f45
correct missing arch
nschwermann Sep 19, 2021
db9648b
make all android tests on google api targets
nschwermann Sep 19, 2021
da1434a
fix build script was still using default instead of google apis
nschwermann Sep 19, 2021
f73e0dc
okhttp websockets
nschwermann Sep 21, 2021
f0be3ca
add test coverage
nschwermann Sep 25, 2021
b709287
Increase code coverage
nschwermann Sep 28, 2021
b71e5aa
update documentation and examples
nschwermann Oct 5, 2021
68ed443
fix race condition could occur when causing first websocket response …
nschwermann Oct 5, 2021
546b38e
undo always run tests
nschwermann Oct 5, 2021
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: 2 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:
with:
name: schnorrkel-lib
path: polkaj-schnorrkel/build/rust/release
- name: Setup Rust for Android
run: ./cargo_ndk_prep.sh; touch local.properties
- name: Check
uses: eskatos/gradle-command-action@v1
with:
Expand Down
57 changes: 54 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ jobs:
- uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v1
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 11

- name: Check
Expand All @@ -38,6 +39,24 @@ jobs:
with:
file: ./build/reports/jacoco/coverageReport/coverageReport.xml

# Run tests for java 8 targets
java8-test:
name: Java 8 Test
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 8

- name: Check
uses: eskatos/gradle-command-action@v1
with:
arguments: checkJava8

# Make sure it works with all standard JVMs on main OSes
platform-test:
name: Java ${{ matrix.java }} on ${{ matrix.os }}
Expand All @@ -51,8 +70,9 @@ jobs:
- uses: actions/checkout@v2

- name: Set up JDK
uses: actions/setup-java@v1
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: ${{ matrix.java }}

- name: Check
Expand All @@ -62,6 +82,33 @@ jobs:
wrapper-cache-enabled: false
arguments: check

#Test with android emulators provided
android-platform-test:
runs-on: macos-latest
strategy:
matrix:
api-level: [24, 25, 26, 28, 29, 30]
arch: [x86_64, x86]
# include:
# - api-level: 30
# arch: arm64-v8a ## Currently this github action isn't able to start any arm emulators

steps:
- name: checkout
uses: actions/checkout@v2

- name: Setup Rust
run: ./cargo_ndk_prep.sh; touch local.properties

- name: run tests
uses: reactivecircus/android-emulator-runner@v2
with:
# ndk: 21.4.7075529 ## This would install the correct NDK version, but it's already installed in the image.
api-level: ${{ matrix.api-level }}
arch: ${{ matrix.arch }}
target: google_apis
script: ./gradlew connectedCheck

# Formatter may behave differently on different locales, makes sure tests are not failing
locale-test:
name: Locale ${{ matrix.locale }}
Expand All @@ -73,14 +120,18 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Setup Rust
run: ./cargo_ndk_prep.sh

- name: Set Locale
run: |
sudo locale-gen ${{ matrix.locale }}
sudo update-locale LANG=${{ matrix.locale }}

- name: Set up JDK
uses: actions/setup-java@v1
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 11

- name: Check
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ scripts

.gradle
build

local.properties
8 changes: 5 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ WARNING: UNDER DEVELOPMENT
- `io.emeraldpay.polkaj:polkaj-scale:{lib-version}` - SCALE codec implementation
- `io.emeraldpay.polkaj:polkaj-scale-types:{lib-version}` - SCALE mapping for standard Polkadot types
- `io.emeraldpay.polkaj:polkaj-schnorrkel:{lib-version}` - Schnorrkel for Java
- `io.emeraldpay.polkaj:polkaj-schnorrkel-android:{lib-version-dev}` - Schnorrkel for Android
- `io.emeraldpay.polkaj:polkaj-ss58:{lib-version}` - SS58 codec to encode/decode addresses and pubkeys
- `io.emeraldpay.polkaj:polkaj-common-types:{lib-version}` - common types (Address, DotAmount, Hash256, etc)
- `io.emeraldpay.polkaj:polkaj-json-types:{lib-version}` - JSON RPC mapping to Java classes
- `io.emeraldpay.polkaj:polkaj-api-base:{lib-version}` - RPC base classes
- `io.emeraldpay.polkaj:polkaj-api-http:{lib-version}` - JSON RPC HTTP client
- `io.emeraldpay.polkaj:polkaj-api-ws:{lib-version}` - JSON RPC WebSocket client
- `io.emeraldpay.polkaj:polkaj-api-okhttp:{lib-version-dev}` - JSON RPC and WebSocket client using OkHttp
- `io.emeraldpay.polkaj:polkaj-tx:{lib-version}` - Storage access and Extrinsics

== Usage
Expand All @@ -34,7 +36,7 @@ To use development SNAPSHOT versions you need to install the library into the lo

.Install into local Maven
----
gradle install
./gradlew publishToMavenLocal
----

.Using with Gradle
Expand All @@ -48,7 +50,7 @@ repositories {
}

dependencies {
implementation 'io.emeraldpay.polkaj:polkaj-api-http:{lib-version}'
implementation 'io.emeraldpay.polkaj:polkaj-api-http:{lib-version-dev}'
}
----

Expand All @@ -61,7 +63,7 @@ See link:docs/[Documentation] in `./docs` directory, and a demonstration in `./e
.Show current finalized block
[source,java]
----
PolkadotHttpApi client = PolkadotApi.newBuilder()
PolkadotApi client = PolkadotApi.newBuilder()
.rpcCallAdapter(JavaHttpAdapter.newBuilder().build())
.build();
Future<Hash256> hashFuture = client.execute(
Expand Down
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
buildscript {

ext.kotlin_version = '1.5.30'
ext.kotlin_coroutine_version = '1.5.2'

repositories {
google()
maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
}
dependencies {
classpath 'com.netflix.nebula:gradle-aggregate-javadocs-plugin:3.0.1'
classpath "com.android.tools.build:gradle:4.2.2"
classpath "gradle.plugin.com.github.willir.rust:plugin:0.3.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand Down Expand Up @@ -29,7 +42,7 @@ allprojects {
}

jacoco {
toolVersion = "0.8.5"
toolVersion = "0.8.7"
}

task coverageReport(type: JacocoReport) {
Expand All @@ -48,6 +61,8 @@ task syncJars(type: Sync) {
into "${buildDir}/libs/"
}

task checkJava8

// Skip Bintray for the root module
bintray {
dryRun=true
Expand Down
3 changes: 3 additions & 0 deletions cargo_ndk_prep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
cargo install cargo-ndk
14 changes: 11 additions & 3 deletions common_java_app.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'groovy'
apply plugin: 'jacoco'
Expand All @@ -10,13 +9,22 @@ afterEvaluate {
rootProject.tasks.coverageReport.additionalSourceDirs.setFrom rootProject.tasks.coverageReport.additionalSourceDirs.files + files(it.sourceSets.main.allSource.srcDirs)
rootProject.tasks.coverageReport.sourceDirectories.setFrom rootProject.tasks.coverageReport.sourceDirectories.files + files(it.sourceSets.main.allSource.srcDirs)
rootProject.tasks.coverageReport.classDirectories.setFrom rootProject.tasks.coverageReport.classDirectories.files + files(it.sourceSets.main.output)
}

//By default we set to java 8 but some project override this to higher levels so after evaluate we
if(java.sourceCompatibility == JavaVersion.VERSION_1_8){
rootProject.tasks.checkJava8.dependsOn(test)
}
}

tasks.withType(JavaCompile) {
options.debug = true
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

compileJava {
targetCompatibility = '8'
sourceCompatibility = '8'
Expand Down Expand Up @@ -79,7 +87,7 @@ artifacts {
}

jacoco {
toolVersion = "0.8.5"
toolVersion = "0.8.7"
}

publishing {
Expand Down
12 changes: 9 additions & 3 deletions examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ cd types
== RPC

Examples for accessing JSON RPC through HTTP or WebSockets.
The examples expect Polkadot node running and listening for RPC and WS requests on the localhost (i.e. default node options)
The examples expect Polkadot node running and listening for RPC and WS requests on the localhost (i.e. default node options). In any of the following examples you can pass the optional `--args="okhttp"` to the run command to use the Java 8 compatible OkHttp implementation of the RPC and WebSocket adapters.

.Show current head
----
cd rpc
./gradlew run
./gradlew run --args="okhttp"
----

.Follow updates to the current head (use `Ctrl+C` to exit)
----
cd rpc
./gradlew run -PmainClass=FollowState
./gradlew run -PmainClass=FollowState --args="okhttp"
----

.Describe runtime
Expand All @@ -45,6 +45,9 @@ cd rpc
./gradlew run -PmainClass=DescribeRuntime
----

NOTE: Currently, only runtimes with Metadata are is supported https://github.com/paritytech/polkadot/releases/tag/v0.8.30[v0.8.30] is suitable version to test with.


== Runtime Explorer

A web-based explorer of the Runtime Metadata.
Expand Down Expand Up @@ -78,6 +81,9 @@ cd balance

NOTE: To run a development network use: `polkadot --dev`

NOTE: Currently, only runtimes with Metadata are is supported https://github.com/paritytech/polkadot/releases/tag/v0.8.30[v0.8.30] is suitable version to test with.


.Transfer using real network (ex. Kusama)
----
cd balance
Expand Down
1 change: 1 addition & 0 deletions examples/balance/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ apply from: '../common.gradle'
dependencies {
implementation "io.emeraldpay.polkaj:polkaj-api-http:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-api-ws:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-api-okhttp:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-json-types:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-scale-types:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-tx:$polkajVersion"
Expand Down
9 changes: 8 additions & 1 deletion examples/balance/src/main/java/Balance.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import io.emeraldpay.polkaj.api.PolkadotApi;
import io.emeraldpay.polkaj.api.RpcCallAdapter;
import io.emeraldpay.polkaj.apihttp.JavaHttpAdapter;
import io.emeraldpay.polkaj.apiokhttp.OkHttpRpcAdapter;
import io.emeraldpay.polkaj.scaletypes.AccountInfo;
import io.emeraldpay.polkaj.tx.AccountRequests;
import io.emeraldpay.polkaj.types.Address;
import io.emeraldpay.polkaj.types.DotAmount;
import io.emeraldpay.polkaj.types.DotAmountFormatter;

import java.util.Arrays;

public class Balance {

public static void main(String[] args) throws Exception {
try (PolkadotApi client = PolkadotApi.newBuilder().rpcCallAdapter(JavaHttpAdapter.newBuilder().build()).build()) {
final boolean useOkhttp = Arrays.asList(args).contains("okhttp");
final RpcCallAdapter adapter = useOkhttp ? OkHttpRpcAdapter.newBuilder().build() :
JavaHttpAdapter.newBuilder().build();
try (PolkadotApi client = PolkadotApi.newBuilder().rpcCallAdapter(adapter).build()) {
DotAmountFormatter formatter = DotAmountFormatter.autoFormatter();

DotAmount total = AccountRequests.totalIssuance().execute(client).get();
Expand Down
12 changes: 10 additions & 2 deletions examples/balance/src/main/java/Transfer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io.emeraldpay.polkaj.api.*;
import io.emeraldpay.polkaj.apiokhttp.OkHttpSubscriptionAdapter;
import io.emeraldpay.polkaj.apiws.JavaHttpSubscriptionAdapter;
import io.emeraldpay.polkaj.scale.ScaleExtract;
import io.emeraldpay.polkaj.scaletypes.AccountInfo;
Expand Down Expand Up @@ -49,9 +50,16 @@ public static void main(String[] args) throws Exception {
Math.abs(random.nextLong()) % DotAmount.fromDots(0.002).getValue().longValue()
);

final JavaHttpSubscriptionAdapter adapter = JavaHttpSubscriptionAdapter.newBuilder().connectTo(api).build();
boolean useOkhttp = true;
final SubscriptionAdapter adapter = useOkhttp ?
OkHttpSubscriptionAdapter.newBuilder().connectTo(api).build() :
JavaHttpSubscriptionAdapter.newBuilder().connectTo(api).build();
try (PolkadotApi client = PolkadotApi.newBuilder().subscriptionAdapter(adapter).build()) {
System.out.println("Connected: " + adapter.connect().get());
if(adapter instanceof JavaHttpSubscriptionAdapter){
//Connect call not required for OkHttp
CompletableFuture<Boolean> connected = ((JavaHttpSubscriptionAdapter)adapter).connect();
System.out.println("Connected: " + connected.get());
}

// Subscribe to block heights
AtomicLong height = new AtomicLong(0);
Expand Down
3 changes: 2 additions & 1 deletion examples/rpc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ apply from: '../common.gradle'
dependencies {
implementation "io.emeraldpay.polkaj:polkaj-api-http:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-api-ws:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-api-okhttp:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-json-types:$polkajVersion"
implementation "io.emeraldpay.polkaj:polkaj-scale-types:$polkajVersion"
implementation 'commons-codec:commons-codec:1.14'
}

application {
mainClassName = project.hasProperty("mainClass") ? getProperty("mainClass") : "ShowState"
mainClassName = findProperty("mainClass") ?: "ShowState"
}
8 changes: 7 additions & 1 deletion examples/rpc/src/main/java/DescribeRuntime.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import io.emeraldpay.polkaj.api.PolkadotApi;
import io.emeraldpay.polkaj.api.RpcCallAdapter;
import io.emeraldpay.polkaj.api.StandardCommands;
import io.emeraldpay.polkaj.apihttp.JavaHttpAdapter;
import io.emeraldpay.polkaj.apiokhttp.OkHttpRpcAdapter;
import io.emeraldpay.polkaj.scale.ScaleExtract;
import io.emeraldpay.polkaj.scaletypes.Metadata;
import io.emeraldpay.polkaj.scaletypes.MetadataReader;

import java.util.Arrays;
import java.util.concurrent.Future;

/**
Expand All @@ -13,8 +16,11 @@
public class DescribeRuntime {

public static void main(String[] args) throws Exception {
final boolean useOkhttp = Arrays.stream(args).anyMatch(arg -> arg.equals("okhttp"));
final RpcCallAdapter adapter = useOkhttp ? OkHttpRpcAdapter.newBuilder().build() :
JavaHttpAdapter.newBuilder().build();
PolkadotApi api = PolkadotApi.newBuilder()
.rpcCallAdapter(JavaHttpAdapter.newBuilder().build())
.rpcCallAdapter(adapter)
.build();
Future<Metadata> metadataFuture = api.execute(StandardCommands.getInstance().stateMetadata())
.thenApply(ScaleExtract.fromBytesData(new MetadataReader()));
Expand Down
Loading