-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from thedilletante/android_multithread
[Android] Added newGuid() overloading to support multithread invocations
- Loading branch information
Showing
45 changed files
with
618 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build/ | |
*.swp | ||
android/libs/ | ||
android/obj/ | ||
cmake_build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
#!/usr/bin/env bash | ||
|
||
export LC_NUMERIC="en_US.UTF-8" | ||
|
||
pushd android | ||
ndk-build clean || { exit 1; } | ||
ndk-build || { exit 1; } | ||
ant debug || { exit 1; } | ||
./gradlew clean assembleDebug | ||
adb uninstall ca.graemehill.crossguid.testapp || { exit 1; } | ||
adb install bin/TestApp-debug.apk || { exit 1; } | ||
adb install app/build/outputs/apk/debug/app-debug.apk || { exit 1; } | ||
adb shell am start -n ca.graemehill.crossguid.testapp/ca.graemehill.crossguid.testapp.MainActivity | ||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
cmake_minimum_required(VERSION 3.4.1) | ||
|
||
set(LIB_NAME crossguidtest) | ||
set(XG_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) | ||
set(XG_TEST_DIR ${XG_DIR}/test) | ||
|
||
add_library(${LIB_NAME} SHARED src/main/cpp/jnitest.cpp ${XG_TEST_DIR}/Test.cpp) | ||
|
||
target_include_directories(${LIB_NAME} PRIVATE | ||
${XG_DIR} | ||
${XG_TEST_DIR}) | ||
|
||
target_compile_definitions(${LIB_NAME} PRIVATE GUID_ANDROID) | ||
|
||
set(XG_TESTS OFF CACHE BOOL "disable tests") | ||
add_subdirectory(${XG_DIR} ${XG_DIR}/cmake_build) | ||
|
||
target_link_libraries(${LIB_NAME} xg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 25 | ||
buildToolsVersion "26.0.0" | ||
defaultConfig { | ||
applicationId "ca.graemehill.crossguid.testapp" | ||
minSdkVersion 14 | ||
targetSdkVersion 25 | ||
versionCode 1 | ||
versionName "1.0" | ||
externalNativeBuild { | ||
cmake { | ||
cppFlags "-std=c++11 -frtti -fexceptions" | ||
arguments "-DANDROID_TOOLCHAIN=clang" | ||
} | ||
} | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
externalNativeBuild { | ||
cmake { | ||
path "CMakeLists.txt" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.0', { | ||
exclude group: 'com.android.support', module: 'support-annotations' | ||
}) | ||
implementation 'com.android.support:appcompat-v7:25.4.0' | ||
testImplementation 'junit:junit:4.12' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /usr/local/Cellar/android-sdk/24.4.1_1/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
14 changes: 9 additions & 5 deletions
14
android/AndroidManifest.xml → android/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <string> | ||
#include <sstream> | ||
#include <atomic> | ||
#include <iostream> | ||
|
||
#include <jni.h> | ||
#include "Guid.hpp" | ||
#include "Test.hpp" | ||
|
||
JavaVM *&javaVM() { | ||
static JavaVM *jvm; | ||
return jvm; | ||
} | ||
|
||
extern "C" | ||
{ | ||
|
||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void * /* reserved */) { | ||
javaVM() = jvm; | ||
return JNI_VERSION_1_6; | ||
} | ||
|
||
JNIEXPORT jstring JNICALL | ||
Java_ca_graemehill_crossguid_testapp_MainActivity_test( | ||
JNIEnv *env, jobject /*thiz*/) | ||
{ | ||
std::stringstream resultStream; | ||
xg::initJni(env); | ||
test(resultStream); | ||
return env->NewStringUTF(resultStream.str().c_str()); | ||
} | ||
|
||
JNIEXPORT jstring JNICALL | ||
Java_ca_graemehill_crossguid_testapp_MainActivity_newGuid( | ||
JNIEnv *env, jobject /*thiz*/) { | ||
return env->NewStringUTF(xg::newGuid(env).str().c_str()); | ||
} | ||
|
||
JNIEXPORT jstring JNICALL | ||
Java_ca_graemehill_crossguid_testapp_MainActivity_createGuidFromNativeThread( | ||
JNIEnv *env, jobject /*thiz*/) { | ||
|
||
// there is no promise<> in armeabi of ndk | ||
// so ugly atomic_bool wait solution | ||
std::atomic_bool ready { false }; | ||
std::string guid; | ||
|
||
std::thread([&ready, &guid](){ | ||
JNIEnv *threadEnv; | ||
javaVM()->AttachCurrentThread(&threadEnv, NULL); | ||
guid = xg::newGuid(threadEnv); | ||
javaVM()->DetachCurrentThread(); | ||
|
||
ready = true; | ||
}).detach(); | ||
|
||
while (!ready); | ||
return env->NewStringUTF(guid.c_str()); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
android/app/src/main/java/ca/graemehill/crossguid/testapp/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ca.graemehill.crossguid.testapp; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
public class MainActivity extends Activity { | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.main); | ||
|
||
final TextView textView = (TextView)findViewById(R.id.mainTextView); | ||
textView.setText(test()); | ||
|
||
final TextView javaThreadView = (TextView)findViewById(R.id.javaThreadView); | ||
javaThreadView.setText(createGuidFromJavaThread()); | ||
|
||
final TextView nativeThreadView = (TextView)findViewById(R.id.nativeThreadView); | ||
nativeThreadView.setText(createGuidFromNativeThread()); | ||
} | ||
|
||
public native String test(); | ||
|
||
private static class StringCapture { | ||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public String createGuidFromJavaThread() { | ||
final CountDownLatch created = new CountDownLatch(1); | ||
final StringCapture result = new StringCapture(); | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
result.setValue(newGuid()); | ||
created.countDown(); | ||
} | ||
}).start(); | ||
try { | ||
created.await(); | ||
} catch (InterruptedException e) { | ||
return "Could not get value: " + e.getMessage(); | ||
} | ||
return result.getValue(); | ||
} | ||
|
||
public native String newGuid(); | ||
|
||
public native String createGuidFromNativeThread(); | ||
static { | ||
System.loadLibrary("crossguidtest"); | ||
} | ||
} |
Oops, something went wrong.