app/internal/headless: [Android] expose Java initialization method

Android apps may want to run Go code independent of the Gio Android Activity.
Expose a Gio.init Java method public for early loading and initialization of
the Go library.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-05 19:10:19 +02:00
parent cde651db60
commit 33493501a6
3 changed files with 42 additions and 22 deletions
+39
View File
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: Unlicense OR MIT
package org.gioui;
import android.content.Context;
import java.io.UnsupportedEncodingException;
public class Gio {
private final static Object initLock = new Object();
private static boolean jniLoaded;
/**
* init loads and initializes the Go native library and runs
* the Go main function.
*
* It is exported for use by Android apps that need to run Go code
* outside the lifecycle of the Gio activity.
*/
public static synchronized void init(Context appCtx) {
synchronized (initLock) {
if (jniLoaded) {
return;
}
String dataDir = appCtx.getFilesDir().getAbsolutePath();
byte[] dataDirUTF8;
try {
dataDirUTF8 = dataDir.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
System.loadLibrary("gio");
runGoMain(dataDirUTF8, appCtx);
jniLoaded = true;
}
}
static private native void runGoMain(byte[] dataDir, Context context);
}