working on it ...

Filters

Explore Public Snippets

Sort by

Found 90 snippets matching: jni

    public by DanielOgbuagu modified Jun 28, 2015  201443  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  7061  9  9  4

    Install A Shortcut On Android With Delphi XE5 Firemonkey

    http://www.fmxexpress.com/install-shortcuts-on-the-android-home-screen-with-delphi-xe5-firemonkey/
    //Android permission required:
    //<uses-permission android:name=”com.android.launcher.permission.INSTALL_SHORTCUT”/>
        
    Uses
    Androidapi.JNI.GraphicsContentViewText, FMX.Helpers.Android,
    Androidapi.JNI.JavaTypes, FMX.Platform.Android, AndroidApi.JniBridge, AndroidApi.Jni.App,
    AndroidAPI.jni.OS;
    
    {$IFDEF ANDROID}
    var
    ShortcutIntent: JIntent;
    addIntent: JIntent;
    wIconIdentifier : integer;
    wIconResource : JIntent_ShortcutIconResource;
    {$ENDIF}
    begin
    {$IFDEF ANDROID}
    
    ShortcutIntent := TJIntent.JavaClass.init(SharedActivityContext, SharedActivityContext.getClass);
    ShortcutIntent.setAction(TJIntent.JavaClass.ACTION_MAIN);
    
    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));
    addIntent.setAction(StringToJString(‘com.android.launcher.action.INSTALL_SHORTCUT’));
    // 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));
    
    SharedActivityContext.sendBroadcast(addIntent);
    
    {$ENDIF}
     

    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: https://developer.mozilla.org/en-US/Add...

    _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: https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/NativeWindow/toast: _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 http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20int,%20int%29
    		int: 'I', // from https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp9502
    		void: 'V' // from http://www.rgagnon.com/javadetails/java-0286.html
    	};
    
    	var Toast = JNI.LoadClass(my_jenv, SIG.Toast.substr(1, SIG.Toast.length - 2), {
    		static_fields: [
    			{ name: 'LENGTH_SHORT', sig: SIG.int }],
    		static_methods: [
    			{ name: 'makeText', sig: '(' + SIG.Context + SIG.CharSequence + SIG.int + ')' + SIG.Toast } // http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20int,%20int%29
    		],
    		methods: [
    			{ name: 'show', sig: '()' + SIG.void } // http://developer.android.com/reference/android/widget/Toast.html#show%28%29
    		]
    	});
    
    	var geckoAppShell = JNI.LoadClass(my_jenv, SIG.GeckoAppShell.substr(1, SIG.GeckoAppShell.length - 2), {
    		static_methods: [
    			{ name: 'getPreferredIconSize', sig: '()' + SIG.int },
    			{ 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);
    
    	toast.show();
    
    } finally {
    	if (my_jenv) {
    		JNI.UnloadClasses(my_jenv);
    	}
    }
    
    

    external by MaximAlien modified Oct 21, 2015  141  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
    C
    /* 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" {
    #endif
    #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
    }
    #endif
    #endif
    
    

    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 {
    
    public:
    	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.");
    		return;
    	}
    	jmethodID mid = env->GetStaticMethodID(c, "asyncProc", "(J)V");
    	if (mid == nullptr) {
    		LOGD("method not found");
    		return;
    	}
    	env->CallStaticVoidMethod(c, mid, reinterpret_cast<jlong>(obj));
    
    }
    
    void Java_com_example_ndkpointer_MainActivity_callback(jlong p) {
    	CppClass* obj = (CppClass*)p;
    	obj->callback();
    	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
    	                                             "com/my/package/JNIExample",
    	                                             // name of java function called
    	                                             "JavaFromCpp", 
    	                                             // this corresponds to the arguments and return value of the called Java function in reverse.
    	                                             // http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
    	                                             "(Ljava/lang/String;[Ljava/lang/String;Z)V")) 
      {
    		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: init_jni.sh
    $ javah -d jni -classpath /Applications/Android\ Studio.app/sdk/platforms/android-19/android.jar:../../build/intermediates/classes/debug 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));
    		env->DeleteLocalRef(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
    	env->DeleteGlobalRef(callbacksClass);
    	env->DeleteGlobalRef(callbacksInstance);
    }
    
    

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

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

    Android JNI で C/C++ のポインタを Java に渡す。(Java 側のソース): com.example.ndkpointer.MainActivity.java
    package com.example.ndkpointer;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
    	static {
    		System.loadLibrary("NdkPointer");
    	}
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		nativeMethod();
    	}
    
    	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() {
    
    			@Override
    			public void run() {
    				// 時間のかかる処理
    				try {
    					Thread.sleep(3000);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				callback(cppPointer);
    			}
    		});
    
    		t.start();
    	}
    
    }
    
    
    
    • Public Snippets
    • Channels Snippets