Android uses JNI to call C ++ custom classes

Source: Internet
Author: User

I have been searching for articles about android Calling C/C ++ libraries for a long time, but there is no article about android Calling C ++ custom classes using jni, instead, I had to look at the android source code and learn how to implement the android graphics library, because the underlying implementation of this library also uses the C ++ skia library. The following three files are described.


First, you have a class in java, which is called by the Person class in your application.

[Java]
Package whf. jnitest;
 
Public class Person {

Static
{
System. loadLibrary ("Person ");
}

Private int mNativePerson;

Public Person ()
{
MNativePerson = init ();
}
Protected void finalize ()
{
Try {
Finalizer (mNativePerson );
} Finally {
Try {
Super. finalize ();
} Catch (Throwable e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Public int getAge ()
{
Return native_getAge (mNativePerson );
}

Public void setAge (int age)
{
Native_setAge (mNativePerson, age );
}
 
Private native void finalizer (int nPerson );
Public native int add (int a, int B );
Public native int sub (int a, int B );
Private native int init ();

Private native int native_getAge (int nPerson );
Private native void native_setAge (int nPerson, int age );
}

Then your C ++ file is as follows. I define it as the CPerson class.
[Cpp]
# Define LOG_TAG "this is a first JNI test"
 
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <assert. h>
# Include "jni. h"
# Include "CPerson. h"
 
 
 
Jint add (JNIEnv * env, jobject obj, jint a, jint B)
{
Return a + B;
}
 
Jint Java_whf_jnitest_Person_sub (JNIEnv * env, jobject obj, jint a, jint B)
{
Return a-B;
}
 
Void Java_whf_jnitest_Person_finalizer (JNIEnv * env, jobject clazz, CPerson * obj)
{
Delete obj;
}
 
CPerson * Java_whf_jnitest_Person_init (JNIEnv * env, jobject clazz)
{
CPerson * p = new CPerson ();
Return p;
}
 
Void Java_whf_jnitest_Person_setAge (JNIEnv * env, jobject clazz, CPerson * obj, jint age)
{
Return obj-> setAge (age );
}
 
Jint Java_whf_jnitest_Person_getAge (JNIEnv * env, jobject clazz, CPerson * obj)
{
Return obj-> getAge ();
}
 
Static JNINativeMethod methods [] {
{"Add", "(II) I", (void *) add },
{"Sub", "(II) I", (void *) Java_whf_jnitest_Person_sub },
{"Finalizer", "(I) V", (void *) Java_whf_jnitest_Person_finalizer },
{"Init", "() I", (void *) Java_whf_jnitest_Person_init },
{"Native_setAge", "(II) V", (void *) Java_whf_jnitest_Person_setAge },
{"Native_getAge", "(I) I", (void *) Java_whf_jnitest_Person_getAge },
};
 
Static const char * classPathName = "whf/jnitest/Person ";
 
Static int registerNativeMethods (JNIEnv * env, const char * className,
JNINativeMethod * gMethods, int numMethods)
{
Jclass clazz;
 
Clazz = env-> FindClass (className );
If (clazz = NULL ){
// LOGE ("Native registration unable to find class '% S'", className );
Return JNI_FALSE;
}
If (env-> RegisterNatives (clazz, gMethods, numMethods) <0 ){
// LOGE ("RegisterNatives failed for '% S'", className );
Return JNI_FALSE;
}
 
Return JNI_TRUE;
}
 
Static int registerNatives (JNIEnv * env)
{
If (! RegisterNativeMethods (env, classPathName,
Methods, sizeof (methods)/sizeof (methods [0]) {
Return JNI_FALSE;
}
 
Return JNI_TRUE;
}
 
Typedef union {
JNIEnv * env;
Void * venv;
} UnionJNIEnvToVoid;
 
/* This function will be call when the library first be loaded */
Jint JNI_OnLoad (JavaVM * vm, void * reserved)
{
UnionJNIEnvToVoid uenv;
JNIEnv * env = NULL;
// LOGI ("JNI_OnLoad! ");
 
If (vm-> GetEnv (void **) & uenv. venv, JNI_VERSION_1_4 )! = JNI_ OK ){
// LOGE ("ERROR: GetEnv failed ");
Return-1;
}
 
Env = uenv. env ;;
 
// JniRegisterNativeMethods (env, "whf/jnitest/Person", methods, sizeof (methods)/sizeof (methods [0]);
 
If (registerNatives (env )! = JNI_TRUE ){
// LOGE ("ERROR: registerNatives failed ");
Return-1;
}
 
Return JNI_VERSION_1_4;
}

CPerson's statement is as follows:
[Cpp]
# Ifndef _ CPERSON_H _
# Define _ CPERSON_H _
 
Class CPerson
{
Private:
Int m_age;
Public:
CPerson ();
Int getAge ();
Void setAge (int age );
};
 
# Endif www.2cto.com

So a simple JNI calls the C ++ custom class.


Author: wanghaofeng

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.