Learning journey based on the Android NDK-----C Call Java

Source: Internet
Author: User

Http://www.cnblogs.com/luxiaofeng54/archive/2011/08/17/2142000.html

Learning journey based on the Android NDK-----C Call Java
Many of the mature C engines are ported to the Android platform and will typically provide some interface for Android SDK and JDK implementations.

The following section describes how C calls static and non-static methods of Java through the JNI layer.

1. Main process

1. Create a new test class Testprovider.java

A) This class provides 2 methods

b) A static method, a non-static method

2. New PROVIDER.C in JNI

A) In this file, you need to map class Testprovider in Java to C

b) Map the two methods of Testprovider to C

c) New Testprovider object

D) Call two methods

3, the Android upper call JNI layer

4. JNI Layer Call C layer

5. C Layer Call Java method

2. Design and implementation

1, the interface design is as follows:

Old, very rubbing, but practical, hey

The code is not posted here, there is a need for brothers to download directly to the end of the article.

2. Key code Description

Classes, methods, objects that define mappings in C

Jclass Testprovider;

Jobject Mtestprovider;

Jmethodid GetTime;

Jmethodid SayHello;

C in the Mapping class

Testprovider = (*jnienv)->findclass (jnienv, "Com/duicky/testprovider");

New Object in C

Jmethodid construction_id = (*jnienv)->getmethodid (jnienv, Testprovider, "<init>", "() V");

Testprovider Mtestprovider = (*jnienv)->newobject (jnienv, testprovider,construction_id);

The mapping method in C

Static:

GetTime = (*jnienv)->getstaticmethodid (jnienv, Testprovider, "GetTime", "() ljava/lang/string;");

Non-static:

SayHello = (*jnienv)->getmethodid (jnienv, Testprovider, "SayHello", "(ljava/lang/string;) V");

Methods for calling Java in C

Static:

(*jnienv)->callstaticobjectmethod (jnienv, Testprovider, getTime);

Non-static:

(*jnienv)->callvoidmethod (jnienv, Mtestprovider, sayhello,jstrmsg);

Pay attention to Getxxxmethodid and Callxxxmethod.

The first XXX represents the type of mapping method, such as: Static and non-static

The second XXX represents the return value of the calling method, such as: Void,object, and so on. (Call the static method to add static after calling)

Refer to the JNI documentation for detailed mapping methods and calling methods, which is important!

3. Java Upper Key Code

Two ways to Testprovider.java

Package com.duicky;

/**
*
*
* @author Luxiaofeng <[email protected]>
*
*/
public class Testprovider {

public static String GetTime () {
Logutils.printwithsystemout ("Call from C Java Static Method");
Logutils.toastmessage (Mainactivity.mcontext, "call from C Java Static Method");
Return string.valueof (System.currenttimemillis ());
}

public void SayHello (String msg) {
Logutils.printwithsystemout ("Call from C Java not Static Method:" + msg);
Logutils.toastmessage (Mainactivity.mcontext, "call from C Java not Static Method:" + msg);
}

}
  

4. android.mk File Key Code

Local_path: = $ (call My-dir)

Include $ (clear_vars)

Local_c_includes: = $ (Local_path)/include
Local_ldlibs + =-l$ (sysroot)/usr/lib-llog


Local_module: = ndk_04
Local_src_files: = \
CTOJAVA.C \
Provider.c

Include $ (build_shared_library)
  

The old way, don't say, you understand. If you don't understand, quack, then please click on the android.mk file introduction

5. Files under the Jni folder

Provider.h

#include <string.h>
#include <jni.h>

void GetTime ();
void SayHello ();
  

Provider.c

#include "Provider.h"
#include <android/log.h>

extern jnienv* jnienv;

Jclass Testprovider;
Jobject Mtestprovider;
Jmethodid GetTime;
Jmethodid SayHello;

int getproviderinstance (Jclass obj_class);

/**
* Initialize classes, objects, methods
*/
int Initprovider () {

__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 1");

if (jnienv = = NULL) {
return 0;
}

if (Testprovider = = NULL) {
Testprovider = (*jnienv)->findclass (jnienv, "Com/duicky/testprovider");
if (Testprovider = = NULL) {
return-1;
}
__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 2 OK");
}

if (Mtestprovider = = NULL) {
if (Getproviderinstance (testprovider)! = 1) {
(*jnienv)->deletelocalref (jnienv, Testprovider);
return-1;
}
__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 3 OK");
}

if (getTime = = NULL) {
GetTime = (*jnienv)->getstaticmethodid (jnienv, Testprovider, "GetTime", "() ljava/lang/string;");
if (getTime = = NULL) {
(*jnienv)->deletelocalref (jnienv, Testprovider);
(*jnienv)->deletelocalref (jnienv, Mtestprovider);
Return-2;
}
__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 4 OK");
}

if (SayHello = = NULL) {
SayHello = (*jnienv)->getmethodid (jnienv, Testprovider, "SayHello", "(ljava/lang/string;) V");
if (SayHello = = NULL) {
(*jnienv)->deletelocalref (jnienv, Testprovider);
(*jnienv)->deletelocalref (jnienv, Mtestprovider);
(*jnienv)->deletelocalref (jnienv, getTime);
return-3;
}
__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 5 OK");
}

__android_log_print (Android_log_info, "jnimsg", "Initprovider Begin 6");
return 1;

}

int getproviderinstance (Jclass obj_class) {

if (Obj_class = = NULL) {
return 0;
}

Jmethodid construction_id = (*jnienv)->getmethodid (jnienv, Obj_class,
"<init>", "() V");

if (construction_id = = 0) {
return-1;
}

Mtestprovider = (*jnienv)->newobject (jnienv, Obj_class,
CONSTRUCTION_ID);

if (Mtestprovider = = NULL) {
Return-2;
}

return 1;
}

/**
* Get time----call Java method
*/
void GetTime () {
if (Testprovider = = NULL | | getTime = = NULL) {
int result = Initprovider ();
if (Result! = 1) {
Return
}
}

jstring jstr = NULL;
char* CStr = NULL;
__android_log_print (Android_log_info, "jnimsg", "GetTime Begin");
Jstr = (*jnienv)->callstaticobjectmethod (jnienv, Testprovider, getTime);
CStr = (char*) (*jnienv)->getstringutfchars (jnienv,jstr, 0);
__android_log_print (Android_log_info, "jnimsg", "Success Get time from Java, Value =%s", CStr);
__android_log_print (Android_log_info, "jnimsg", "GetTime End");

(*jnienv)->releasestringutfchars (jnienv, Jstr, CStr);
(*jnienv)->deletelocalref (jnienv, JSTR);
}

/**
* SayHello----Calling Java methods
*/
void SayHello () {
if (Testprovider = = NULL | | mtestprovider = = NULL | | sayHello = = NULL) {
int result = Initprovider ();
if (Result! = 1) {
Return
}
}

jstring jstrmsg = NULL;
Jstrmsg = (*jnienv)->newstringutf (jnienv, "Hi,i ' m from C");
__android_log_print (Android_log_info, "jnimsg", "SayHello Begin");
(*jnienv)->callvoidmethod (jnienv, Mtestprovider, sayhello,jstrmsg);
__android_log_print (Android_log_info, "jnimsg", "SayHello End");

(*jnienv)->deletelocalref (jnienv, jstrmsg);
}
  

Ctojava.c

#include <string.h>
#include <android/log.h>
#include <jni.h>
#include "Provider.h"

jnienv* jnienv;

/**
* Implementation of the native GetTime method declared in Java
*/
void Java_com_duicky_mainactivity_gettime (jnienv* env, Jobject thiz)
{

if (jnienv = = NULL) {
JNIEnv = env;
}

GetTime ();
}

/**
* Implementation of the native SayHello method declared in Java
*/
void Java_com_duicky_mainactivity_sayhello (jnienv* env, Jobject thiz)
{
if (jnienv = = NULL) {
JNIEnv = env;
}

SayHello ();
}
  

3. Operation effect

1. Click the "C Call Java static method" button

C successfully called the Java GetTime method, through the C method to print out the upper call to get the time, and the upper layer successful toast out of the call information.


2. Click the "C call Java non-static method" button

C successfully called the SayHello method, and successfully received the C pass parameters, and toast out of the corresponding information

4, C call Java Note Point

A) C the corresponding signature when mapping Java methods

GetTime = (*jnienv)->getstaticmethodid (jnienv, Testprovider, "GetTime", "() ljava/lang/string;");

The storyline has not developed so fast, the next chapter will specifically introduce the use of this signature

b) The mapping method requires a distinction between static and non-static getstaticmethodid,getmethodid

c) also need to distinguish between Callstaticobjectmethod,callvoidmethod and return value types when calling

There is no understanding of the brother please leave a message, personal technology is limited, there is the wrong place please Daniel pointed out that not enough comprehensive please forgive, thank you,

Click Download source code C Call Java Example

This article from Duicky Blog, reproduced please indicate the source http://www.cnblogs.com/luxiaofeng54/archive/2011/08/17/2142000.html

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.