macOS static lib build target update, minor android/unity3d updates

This commit is contained in:
Joseph Henry
2016-12-20 12:57:59 -08:00
parent 7393c80ae6
commit 2741508520
27 changed files with 469 additions and 195 deletions

View File

@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.example.joseph.example_app"
minSdkVersion 15
@@ -22,7 +22,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'

View File

@@ -27,6 +27,7 @@ public class MainActivity extends AppCompatActivity {
String nwid = "8056c2e21c000001";
// Set up service
final ZTSDK zt = new ZTSDK();
final String homeDir = getApplicationContext().getFilesDir() + "/zerotier";
new Thread(new Runnable() {
public void run() {
@@ -133,8 +134,7 @@ public class MainActivity extends AppCompatActivity {
}
// UDP Echo ZTSDK
if(mode==4)
{
if(mode==4) {
// Remote server address (will be populated by recvfrom()
ZTAddress remoteServer = new ZTAddress();
ZTAddress bindAddr = new ZTAddress("0.0.0.0", 8080);
@@ -145,16 +145,15 @@ public class MainActivity extends AppCompatActivity {
int sock = zt.socket(ZTSDK.AF_INET, ZTSDK.SOCK_DGRAM, 0);
Log.d("ZTSDK", "binding...");
if((err = zt.bind(sock, bindAddr, nwid)) < 0)
if ((err = zt.bind(sock, bindAddr, nwid)) < 0)
Log.d("ZTSDK", "bind_err = " + err + "\n");
if((err = zt.listen(sock, 0)) < 0)
if ((err = zt.listen(sock, 0)) < 0)
Log.d("ZTSDK", "listen_err = " + err);
ArrayList<String> addresses = zt.get_addresses(nwid);
if(addresses.size() < 0) {
if (addresses.size() < 0) {
Log.d("ZTSDK", "unable to obtain ZT address");
return;
}
else {
} else {
Log.d("ZTSDK", "App IP = " + addresses.get(0));
}
@@ -164,10 +163,10 @@ public class MainActivity extends AppCompatActivity {
zt.fcntl(sock, ZTSDK.F_SETFL, ZTSDK.O_NONBLOCK);
// ECHO
while(true) {
while (true) {
// RX
if((err = zt.recvfrom(sock, buffer, 32, 0, remoteServer)) > 0) {
if ((err = zt.recvfrom(sock, buffer, 32, 0, remoteServer)) > 0) {
bufStr = new String(buffer).substring(0, err);
Log.d("ZTSDK", "read (" + err + ") bytes from " + remoteServer.Address() + " : " + remoteServer.Port() + ", msg = " + bufStr);

View File

@@ -5,68 +5,61 @@ Welcome!
Imagine a flat, encrypted, no-configuration LAN for all of the instances of your Android app. This short tutorial will show you how to enable ZeroTier functionality for your Android app with little to no code modification. Check out our [ZeroTier SDK](https://www.zerotier.com/blog) page for more info on how the integration works. In this example we aim to set up a minimal [Android Studio](https://developer.android.com/studio/index.html) project which contains all of the components necessary to enable ZeroTier for your app.
*NOTE: For Android JNI libraries to build you'll need to install [Android Studio](https://developer.android.com/studio/index.html) the [Android NDK](https://developer.android.com/ndk/index.html). Currently only Android NDK r10e is supported and can be found [here for OSX](http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip) and [here for Linux](http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip). You'll need to tell our project where you put it by putting the path in [this file](android/proj/local.properties), you'll need to install the Android Build-Tools (this can typically be done through the editor the first time you start it up), and finally you should probably upgrade your Gradle plugin if it asks you to. If you don't have these things installed and configured we will detect that and just skip those builds automatically.*
*NOTE: For Android JNI libraries to build you'll need to install [Android Studio](https://developer.android.com/studio/index.html) the [Android NDK](https://developer.android.com/ndk/index.html). Currently only Android NDK r10e is supported and can be found [here for OSX](http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip) and [here for Linux](http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip). You'll need to tell our project where you put it by putting the path in [this file](android_jni_lib/proj/local.properties), you'll need to install the Android Build-Tools (this can typically be done through the editor the first time you start it up), and finally you should probably upgrade your Gradle plugin if it asks you to. If you don't have these things installed and configured we will detect that and just skip those builds automatically.*
If you want to skip these steps and just take a look at the project, go [here](example_app).
If you want to skip the following steps and just take a look at the project, go [here](example_app).
***
**Step 1: Select build targets**
- Specify the target architectures you want to build in [Application.mk](android/java/jni/Application.mk). By default it will build `arm64-v8a`, `armeabi`, `armeabi-v7a`, `mips`, `mips64`, `x86`, and `x86_64`.
**Step 2: Build Shared Library**
- `make android_jni_lib`
- The resultant `build/android_jni_lib_YOUR_ARCH/libZeroTierOneJNI.so` is what you want to import into your own project to provide the API to your app. Select your architecture and copy the shared library into your project's JNI directory, possibly `/src/main/jniLibs/YOUR_ARCH/`. Selecting only the architectures you need will *significantly* reduce overall build time.
**Step 1: App Code Modifications**
- In your project, create a new package called `ZeroTier` and class file within called `ZTSDK.java` and copy contents from `src/wrappers/java/JavaWrapper.java`
- Start the service
**Step 3: App permissions**
```
String nwid = "8056c2e21c000001";
// Set up service
final ZTSDK zt = new ZTSDK();
final String homeDir = getApplicationContext().getFilesDir() + "/zerotier";
new Thread(new Runnable() {
public void run() {
// Calls to JNI code
zt.start_service(homeDir);
}
}).start();
while(!zt.running()) { }
```
- Join network and start doing network stuff!
```
zt.join_network(nwid);
int sock = zt.socket(zt.AF_INET, zt.SOCK_STREAM, 0);
int err = zt.connect(sock, "10.9.9.203", 8080, nwid);
// zt.recvfrom(), zt.write(), etc...
```
**Step 2: App permissions**
- In order for your application to write the auth keys and network files to the internal storage you'll need to set a few permissions in your `AndroidManifest.xml` file at the same scope level as `<application>`:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
**Step 4: App Code Modifications**
- Create new package called `ZeroTierSDK` in your project and add a new file called `ZeroTierSDK.java` containing:
**Step 3: Set NDK/SDK paths**
- Specify your SDK/NDK path in `android_jni_lib/proj/local.properties`. For example:
```
package ZeroTier;
public class ZeroTierSDK {
public native void startOneService(String homeDir);
public native void joinNetwork(String nwid);
public native void leaveNetwork(String nwid);
public native boolean isRunning();
static { System.loadLibrary("ZeroTierOneJNI"); } // Loads JNI code
}
```
```
sdk.dir=/Users/Name/Library/Android/sdk
ndk.dir=/Users/Name/Library/Android/ndk-r10e
```
- Start the service
**Step 4: Select build targets**
- Specify the target architectures you want to build in [Application.mk](android_jni_lib/java/jni/Application.mk). By default it will build `arm64-v8a`, `armeabi`, `armeabi-v7a`, `mips`, `mips64`, `x86`, and `x86_64`. For each architecture you wish to support a different shared library will need to be built. This is all taken care of automatically by the build script.
```
final SDK zt = new SDK();
final String homeDir = getApplicationContext().getFilesDir() + "/zerotier";
new Thread(new Runnable() {
public void run() {
// Calls to JNI code
zt.zt_start_service(homeDir);
}
}).start();
```
- Join network and perform network call
```
while(!zt.zt_running()) { }
zt.zt_join_network("XXXXXXXXXXXXXXXX");
// Create ZeroTier socket
int sock = zt.zt_socket(zt.AF_INET, zt.SOCK_STREAM, 0);
// Connect to remote host
int err = zt.zt_connect(sock, "10.9.9.203", 8080);
```
***
*Note for the curious on JNI naming conventions: In order to reference a symbol in the JNI library you need to structure the package and class in your Android Studio project in a very particular way. For example, in the ZeroTierSDK we define a function called `Java_ZeroTier_SDK_zt_1start_1service`, the name can be broken down as follows: `Java_PACKAGENAME_CLASSNAME_zt_1start_1service`, so as we've defined it, you must create a package called `ZeroTier` and add a class called `SDK`.*
**Step 4: Build Shared Library**
- `make android_jni_lib`
- The resultant `build/android_jni_lib/ARCH/libZeroTierOneJNI.so` is what you want to import into your own project to provide our API implementation to your app. Select your architecture and copy the shared library `libZeroTierOneJNI.so` into your project's JNI directory, possibly `/src/main/jniLibs/ARCH/libZeroTierOneJNI.so`.
- Selecting only the architectures you need will *significantly* reduce overall build time.

View File

@@ -1,11 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

View File

@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Thu Dec 15 15:58:49 PST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://downloads.gradle.org/distributions/gradle-2.14-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip