Android Studio under JNI pass int,string, array type parameters

Source: Internet
Author: User

This article mainly discusses how Java can pass parameters when it calls C-level through JNI, and how to callback Java method in C code

Previous Android Studio JNI configuration and first Hello Word program

On the basis of the previous example, we continue to study how to pass parameters to C through JNI, in fact, passing parameters is very simple, first of all, say the steps:

1, in Java to define the native of the method of containing parameters

2. Use Javah to generate header files

3, in the header file to find our method of reference, and in C perfect its method body, return C final processing results

The following step-by-step instructions are detailed:

public class Jniutil {
    //directly obtains a string public
   native String Getstringfromc () from C;
    Passing arguments of type int to c,c return processing result public
    native int addnum (int x, int y);
    Passing string-type arguments to C processing the return to the public
    native string Sayfromc (string s);
    Passing an array of type int to c public
    native int[] arrelementsincrease (int[] intarray);
Then open the terminal from Android Studio and go to the App/src/main directory and use the following command:

javah-d Jni-classpath D:\android_studio\sdk\platforms\android-23\android.jar; \.. \build\intermediates\classes\debug Com.zhaocd.activity.myjnidemo1.JNIUtil

You can generate a header file Com_zhaocd_activity_myjnidemo1_jniutil.h Note: After adding a method in Jniutil, you must build the-> rebuild Preject, Otherwise the class file is still the same, and there is no new method in the generated header file.

Open the header file, find several methods for our defined native, and then copy them into HELLO.C's C file to refine the method body:

 * returns two int type variable and/*
jniexport jint jnicall java_com_zhaocd_activity_myjnidemo1_jniutil_addnum
        ( JNIENV * env, Jobject obj, Jint x, Jint y) {

For length, here only a way to paste, because our focus is not C writing, need source code can be downloaded at the end of the article.

You may also notice that the jint,string corresponding to the int corresponds to the jstring, in fact these basic data types are defined in jni.h this file

# include <inttypes.h>/* C99 *typedefuint8_t Jboolean; /* Unsigned 8 bits * *typedefint8_t Jbyte; /* Signed 8 bits * *typedefuint16_t Jchar; /* Unsigned bits * *typedefint16_t Jshort; /* Signed BITS * *typedefint32_t Jint; * Signed Bits * *typedefint64_t Jlong; /* Signed BITS * *typedef floatJfloat; /* 32-bit IEEE 754 */typedef doublejdouble; /* 64-bit IEEE 754 */#elsetypedef unsigned CHARJboolean; /* Unsigned 8 bits * *typedef signed CHARJbyte; /* Signed 8 bits * *typedef unsigned shortJchar; /* Unsigned bits * *typedef ShortJshort; /* Signed BITS * *typedef intJint * Signed Bits * *typedef Long LongJlong; /* Signed BITS * *typedef floatJfloat; /* 32-bit IEEE 754 */typedef doublejdouble; /* 64-bit IEEE 754/#endif/* "Cardinal Indices and sizes" * *
An integer that you define as int, the JNI automatically converts you to jint when you generate the header file, and you can ignore that "J" when you know this, NDK will convert the data type of Java and the data type of C.

In addition to the basic data type, there must be an array.

typedef void*           jobject;
 jobject         jclass;
 jobject         jstring;
 jobject         jarray;
 jarray          jobjectarray;
 jarray          jbooleanarray;
 jarray          jbytearray;
 jarray          jchararray;
 jarray          jshortarray;
 jarray          jintarray;
 jarray          jlongarray;
 jarray          jfloatarray;
 jarray          jdoublearray;
 jobject         jthrowable;
 jobject         jweak;
You can see that regardless of the type of array, it is first converted into Jarray-> jobject-> void*. Void* is a special kind of pointer, saying it is especially because it cannot determine the type of data, or can not determine the length of the object according to the type.

Because we develop projects, usually very little to write their own C-tier code, but the use of some open source of the existing class library or written by C + + engineers, so here is not to do in-depth study of C ~ ~ ~ ~

SOURCE download

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: 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.