In-depth understanding of Android (2) -- Understanding JNI (medium) and androidjni in Android

Source: Internet
Author: User

In-depth understanding of Android (2) -- Understanding JNI (medium) and androidjni in Android

Sunshine Xiaoqiang participated in the CSDN blog Star Selection, If you think sunshine Xiaoqiang blog is helpful to you, vote for Xiaoqiang: http://vote.blog.csdn.net/blogstar2014/details? Username = lxq_xsyu # content

In the previous article, we learned about the use of JNI in Android. In fact, JNI is a very early technology, not a new technology created in Android, is a method provided by SUN for us to call each other between Java and local code, this article describes how to call C/C ++ code in Java.

1. Create a common Java Project

Package com. csdn. test;/*** sunshine Xiaoqiang http://blog.csdn.net/dawanganban * @ author lixiaoqiang **/public class TestJNI {public native void saveHello (); public static void main (String [] args) {TestJNI jni = new TestJNI (); jni. saveHello ();}}
In TestJNI, we declare a native method.

Ii. generate C/C ++ header files

SUN's JDK already provides a tool (javah) for us to generate the corresponding C/C ++ header file)

lixiaoqiangdeMac-mini:bin lixiaoqiang$ whereis javah/usr/bin/javahlixiaoqiangdeMac-mini:bin lixiaoqiang$ 
Note: I am using the max OS system. If you are using a windows system, find it in the jdk directory.

Find the directory where our project file is located and execute the javah command as follows:

lixiaoqiangdeMac-mini:bin lixiaoqiang$ pwd/Users/lixiaoqiang/Documents/AndroidWorkspace/TestJNI/binlixiaoqiangdeMac-mini:bin lixiaoqiang$ javah com.csdn.test.TestJNIlixiaoqiangdeMac-mini:bin lixiaoqiang$ ls comcom_csdn_test_TestJNI.hlixiaoqiangdeMac-mini:bin lixiaoqiang$ 
We can see that a header file com_csdn_test_TestJNI.h is generated for us.

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_csdn_test_TestJNI */#ifndef _Included_com_csdn_test_TestJNI#define _Included_com_csdn_test_TestJNI#ifdef __cplusplusextern "C" {#endif/* * Class:     com_csdn_test_TestJNI * Method:    saveHello * Signature: ()V */JNIEXPORT void JNICALL Java_com_csdn_test_TestJNI_saveHello  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
3. Generate a dynamic library file. Open the C/C ++ Editor (Xcode is used here, and friends on Windows can use Visual Studio)

In main. cpp, We output a line of text, as shown below:

#include "com_csdn_test_TestJNI.h"#include <iostream>using namespace std;JNIEXPORT void JNICALL Java_com_csdn_test_TestJNI_saveHello(JNIEnv * evn, jobject obj){    cout << "hello world" << endl;}
Jni cannot be found during compilation. h and jni_md.h files, which can be found in the include directory of JDK and copied to the project. Next, export the above project to the dynamic link library.

When exporting the link library (Compilation), it is different in MacOS and in Linux and Windows, instead of compiling. so or dll, but the jnilib of MacOS. and jni. h directory is also quite special, is/System/Library/Frameworks/JavaVM. framework/Headers/. The specific command is as follows:

lixiaoqiangdeMac-mini:TestJNI lixiaoqiang$ g++ -dynamiclib -o libhelloworld.jnilib main.cpp -framework JavaVM -I/System/Library/Frameworks/JavaVM.framework/HeaderslixiaoqiangdeMac-mini:TestJNI lixiaoqiang$ lscom_csdn_test_TestJNI.hjni_md.hmain.cppjni.hlibhelloworld.jniliblixiaoqiangdeMac-mini:TestJNI lixiaoqiang$ 
We can see that a libhelloworld. jnilib file is generated in this directory. We copy this file to the bin directory of our Java Project (classes directory in windows)

Add the code for adding a dynamic library to the Java project (the System. loadLibrary method in the static statement block below)

Package com. csdn. test;/*** sunshine Xiaoqiang http://blog.csdn.net/dawanganban * @ author lixiaoqiang **/public class TestJNI {public native void saveHello (); public static void main (String [] args) {TestJNI jni = new TestJNI (); jni. saveHello ();} static {System. loadLibrary ("helloworld ");}}
Finally, add the library file to the environment variables that can be accessed by the Java program to run the output "helloworld.

The next article will show you how to call methods in Java in C/C ++.

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.