working on it ...


Explore Public Snippets

Sort by

Found 89 snippets matching: jni

    public by DanielOgbuagu modified Jun 28, 2015  201418  1  5  0

    Adding a *.jar file (originating from java-wrapped C++ code) to an SBT project classpath

    I thought using unmanaged dependencies in SBT projects was simply dropping a jar file into the lib directory, until I bought an SDK for a biometric device. Both the C++ and the Java packages were provided. I opted for the Java package, copying the provided jar file and the JNI (*.so) files into the appropriate locations and configuring the ja
     * In my <code>build.sbt</code> file, I appended a blank line and then the following:
    libraryDependencies += "groupID" % "artifactId" % "revision" from "file:///home/Danogbuagu/Projects/biometric-lib/filename.jar"
    // where,
    // groupID for example is like: com.zigzagcompany 
    // artifactId for example is like: zigzag
    // revision for example is like: 1.3.4
    // Remember the line befor the code and that after.

    public by FMXExpress modified Nov 12, 2014  7005  9  9  4

    Install A Shortcut On Android With Delphi XE5 Firemonkey
    //Android permission required:
    //<uses-permission android:name=””/>
    Androidapi.JNI.GraphicsContentViewText, FMX.Helpers.Android,
    Androidapi.JNI.JavaTypes, FMX.Platform.Android, AndroidApi.JniBridge, AndroidApi.Jni.App,
    ShortcutIntent: JIntent;
    addIntent: JIntent;
    wIconIdentifier : integer;
    wIconResource : JIntent_ShortcutIconResource;
    ShortcutIntent := TJIntent.JavaClass.init(SharedActivityContext, SharedActivityContext.getClass);
    addIntent := TJIntent.Create;
    addIntent.putExtra(TJIntent.JavaClass.EXTRA_SHORTCUT_INTENT, TJParcelable.Wrap((shortcutIntent as ILocalObject).GetObjectID));// here we need to cast the intent as it’s not done in delphi by default, not like java
    addIntent.putExtra(TJIntent.JavaClass.EXTRA_SHORTCUT_NAME, StringToJString(Application.Title));
    // get icon resource identifier
    wIconIdentifier := SharedActivity.getResources.getIdentifier(StringToJString(‘ic_launcher’), StringToJString(‘drawable’), StringToJString(‘com.embarcadero.HeaderFooterApplication’)); // if the app name change, you must change the package name
    wIconResource := TJIntent_ShortcutIconResource.JavaClass.fromContext(SharedActivityContext, wIconIdentifier);
    // set icon for shortcut
    addIntent.putExtra(TJIntent.JavaClass.EXTRA_SHORTCUT_ICON_RESOURCE, TJParcelable.Wrap((wIconResource as ILocalObject).GetObjectID));

    external by Noitidart modified Jul 15, 2015  57  0  1  0

    _ff-addon-tutorial-jniAndroidToast - Using js-ctypes to use JNI to call functions from the Android API, this specific example does the same as the NativeWindow component, it creates a toast message as seen here:

    _ff-addon-tutorial-jniAndroidToast - Using js-ctypes to use JNI to call functions from the Android API, this specific example does the same as the NativeWindow component, it creates a toast message as seen here: _ff-addon-tutorial-jniAndroidToast.js
    var my_jenv = null;
    try {
    	my_jenv = JNI.GetForThread();
    	// We declare the classes, methods, etc we will be using: this part is not seen in the native example
    	var SIG = {
    		CharSequence: 'Ljava/lang/CharSequence;',
    		Context: 'Landroid/content/Context;',
    		GeckoAppShell: 'Lorg.mozilla.gecko.GeckoAppShell;',
    		Toast: 'Landroid/widget/Toast;', // can use .'s or /'s so `android.widget.Toast` and `android/widget/Toast` both work // from docs page header,%20int,%20int%29
    		int: 'I', // from
    		void: 'V' // from
    	var Toast = JNI.LoadClass(my_jenv, SIG.Toast.substr(1, SIG.Toast.length - 2), {
    		static_fields: [
    			{ name: 'LENGTH_SHORT', sig: }],
    		static_methods: [
    			{ name: 'makeText', sig: '(' + SIG.Context + SIG.CharSequence + + ')' + SIG.Toast } //,%20int,%20int%29
    		methods: [
    			{ name: 'show', sig: '()' + SIG.void } //
    	var geckoAppShell = JNI.LoadClass(my_jenv, SIG.GeckoAppShell.substr(1, SIG.GeckoAppShell.length - 2), {
    		static_methods: [
    			{ name: 'getPreferredIconSize', sig: '()' + },
    			{ name: 'getContext', sig: '()' + SIG.Context },
    	// ok this ends the JNI specific stuff, now below you will see native-by-jni side-by-side
    	// Context context = getApplicationContext();
    	var context = geckoAppShell.getContext();
    	// CharSequence text = 'Hello toast!';
    	var text = 'Hello toast!';
    	// int duration = Toast.LENGTH_SHORT;
    	var duration = Toast.LENGTH_SHORT;
    	// Toast toast = Toast.makeText(context, text, duration);
    	var toast = Toast.makeText(context, text, duration);
    } finally {
    	if (my_jenv) {

    external by MaximAlien modified Oct 21, 2015  139  0  2  0

    [Android] [JNI] Assign value of the int array through JNI

    [Android] [JNI] Assign value of the int array through JNI: AssignIntJNI
    int[] intVal = new int[1];
    JNIEXPORT jboolean JNICALL Java_com_test_example_ChIntVal_change_int(JNIEnv* env, jobject cls, jintArray intValArray)
        int testVal = -1;
        jint *exTypeVal = env->GetIntArrayElements(intValArray, 0);
        exTypeVal[0] = testVal;
        env->ReleaseIntArrayElements(intValArray, exTypeVal, 0);
        return true;

    external by Melvin Cabatuan modified Oct 5, 2014  103  0  2  0

    javah -d jni -classpath $SDK/platforms/android-19/android.jar:../../build/intermediates/classes/debug com.cabatuan.hellondk.Main

    javah -d jni -classpath $SDK/platforms/android-19/android.jar:../../build/intermediates/classes/debug com.cabatuan.hellondk.Main: com_cabatuan_hellondk_Main.h
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_cabatuan_hellondk_Main */
    #ifndef _Included_com_cabatuan_hellondk_Main
    #define _Included_com_cabatuan_hellondk_Main
    #ifdef __cplusplus
    extern "C" {
    #undef com_cabatuan_hellondk_Main_MODE_PRIVATE
    #define com_cabatuan_hellondk_Main_MODE_PRIVATE 0L
    #undef com_cabatuan_hellondk_Main_MODE_WORLD_READABLE
    #define com_cabatuan_hellondk_Main_MODE_WORLD_READABLE 1L
    #undef com_cabatuan_hellondk_Main_MODE_WORLD_WRITEABLE
    #define com_cabatuan_hellondk_Main_MODE_WORLD_WRITEABLE 2L
    #undef com_cabatuan_hellondk_Main_MODE_APPEND
    #define com_cabatuan_hellondk_Main_MODE_APPEND 32768L
    #undef com_cabatuan_hellondk_Main_MODE_MULTI_PROCESS
    #define com_cabatuan_hellondk_Main_MODE_MULTI_PROCESS 4L
    #undef com_cabatuan_hellondk_Main_MODE_ENABLE_WRITE_AHEAD_LOGGING
    #define com_cabatuan_hellondk_Main_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
    #undef com_cabatuan_hellondk_Main_BIND_AUTO_CREATE
    #define com_cabatuan_hellondk_Main_BIND_AUTO_CREATE 1L
    #undef com_cabatuan_hellondk_Main_BIND_DEBUG_UNBIND
    #define com_cabatuan_hellondk_Main_BIND_DEBUG_UNBIND 2L
    #undef com_cabatuan_hellondk_Main_BIND_NOT_FOREGROUND
    #define com_cabatuan_hellondk_Main_BIND_NOT_FOREGROUND 4L
    #undef com_cabatuan_hellondk_Main_BIND_ABOVE_CLIENT
    #define com_cabatuan_hellondk_Main_BIND_ABOVE_CLIENT 8L
    #undef com_cabatuan_hellondk_Main_BIND_ALLOW_OOM_MANAGEMENT
    #define com_cabatuan_hellondk_Main_BIND_ALLOW_OOM_MANAGEMENT 16L
    #undef com_cabatuan_hellondk_Main_BIND_WAIVE_PRIORITY
    #define com_cabatuan_hellondk_Main_BIND_WAIVE_PRIORITY 32L
    #undef com_cabatuan_hellondk_Main_BIND_IMPORTANT
    #define com_cabatuan_hellondk_Main_BIND_IMPORTANT 64L
    #undef com_cabatuan_hellondk_Main_BIND_ADJUST_WITH_ACTIVITY
    #define com_cabatuan_hellondk_Main_BIND_ADJUST_WITH_ACTIVITY 128L
    #undef com_cabatuan_hellondk_Main_CONTEXT_INCLUDE_CODE
    #define com_cabatuan_hellondk_Main_CONTEXT_INCLUDE_CODE 1L
    #undef com_cabatuan_hellondk_Main_CONTEXT_IGNORE_SECURITY
    #define com_cabatuan_hellondk_Main_CONTEXT_IGNORE_SECURITY 2L
    #undef com_cabatuan_hellondk_Main_CONTEXT_RESTRICTED
    #define com_cabatuan_hellondk_Main_CONTEXT_RESTRICTED 4L
    #undef com_cabatuan_hellondk_Main_RESULT_CANCELED
    #define com_cabatuan_hellondk_Main_RESULT_CANCELED 0L
    #undef com_cabatuan_hellondk_Main_RESULT_OK
    #define com_cabatuan_hellondk_Main_RESULT_OK -1L
    #undef com_cabatuan_hellondk_Main_RESULT_FIRST_USER
    #define com_cabatuan_hellondk_Main_RESULT_FIRST_USER 1L
    #undef com_cabatuan_hellondk_Main_DEFAULT_KEYS_DISABLE
    #define com_cabatuan_hellondk_Main_DEFAULT_KEYS_DISABLE 0L
    #undef com_cabatuan_hellondk_Main_DEFAULT_KEYS_DIALER
    #define com_cabatuan_hellondk_Main_DEFAULT_KEYS_DIALER 1L
    #undef com_cabatuan_hellondk_Main_DEFAULT_KEYS_SHORTCUT
    #define com_cabatuan_hellondk_Main_DEFAULT_KEYS_SHORTCUT 2L
    #undef com_cabatuan_hellondk_Main_DEFAULT_KEYS_SEARCH_LOCAL
    #define com_cabatuan_hellondk_Main_DEFAULT_KEYS_SEARCH_LOCAL 3L
    #undef com_cabatuan_hellondk_Main_DEFAULT_KEYS_SEARCH_GLOBAL
    #define com_cabatuan_hellondk_Main_DEFAULT_KEYS_SEARCH_GLOBAL 4L
     * Class:     com_cabatuan_hellondk_Main
     * Method:    getStringFromNative
     * Signature: ()Ljava/lang/String;
    JNIEXPORT jstring JNICALL Java_com_cabatuan_hellondk_Main_getStringFromNative
      (JNIEnv *, jobject);
    #ifdef __cplusplus

    external by maple-nishiyama modified Dec 14, 2015  68  0  1  0

    Android JNI で C/C++ のポインタを Java に渡す。(C++側のソース)

    Android JNI で C/C++ のポインタを Java に渡す。(C++側のソース): NdkPointer.cpp
    #include <jni.h>
    #include <android/log.h>
    #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"NdkPointer", __VA_ARGS__)
    static JavaVM* jvm = nullptr;
    static JNIEnv* env;
    class CppClass {
    	void callback() {
    		LOGD("!!!! callback called !!!!");
    extern "C" {
    jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    	jvm = vm;
    	vm->GetEnv((void**)&env, JNI_VERSION_1_6);
    	return JNI_VERSION_1_6;
    void Java_com_example_ndkpointer_MainActivity_nativeMethod() {
    	LOGD("nativeMethod called");
    	auto obj = new CppClass();
    	jclass c = env->FindClass("com/example/ndkpointer/MainActivity");
    	if (c == nullptr) {
    		LOGD("class not found.");
    	jmethodID mid = env->GetStaticMethodID(c, "asyncProc", "(J)V");
    	if (mid == nullptr) {
    		LOGD("method not found");
    	env->CallStaticVoidMethod(c, mid, reinterpret_cast<jlong>(obj));
    void Java_com_example_ndkpointer_MainActivity_callback(jlong p) {
    	CppClass* obj = (CppClass*)p;
    	delete obj;

    external by Rahnem modified Jan 14, 2015  57  0  1  0

    JNI Demo

    JNI Demo: jnidemo.cpp
    #include <jni.h>
    #include "platform/android/jni/JniHelper.h"
    void CppToJava(const char* str, bool flag)
    	cocos2d::JniMethodInfo t;
    	if (cocos2d::JniHelper::getStaticMethodInfo( t, 
    	                                             // package and class name with slashes rather than dots
    	                                             // name of java function called
    	                                             // this corresponds to the arguments and return value of the called Java function in reverse.
    		jstring java_str = 						t.env->NewStringUTF	(str);
        // create an array of strings
        jclass jStringCls = t.env->FindClass("java/lang/String");
        jobjectArray str_array = t.env->NewObjectArray( 2, jStringCls, NULL);
        jstring keyString1 = t.env->NewStringUTF("one");
        jstring keyString2 = t.env->NewStringUTF("two");
        t.env->SetObjectArrayElement(result, 0, keyString1);
        t.env->SetObjectArrayElement(result, 1, keyString2);
    		t.env->CallStaticVoidMethod		(t.classID, t.methodID, java_str, str_array, (jboolean)flag);
    		t.env->DeleteLocalRef					(t.classID);
    		t.env->DeleteLocalRef					(java_str);
    		t.env->DeleteLocalRef					(str_array);
    extern "C" {
      // The function name here follows a stict naming convention and must be correct in order to recieve call from Java
      // First two arguments are always (JNIEnv* env, jclass clazz) the following are those passed from Java function.
      JNIEXPORT void JNICALL Java_com_my_package_JNIExample_CppFromJava(JNIEnv* env, jclass clazz, jstring str, jfloat num)
        std::string cpp_str = cocos2d::JniHelper::jstring2string(str);
        float cpp_float = (float)jprice;

    external by Le Nam modified Sep 10, 2014  34  0  1  0

    init jni

    init jni:
    $ javah -d jni -classpath /Applications/Android\ com.namlh.ndkexample.MainActivity

    external by Nimrodda modified Mar 5, 2015  34  0  1  0

    Raw JNI with async callbacks

    Raw JNI with async callbacks: JNI.cpp
    static jclass callbacksClass;
    static jobject callbacksInstance;
    JNIEXPORT void JNICALL Java_com_example_NativeClass_nativeMethod(JNIEnv* env, jclass callingObject, jobject callbacks)
    	// Cache the Java callbacks instance
    	callbacksInstance = env->NewGlobalRef(callbacks);
    	// Cache the Java callbacks class (in case of interface, this will be the concrete implementation class)
    	jclass objClass = env->GetObjectClass(callbacks);
    	// Check for null
    	if (objClass)
    		callbacksClass = reinterpret_cast<jclass>(env->NewGlobalRef(objClass));
    // Example of callback lambdas
    void callbacksLambdas()
    		[env]() {
          // Invoke method called 'void onInitSucceeded()'
    			jmethodID onSuccess = env->GetMethodID(callbacksClass, "onInitSucceeded", "()V");
    			// Invoking the callback method on Java side
    			env->CallVoidMethod(callbacksInstance, onSuccess);
    		// FailureCallback lambda
    		[env](int errorCode, const std::string& message) {
    		  // Invoke a method called 'void onInitFailed(int, String)'
    			jmethodID onFailure = env->GetMethodID(callbacksClass, "onInitFailed", "(ILjava/lang/String;)V");
    			// Prepare method paramters (note that it's not possible to release the jstring since we are returning it to Java side)
    			jstring msg = env->NewStringUTF(message.c_str());
    			jint err = errorCode;
    			// Invoking the callback method on Java side
    			env->CallVoidMethod(callbacksInstance, onFailure, err, msg);
    void release()
      // Release the global references to prevent memory leaks

    external by maple-nishiyama modified Dec 14, 2015  34  0  1  0

    Android JNI で C/C++ のポインタを Java に渡す。(Java 側のソース)

    Android JNI で C/C++ のポインタを Java に渡す。(Java 側のソース):
    package com.example.ndkpointer;
    import android.os.Bundle;
    public class MainActivity extends Activity {
    	static {
    	protected void onCreate(Bundle savedInstanceState) {
    	public static native void nativeMethod();
    	public static native void callback(long cppPointer);
    	public static void asyncProc(final long cppPointer) {
    		Thread t = new Thread(new Runnable() {
    			public void run() {
    				// 時間のかかる処理
    				try {
    				} catch (InterruptedException e) {
    • Public Snippets
    • Channels Snippets