Android JNI entry instance (Windows + cygwin + eclipse)

Source: Internet
Author: User
1 Basic Knowledge Point

① JNI: Java Native Interface
② Call JNI in Android:

The Code declared in the static // static area is 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 the Android. mk file (same directory as the. c file)
⑥ Compile and generate the. So file

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

Shows the compilation method. 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 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:

Note:

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



Author: skyseraph

Source: http://www.cnblogs.com/skyseraph/

Email/Gtalk: zgzhaobo@gmail.com

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.