2021-12-17 18:09:10 +01:00
|
|
|
|
# Building mobile libraries
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
## Prerequisites
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
### Go language
|
|
|
|
|
|
|
|
|
|
- [Install](https://github.com/lightningnetwork/lnd/blob/master/docs/INSTALL.md#preliminaries-for-installing-from-source) `GO` language.
|
|
|
|
|
- Provide `$GOPATH` to `.zshrc` or `.bash_profile` files.
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
export GOPATH=$HOME/go
|
|
|
|
|
export PATH=$PATH:$GOPATH/bin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Or any path you want it to be in.
|
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
### Docker
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
- Install and run [Docker](https://www.docker.com/products/docker-desktop).
|
|
|
|
|
|
|
|
|
|
### Make
|
|
|
|
|
|
|
|
|
|
- Check that `make` is available by running the following command without errors:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ make --version
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Go mobile
|
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
- Install [gomobile](https://github.com/golang/go/wiki/Mobile):
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ go install golang.org/x/mobile/cmd/gomobile@latest
|
2022-01-04 20:42:11 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- Install `gobind`
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ go install golang.org/x/mobile/cmd/gobind@latest
|
2021-12-17 18:09:10 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Building the libraries
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
Note that `gomobile` only supports building projects from `GOPATH` at this point. So, before continuing, be sure to be in the `src` folder:
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ cd $GOPATH/src/github.com/lightningnetwork/lnd
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To checkout the latest tagged release of lnd, run
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-01-17 14:59:23 +01:00
|
|
|
|
```shell
|
2021-12-17 18:09:10 +01:00
|
|
|
|
⛰ git checkout $(git describe --tags --abbrev=0)
|
2019-07-09 15:36:56 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
Or, the second option: just clone the project and checkout to any branch/tag (pay attention to the dot in the end).
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ git clone https://github.com/lightningnetwork/lnd .
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### For Android:
|
|
|
|
|
|
|
|
|
|
Move to the folder or create one:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ cd $GOPATH/src/golang.org/x
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
After that clone the goland mobile repo
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ git clone https://github.com/golang/mobile
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-09 15:36:56 +02:00
|
|
|
|
### Building `lnd` for iOS
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
2021-01-17 14:59:23 +01:00
|
|
|
|
```shell
|
|
|
|
|
⛰ make ios
|
2019-07-09 15:36:56 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Building `lnd` for Android
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
Go to `$GOPATH/src/github.com/lightningnetwork/lnd` and run the command below (make sure that the Docker engine is running):
|
|
|
|
|
|
2021-01-17 14:59:23 +01:00
|
|
|
|
```shell
|
|
|
|
|
⛰ make android
|
2019-07-09 15:36:56 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
`make mobile` will build both iOS and Android libraries.
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
|
|
|
|
### Libraries
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
2019-11-21 12:35:52 +01:00
|
|
|
|
After the build has succeeded, the libraries will be found in
|
2021-12-17 18:09:10 +01:00
|
|
|
|
`mobile/build/ios/Lndmobile.xcframework` and
|
2019-11-21 12:35:52 +01:00
|
|
|
|
`mobile/build/android/Lndmobile.aar`. Reference your platforms' SDK
|
|
|
|
|
documentation for how to add the library to your project.
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
## Generating proto definitions
|
|
|
|
|
|
2019-11-21 12:35:52 +01:00
|
|
|
|
In order to call the methods in the generated library, the serialized proto for
|
|
|
|
|
the given RPC call must be provided. Similarly, the response will be a
|
|
|
|
|
serialized proto.
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
### iOS
|
2021-10-26 14:48:17 +04:00
|
|
|
|
|
|
|
|
|
In order to generate protobuf definitions for iOS, add `--swift_out=.` to the
|
2021-12-17 18:09:10 +01:00
|
|
|
|
first `protoc` invocation found in [ `gen_protos.sh` ](../lnrpc/gen_protos.sh).
|
2021-10-26 14:48:17 +04:00
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
Then, some changes to [Dockerfile](../lnrpc/Dockerfile) need to be done in
|
2021-10-26 14:48:17 +04:00
|
|
|
|
order to use the [Swift protobuf](https://github.com/apple/swift-protobuf)
|
|
|
|
|
plugin with protoc:
|
|
|
|
|
|
|
|
|
|
1. Replace the base image with `FROM swift:focal` so that Swift can be used.
|
|
|
|
|
2. `clang-format='1:7.0*'` is unavailable in Ubuntu Focal. Change that to
|
|
|
|
|
`clang-format='1:10.0*`.
|
|
|
|
|
3. On the next line, install Go and set the environment variables by adding the
|
|
|
|
|
following commands:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
RUN apt-get install -y wget \
|
2021-12-17 18:09:10 +01:00
|
|
|
|
&& wget -c https://golang.org/dl/go1.17.3.linux-amd64.tar.gz -O - \
|
2021-10-26 14:48:17 +04:00
|
|
|
|
| tar -xz -C /usr/local
|
|
|
|
|
ENV GOPATH=/go
|
|
|
|
|
ENV PATH=$PATH:/usr/local/go/bin:/go/bin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
4. At the end of the file, just above `CMD`, add the following `RUN` command.
|
|
|
|
|
This will download and compile the latest tagged release of Swift protobuf.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
RUN git clone https://github.com/apple/swift-protobuf.git \
|
|
|
|
|
&& cd swift-protobuf \
|
|
|
|
|
&& git checkout $(git describe --tags --abbrev=0) \
|
|
|
|
|
&& swift build -c release \
|
|
|
|
|
&& mv .build/release/protoc-gen-swift /bin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Finally, run `make rpc`.
|
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
Tip: The generated Swift files will be found in various folders. If you’d like
|
|
|
|
|
to move them to the same folder as the framework file, run
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
⛰ `find . -name "*.swift" -print0 | xargs -0 -I {} mv {} mobile/build/ios`.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`Lndmobile.xcframework` and all Swift files should now be added to your Xcode
|
|
|
|
|
project. You will also need to add [Swift Protobuf](https://github.com/apple/swift-protobuf)
|
|
|
|
|
to your project to support the generated code.
|
|
|
|
|
|
|
|
|
|
### Android
|
2021-10-26 14:48:17 +04:00
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
#### First option:
|
|
|
|
|
|
2021-10-26 14:48:17 +04:00
|
|
|
|
In order to generate protobuf definitions for Android, add `--java_out=.`
|
2021-12-17 18:09:10 +01:00
|
|
|
|
|
2021-10-26 14:48:17 +04:00
|
|
|
|
to the first `protoc` invocation found in
|
2021-12-17 18:09:10 +01:00
|
|
|
|
[ `gen_protos.sh` ](../lnrpc/gen_protos.sh). Then, run `make rpc`.
|
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
|
|
|
|
|
#### Second option (preferable):
|
|
|
|
|
|
|
|
|
|
- You have to install the profobuf plugin to your Android application.
|
|
|
|
|
Please, follow this link https://github.com/google/protobuf-gradle-plugin.
|
|
|
|
|
- Add this line to your `app build.gradle` file.
|
|
|
|
|
```shell
|
|
|
|
|
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.17"
|
|
|
|
|
```
|
|
|
|
|
- Create a `proto` folder under the `main` folder.
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
- Add `aar` file to libs folder.
|
|
|
|
|
|
|
|
|
|
- After that add these lines to your `module's` `build.gradle` file:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
plugins {
|
|
|
|
|
id "com.google.protobuf"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
android {
|
|
|
|
|
sourceSets {
|
|
|
|
|
main {
|
|
|
|
|
proto {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dependencies {
|
|
|
|
|
implementation fileTree(dir: "libs", include: ["*.jar"])
|
|
|
|
|
implementation "com.google.protobuf:protobuf-javalite:${rootProject.ext.javalite_version}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protobuf {
|
|
|
|
|
protoc {
|
|
|
|
|
artifact = "com.google.protobuf:protoc:${rootProject.ext.protoc_version}"
|
|
|
|
|
}
|
|
|
|
|
generateProtoTasks {
|
|
|
|
|
all().each { task ->
|
|
|
|
|
task.builtins {
|
|
|
|
|
java {
|
|
|
|
|
option "lite"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- Then, copy all the proto files from `lnd/lnrpc` to your `proto` folder, saving the structure.
|
|
|
|
|
- Build the project and wait until you see the generated Java proto files in the `build` folder.
|
|
|
|
|
|
|
|
|
|
#### Note:
|
|
|
|
|
|
|
|
|
|
If Android Studio tells you that the `aar` file cannot be included into the `app-bundle`, this is a workaround:
|
|
|
|
|
|
|
|
|
|
1. Create a separate gradle module
|
|
|
|
|
2. Remove everything from there and leave only `aar` and `build.gradle`.
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
3. Gradle file should countain only these lines:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
configurations.maybeCreate("default")
|
|
|
|
|
artifacts.add("default", file('Lndmobile.aar'))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
4. In `dependencies` add this line instead of depending on `libs` folder:
|
|
|
|
|
```shell
|
|
|
|
|
implementation project(":lndmobile", { "default" })
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
## Options
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
Similar to lnd, subservers can be conditionally compiled with the build by setting the tags argument:
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-01-17 14:59:23 +01:00
|
|
|
|
```shell
|
|
|
|
|
⛰ make ios
|
2019-07-09 15:36:56 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-01-04 20:42:11 +02:00
|
|
|
|
To support subservers that have APIs with name conflicts, pass the "prefix" flag. This will add the subserver name as a prefix to each method name:
|
2019-07-09 15:36:56 +02:00
|
|
|
|
|
2021-01-17 14:59:23 +01:00
|
|
|
|
```shell
|
|
|
|
|
⛰ make ios prefix=1
|
2019-07-09 15:36:56 +02:00
|
|
|
|
```
|
|
|
|
|
|
2021-12-17 18:09:10 +01:00
|
|
|
|
## API docs
|
|
|
|
|
|
|
|
|
|
[LND gRPC API Reference](https://api.lightning.community)
|
|
|
|
|
|
2019-07-09 15:36:56 +02:00
|
|
|
|
TODO(halseth)
|