Recently, we released support for native Android development for SAFE using a new Java API and we’re excited to see applications from the community!
Java, being platform independent, will run on any platform and since the recent release targeted only Android an interesting question arises, “What about desktop?”
The Java API are indeed supported on desktop too. But however, we are looking into certain aspects of packaging (following maven standards) and some IPC requirements before official release.
Nonetheless, by following the instructions below you can get started with the Java API on desktop. Note that these are not yet finalized and are likely to change.
Building the Java libraries:
The java libs for desktop can be built from the safe_app_java repository. The desktop libraries can be built via the following commands:
git clone https://github.com/maidsafe/safe_app_java chmod +x gradlew gradlew safe-app:download-nativelibs
Note: The above command will throw an error in case of missing Android SDKs due to the multi-module structure of safe_app_java. If this is the case for you run the following commands:
mkdir -p ~/.android/licenses export $ANDROID_HOME=~/.android echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > $ANDROID_HOME/licenses/android-sdk-license echo "d56f5187479451eabf01fb78af6dfcb131a6481e" >> $ANDROID_HOME/licenses/android-sdk-license
In other words, create a folder and set it’s path to the
ANDROID_HOME environment variable and create a file with the following content at
gradle :safe-app:download-nativelibs again.
This command will download all the native libraries that are required. You can now build the java libraries using the following command
This command will build the java libraries and output 6 JAR files in the
safe-app-0.1.0-linux-x64 safe-app-0.1.0-osx-x64 safe-app-0.1.0-win-x64 safe-app-mock-0.1.0-linux-x64 safe-app-mock-0.1.0-osx-x64 safe-app-mock-0.1.0-win-x64
Each of these JAR files will contain the required classes and native libraries for the specific platform and variant (mock / non-mock). You can now include these libraries into your project and use the SAFE APIs
For more information about the APIs check out this android tutorial and the API documentation.
Note: All the 6 JARs contain the same class files so adding more than one of the above libraries will throw an error.
Authentication with the SAFE browser
The application can authenticate to the SAFE network through the SAFE browser. You can create an auth request by following the authentication section of the tutorial. The only difference on desktop would be that the URL format to send the request to the authenticator would be:
On opening this URI the auth request will be sent to the authenticator in the SAFE browser.
After authentication, the browser will send the auth response back to the application through a custom URI registered for the application in the following format:
The application will need to register itself to open URLs of this type and use the auth response to connect to the network. This URI registration can be done across platforms using the system_uri crate.
With some experimentation I was able to create Java bindings for this crate using jnr-ffi as a proof of concept. The code is available in this repository. A lot more is to be done there in terms of error handling across the FFI but it’s a start You can download the built jars (with native libs) from the releases.
Example CLI application
To demonstrate the authentication process with the SAFE browser I have created a simple Java CLI application which uses the SAFE browser to authenticate and connect to the network. The code is available is this GitHub repository.
Assuming you are on linux, to run the application on the mock network:
- Download the latest mock browser from the GitHub releases
- Clone the repository
- Create a
libsfolder in the repository’s root and copy the built
safe-app-mock-0.1.0-linux-x64.jarfile from the
safe_app_java/safe-app/build/libsfolder. You will also need the
system_uri-0.0.1-linux-x64.jarfile from this release.
- Now run the following command:
This will generate a
java-mock-0.0.1-linux-x64.jar file in the
build/libs folder. This executable JAR is packaged along with the
system-uri packages. Now open the downloaded mock browser, create an account and login. On executing the CLI application via:
java -jar <path/to/executable.jar>
an auth request will be sent to the SAFE browser and on authentication the response will be sent back to the CLI application.
Now that you are connected to the network you can perform network operations of your choice by referring the rest of the tutorial or the API docs
OS X Limitation:
Unfortunately, we cannot map an executable JAR to a custom URI on OS X. It needs to be packaged as an application with an
Info.plist file. So this CLI is limited to Windows and Linux(for now). More details about this issue on this thread.