[Android tutorial] Android JNI entry instance (Windows + cygwin + eclipse)

Source: Internet
Author: User
ArticleDirectory
    • ① Create an android project and compile the testjni. Java class. The source code is as follows:
    • ② Compile and generate the testjni. Class File
    • ③ Compile and generate the com_testjni_testjni.h File
    • ⑤ Create the Android. mk file (same directory as the. c file)
    • 6 compile and generate the libtestjni. So file
[Android tutorial] Android JNI entry instance (Windows + cygwin + eclipse) Skyseraph Feb 20ty 2012 sztcl

Email: zgzhaobo@gmail.com QQ: 452728574

 

1 Basic Knowledge Point

① JNI: Java Native Interface
② Call JNI in Android:
Static // declared in the static areaCodeWill be executed before the oncreate Method
{
System. loadlibrary ("helloworld"); // load. The complete name is lib helloworld. So.
}
Private native string Hello (); // has the native keyword

2. Basic Steps for JNI Development

① Write. Java class
② Generate a. Class File
③ Generate a. h file
③ Check. h file
Jniexport and jnicall are JNI macros.
Note signature before the function: () ljava/lang/string; brackets in the meaning: indicates that the function parameter is null, ljava/lang/string indicates that the function return value is a string object of Java
④ Compile the. c/CPP File
⑤ Create an android. mk File(Same directory as the. c file)
⑥ Compile and generate the. So file

In addition:Borrow an image from the Refs-8, as shown below

3. Example: testjni ([cygwin] in windows, for the Linux environment, please refer to the article in refs) ① create an android project and compile the testjni. Java class. The source code is as follows:
 1  Package Com. testjni;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 Import Android. util. log;
6
7 Public Class Testjni Extends Activity
8 {
9 /** Called when the activity is first created. */
10 @ Override
11 Public Void Oncreate (bundle savedinstancestate)
12 {
13 Super . Oncreate (savedinstancestate );
14 Setcontentview (R. layout. Main );
15 Log. D ("hi", "android call JNI:" + outputjni ());
16 }
17
18 Private Native String outputjni ();
19
20 Static
21 {
22 System. loadlibrary ("testjni ");
23 }
24 }
② Compile and generate the testjni. Class File

Compilation Method:
Method 1: Click build project under android to generate the corresponding. Class file under the binfile of the project.
Method 2: Run cmd, locate the project directory, and run the javac command.

③ Compile and generate the com_testjni_testjni.h File

Compilation Method: As shown in. The first three red boxes are common errors. Please note!

④ Compile the. c file.

You can write it directly in Notepad. Of course, if your eclipse environment has been integrated with the C/C ++ Development Environment (For details, refer to [Android tutorial] to build the android ndk environment. ), Which can be directly written in eclipse

The Code is as follows. For the description, see the notes:

⑤ Create the Android. mk file (same directory as the. c file)

 

6 compile and generate the libtestjni. So file

Compilation Method:
Open cygwin (for cygwin installation, see [Android tutorial] to build the android ndk Environment). Run the following command:

Description:
I copied the project to the ndk configuration environment directory to run it. I can say that I can directly CD it to the current project directory, and then run it. I tried it n times and failed, my project is under D: \ My document (ndk is in c: \ Android \ android-ndk-r7)

The running result is as follows:

Refs/related

1 http://my.unix-center.net /~ Simon_fu /? P = 833
2 http://my.unix-center.net /~ Simon_fu /? P = 836
3 http://www.android123.com.cn/androidkaifa/679.html
4 http://doandroid.info/2012/03/18/jni_and_android_jni_details/
Http://blog.csdn.net/hellogv/article/details/6094127
Http://blog.csdn.net/redoffice/article/details/6654714
7 http://www.cnblogs.com/lilactutu/archive/2010/12/06/1897696.html
8 http://my.unix-center.net /~ Simon_fu /? P = 359

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.