|
| 1 | +# How to use SDK in Android app |
| 2 | + |
| 3 | +## Root project `build.gradle` |
| 4 | + |
| 5 | +Add Maven Central and Jitpack.io repositories into `buildscript/repositories` |
| 6 | +section. Maven Central is used to get Gradle Protobuf plugin. Jitpack.io repo |
| 7 | +contains SingularityNet SDK dependencies. |
| 8 | +``` |
| 9 | + mavenCentral() |
| 10 | + maven { |
| 11 | + url 'https://jitpack.io' |
| 12 | + } |
| 13 | +``` |
| 14 | + |
| 15 | +Add SingularityNet and Protobuf plugins into the classpath using |
| 16 | +`buildscript/dependencies` section: |
| 17 | +``` |
| 18 | + classpath 'io.singularitynet:snet-sdk-gradle-plugin:1.0-SNAPSHOT' |
| 19 | + classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10' |
| 20 | +``` |
| 21 | + |
| 22 | +## Android app `build.gradle` script |
| 23 | + |
| 24 | +Apply SingularityNet SDK and Protobuf plugins. |
| 25 | +``` |
| 26 | +apply plugin: 'io.singularitynet.sdk' |
| 27 | +apply plugin: 'com.google.protobuf' |
| 28 | +``` |
| 29 | + |
| 30 | +Add Jitpack.io repository before `dependencies` section to get SingularityNet |
| 31 | +Java SDK dependencies. |
| 32 | +``` |
| 33 | +repositories { |
| 34 | + maven { |
| 35 | + url 'https://jitpack.io' |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Add SingularityNet Java SDK into `dependencies` section: |
| 41 | +``` |
| 42 | +implementation 'io.grpc:grpc-okhttp:1.20.0' |
| 43 | +implementation 'org.slf4j:slf4j-android:1.7.30' |
| 44 | +implementation 'io.singularitynet:snet-sdk-java:1.0-SNAPSHOT' |
| 45 | +``` |
| 46 | + |
| 47 | +Add one Gradle task to download and unpack each SingularityNet service API you |
| 48 | +are going to use. You should provide at least `orgId`, `serviceId` to specify |
| 49 | +service. `javaPackage` to set convenient package for Java files to be |
| 50 | +generated. `outputDir` to write resulting protobuf files. |
| 51 | +``` |
| 52 | +tasks.register('getExampleServiceApi', io.singularitynet.sdk.gradle.GetSingularityNetServiceApi) { |
| 53 | + orgId = 'snet' |
| 54 | + serviceId = 'example-service' |
| 55 | + javaPackage = 'io.singularitynet.service.exampleservice' |
| 56 | + outputDir = file("$buildDir/proto") |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Add dir which was used to unpack service API protobuf on the previous step into |
| 61 | +set of source dirs. Turn on support of Java 8 features. |
| 62 | +``` |
| 63 | +android { |
| 64 | + sourceSets { |
| 65 | + main { |
| 66 | + proto { |
| 67 | + srcDir "$buildDir/proto" |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + compileOptions { |
| 72 | + sourceCompatibility = 1.8 |
| 73 | + targetCompatibility = 1.8 |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Use Protobuf compiler with gRPC plugin to compile SingularityNet service API |
| 79 | +into Java code. gRPC and Protobuf versions below are recommended as they were |
| 80 | +used to compile SingularityNet Java SDK. |
| 81 | +``` |
| 82 | +def grpcVersion = '1.20.0' |
| 83 | +def protobufVersion = '3.5.1' |
| 84 | +def protocVersion = protobufVersion |
| 85 | +
|
| 86 | +protobuf { |
| 87 | + protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" } |
| 88 | + plugins { |
| 89 | + grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } |
| 90 | + } |
| 91 | + generateProtoTasks { |
| 92 | + all()*.builtins { remove java } |
| 93 | + all()*.plugins { |
| 94 | + grpc {} |
| 95 | + java {} |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
0 commit comments