Jni Tutorial for Android

Hi, can anyone suggest me some good resources for learning JNI for Android and some good JNI tutorials?

+45
android jni
Apr 14 2018-11-11T00:
source share
3 answers

I would suggest downloading ndk. Unzip it and view ndk example codes . Start with a greeting and move on. This explains a lot with ease. You can also view these links by looking at the code and continuing to go back and forth.

+16
Apr 25 2018-12-12T00:
source share
— -

Study Guide for ECLIPSE

Here is the first and second little tutorial, but if you want to write a simple program that uses JNI, you can continue reading :)

Create an Android app project. After creating your project, you need to create a new folder at the top level of the project. To do this, right-click the name of your project → New → Folder. Name this folder jni. Then add a class called SquaredWrapper . add this code to it

 package org.edwards_research.demo.jni; public class SquaredWrapper { // Declare native method (and make it public to expose it directly) public static native int squared(int base); // Provide additional functionality, that "extends" the native method public static int to4(int base) { int sq = squared(base); return squared(sq); } // Load library static { System.loadLibrary("square"); } } 

Open a terminal. You must compile this code to get the header file. Call this command first.

 cd src # change into the source directory javac -d /tmp/ org/edwards_research/demo/jni/SquaredWrapper.java 

Than

 cd /tmp javah -jni org.edwards_research.demo.jni.SquaredWrapper 

SO, you will have a header file named org.edwards_research.demo.jni.SquaredWrapper in the tmp .

it should be something like this

 /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class org_edwards_research_demo_jni_SquaredWrapper */ #ifndef _Included_org_edwards_research_demo_jni_SquaredWrapper #define _Included_org_edwards_research_demo_jni_SquaredWrapper #ifdef __cplusplus extern "C" { #endif /* * Class: org_edwards_research_demo_jni_SquaredWrapper * Method: squared * Signature: (I)I */ JNIEXPORT jint JNICALL Java_org_edwards_1research_demo_jni_SquaredWrapper_squared (JNIEnv *, jclass, jint); #ifdef __cplusplus } #endif #endif 

change this name to something short for your comfort, for example square.h. Copy this header file into the jni folder of your application. Then create the square.c file in the same folder, copy it to it.

  #include "square.h" JNIEXPORT jint JNICALL Java_com_example_android_1jni_1second_1demo_SquaredWrapper_squared (JNIEnv * je, jclass jc, jint base) { return (base*base); } 

Add this to your MainActivity.java

 int x = SquaredWrapper.to4(2); x = x*2; 

Add Android.mk file to jni folder with this body

  LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := squared LOCAL_SRC_FILES := square.c include $(BUILD_SHARED_LIBRARY) 

It should create a library from header files and cpp.

So, you only need to make some settings by looking at mine. Now you can compile it, make sure your library is created and your lib directory consists of it.

Study Guide for Android Studio

Suppose you have a simple Android application open by Android Studio

step 1: open the Android Studio application

Step 2: Download the NDK and set the path to the NDK in the local properties of your application (below / above the android sdk path), for example, ndk.dir=C\:\\Android\\android-ndk-r10e

PS for windows double //, for linux one /

step3: Add the JNI folder in the application (right-click on the application → new → folder → JNI folder)

Step 4 Configure Gradle as follows:

Add this code to app/build.gradle to run NDK

  sourceSets.main { jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' } tasks.withType(NdkCompile) { // disable automatic ndk-build call compileTask -> compileTask.enabled = false } task ndkBuild(type: Exec) { // call ndk-build(.cmd) script if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'cmd', '/c', 'ndk-build.cmd', '-C', file('src/main').absolutePath } else { commandLine 'ndk-build', '-C', file('src/main').absolutePath } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } 

step 4:

Create Android.mk and Application.mk files in the JNI folder of the application using these bodies:

Android.mk

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := main LOCAL_SRC_FILES := main.cpp include $(BUILD_SHARED_LIBRARY) 

Application.mk

 APP_ABI := all include $(BUILD_SHARED_LIBRARY) 

In APP_ABI, you choose which compilers to use. it

step 5:

We need to manually start NDK, since we disconnected it from build.config . To create the file com_example_nativedemo_app_MainActivit.h in the src \ main \ jn folder, we must run this command in the terminal

 javah -d .\jni -classpath C:\Intel\INDE\IDEintegration\android-sdk-windows\platforms\android-21\android.jar;..\..\build\intermediates\classes\debug com.example.mydemo.nativedemo.MainActivity 

for Windows cmd you must provide the full path to the files. For

step 6:

Add the main.cpp file to the JNI folder with this body:

 #include <string.h> #include <jni.h> #include <vchat_cpptest_Hellojnicpp.h> extern "C" { JNIEXPORT jstring JNICALL Java_vchat_cpptest_Hellojnicpp_stringFromJNI (JNIEnv *env, jobject obj) { #ifdef __INTEL_COMPILER_UPDATE return env->NewStringUTF("Hello from Intel C++ over JNI!"); #else return env->NewStringUTF("Hello from default C++ over JNI!"); #endif } 
+41
Nov 15 '12 at 16:18
source share

By the way, you can use cool wrappers for Java classes. And you won't need JNI tutorials. Using these shells, you can write C ++ code in the same way as in java. For example, something like this:

 ... View someView; someView.setAlpha(0.5); ... 

Here is the link: http://code.google.com/p/android-cpp-sdk/

0
Aug 04 2018-12-12T00:
source share



All Articles