The main points of JNI usage

Source: Internet
Author: User

Recently in the company to open a new project, and the backend to discuss a bit, ready to exchange HTTP encryption algorithm, this change does not matter, altogether found that the water is quite deep, first met himself did not involve the NDK, and then is JNI, here on the above content to do some summary.

The first is the NDK environment configuration

Later learned that Android Studio has supported the NDK, and quickly cut from eclipse, found a good tutorial, basically can be very simple to make a demo.

Well, the project has been set up, the following is the JNI programming, just beginning to see people's projects, simply unthinking, messy to describe, after a period of research, found that the JNI call Java method is actually very simple, commonly used several initialization, declaration, call will, you can convert Java code in the past , here are a few common methods:

Androidjni.findclass Find class static function Findclass (name:string): intptrdescription Description This function loads a Locally-defin Ed class. This function loads a locally defined class.

Androidjni.getmethodid Get method idstatic function Getmethodid (clazz:intptr, Name:string, sig:string): intptrdescription Describes the returns the method ID for a instance (nonstatic) methods of a class or interface. Returns a class or interface instance (non-static) approach ID. The Clazz method can be defined in a superclass of a certain class, or it can be inherited from Clazz. The method is determined by its name and signature. Getmethodid () Initializes a class that is not initialized.

Androidjni.newobject new Object static function NewObject (Clazz:intptr, Methodid:intptr, args:jvalue[]): Intptrdescriptio N describes constructs a new Java object. The method ID indicates which constructor method to invoke. This ID must is obtained by calling Getmethodid () with as the method name and void (V) as the return type. Constructs a new Java object. The method ID indicates the constructor method that should be called. The ID must be obtained by calling Getmethodid () and the method name must be <init> when called, and the return type must be void (V). The Clazz parameter must not refer to the array class.
Androidjni.callintmethod call integer method static function Callobjectmethod (Obj:intptr, Methodid:intptr, args:jvalue[]): IntPt Rdescription Description calls an instance (nonstatic) Java method defined by Methodid, optionally passing an array of arguments (ARG s) to the method. Invokes a Java method of an instance defined by Methodid, optionally passing an array of parameters (args) to this approach.
For more details, please see the following article Androidjni function introduction

Let's look at an example:
Jclass string_cls = Env->findclass ("java/lang/string");    Jmethodid Substring_mid = Env->getmethodid (string_cls, "substring", "(II) ljava/lang/string;");    Return reinterpret_cast<jstring> (Env->callobjectmethod (sign,substring_mid,4, 17));

It is important to note the format of the Getmethodid method.

jmethodid Getmethodid (jnienv *env, Jclass clazz, const char *name, const char *sig);


JNIEnv This parameter is not required in C + +. Clazz is the Jclass in front.

Name is the method name, and the sig is the method signature.

The method signature has a specific format: (Param-type) Ret-type, which indicates that the method passed in the parameter type, followed by the return type

Where Param-type and Ret-type are made up of specific symbols. So how can the sig get to Nepal?

$ javap-s Class

Like what:

$ javap-s java.lang.String

Public java.lang.String touppercase ();    Signature: () ljava/lang/string;  Public java.lang.String trim ();    Signature: () ljava/lang/string;  Public java.lang.String toString ();    Signature: () ljava/lang/string;

you can see all the methods and signatures.






The main points of JNI usage

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.