Andong supports C + + (NDK) and Java (SDK) languages, and how C + + code interacts with Java is especially important when using the C + + language. In the downloaded NDK package Samples/hello-jni has a simple example to refer to.
Java calls C + +
Create a new Android project, creating the following classes:
Package com.example.testjni;
public class Textjni {
//support to C
static {
system.loadlibrary ("Jniinterface");
}
public static native int getInt ();
public static native String getString ();
}
Two native methods are declared above, and the method implementations representing Getint and GetString are given in C + + (libjniinterface.so).
Run the following command in the classes directory to generate the native corresponding implementation file.
Javah Com.example.testjni.TextJni
# Note If you want to have the Android SDK class need to specify classpath, such as
javah-classpath/users/richard/ Dev/android/sdk/platforms/android-19/android.jar:./bin/classes Com.togic.gameengine.GFRenderer
Generate a header file copy, create a JNI folder, and create a CPP implementation file
Com_example_testjni_textjni.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "com_ example_testjni_textjni.h "
int sum ()
{
int x,y;
x = m;
y = 1000;
x = = y;
return x;
}
Methods to implement Com_example_textjni_textjni.h
Jniexport jint jnicall java_com_example_testjni_textjni_getint (JNIENV * ENV, Jclass CLS)
{return
sum ();
}
Jniexport jstring jnicall java_com_example_testjni_textjni_getstring (jnienv * env, Jclass CLS)
{return
env- >newstringutf ("hellondk!");
}
To use cross compilation, the organization C + + code needs to be android.mk.
Create a new android.mk file under jni/
ANDROID.MK:
Local_path: = $ (call My-dir)
include $ (clear_vars)
local_module: = Jniinterface
Local_src_files: = Com_example_testjni_textjni.cpp
#LOCAL_C_INCLUDES: = $ (Local_path)
include $ (build_ Shared_library)
You can then use the tools in NDK: Ndk-build to generate a dynamic link library: libjniinterface.so
The generated library file can be invoked by the previous Java file.
C + + calls Java
You can invoke Java in the GetString method in the previous example using JNI:
Jniexport jstring jnicall java_com_togic_testjni2_textjni_getstring (jnienv * env, Jclass CLS)
{
Jclass TextJni ;
Jobject Insttextjni;
Jmethodid Getcurrint;
jnienv* jnienv = env;
Textjni = Jnienv->findclass ("Com/togic/testjni2/textjni");
Jmethodid construction_id = Jnienv->getmethodid (Textjni, "Init", "() V");
Insttextjni = Jnienv->newobject (Textjni, construction_id);
Getcurrint = Jnienv->getstaticmethodid (Textjni, "Getcurrint", "() I");
Call Java static method
Jint Jiref = Jnienv->callstaticintmethod (Textjni, getcurrint);
Clean
jnienv->deletelocalref (textjni);
Jnienv->deletelocalref (INSTTEXTJNI);
std::string strref = "hellondk!" + view->getstaticstring ();
Return Env->newstringutf (Strref.c_str ());
}
First of all, it is worth noting that the functions in jni.h distinguish between the C and C + + language interfaces, for C + + generally as follows:
Jclass clazz = Env->findclass (classname);
And for C:
Jclass clazz = (*env)->findclass (env, classname);
The third parameter in Getmethodid represents the method signature, which can be obtained as follows:
Javap-s package name. Class name gets the signature of the method
Data type conversion with JNI
jstring Turn char *
const char nativestring = (env)->getstringutfchars (env, javastring, 0);
Returns a pointer to a UTF-8 character array of strings that will remain in effect until Releasestringutfchars () is released.
(*env)->releasestringutfchars (env, javastring, nativestring);
char * Turn jstring
Jstring jstr = (env)->NEWSTRINGUTF (env, char UTF)
Constructs a new Java.lang.String object with a UTF-8 character array.
Other types
All-in-place copy into notes Java type local c type description
Boolean Jboolean unsigned, 8-bit
byte jbyte unsigned, 8-bit
Char Jchar unsigned, 16-bit
Short Jshort signed, 16-bit
int Jint signed, 32-bit
Long Jlong signed, 64-bit
Float jfloat 32-bit
Double jdouble 64-bit void void N/a