Android JNI (2) The first android project, jniandroid

Source: Internet
Author: User

Android JNI (2) The first android project, jniandroid

After downloading the NDK, it comes with a sample. Beginners can import it to Eclipse to run it.

 

Here I create a new project myself.

Step 1: Create an Android project jni_test (named by yourself)

Step 2: add local support for the project

Right-click the project and select android tools -- Add Native support. A dialog box is displayed asking you to enter the library name.

You can modify the name or use the default name. After you click finish, you will find that there are several more folders in the project, one of which is called jni, here is where we write c code. By default, two files are generated in the jni folder, one is cpp and the other is mk.

Next we need to load the library file in the Java code and declare the local method, as follows:

 

// Declare the local method

Public native String getStr ();

// Jni_test is the library file name. It is also the name entered in the pop-up dialog box just now, and also the name of the so file generated after compiling the c file.

Static {
System. loadLibrary ("jni_test ");
}

Note: If you need to use the local method in the activity, remove the extend activity code first.

 

Next, run the command to generate the header file.

Run the cd command to enter the project, and then run javah-classpath bin/classes-d jni to use the full name of the local code class.

If the javah command is unavailable, it indicates that your java environment variable is not configured

 

After the execution is successful, you can find an additional. h file in the jni folder.

You can also generate header files without using javah.

The header file name is very long. We can make a change. Here I will change it to jni_test.h.

 

Next, we need to write code in the cpp file according to our header file.

# Include <jni. h>
# Include <jni_test.h>
JNIEXPORT jstring JNICALL Java_com_zhangyi_android_jnitest_MainActivity_getStr
(JNIEnv * env, jobject obj ){
Return env-> NewStringUTF ("Hello, I am from c ");
}

 

 

Next, we need to modify the activity code. Because the java code only declares the Local Code and is not called, add a text file to the layout file and initialize it (no code is written, we can see that all people in this place have a certain android Foundation.) set the content of this text control to getStr ();

 

 

Code has been written. run it ~~~~~~~

 


Remove JNI from android Project

Are there any Android. mk files? Remove them? Remove the so library loaded in the java file, and remove all related methods.

Android jni Problems

1. on Android, whether Java or C/C ++ is used depends on your actual situation. If you are porting the existing C/C ++ program, it is undoubtedly NDK, or when you focus on program memory control and execution efficiency, you have to take the native route. After all, the Java features will lead to a slight difference in these aspects. If it is not the case above, it is good to directly use the Java-layer HttpConnection and so on. After all, it is easy to use. The issue of Native debugging for Android is indeed a headache in the 1.6 era. But with the continuous development of NDK and the official attention to this issue, now 2. the version of X (as if it was 2.2, you can check it on the official website) already supports GDB Command Line Debugging. Of course, this is not as silly as VS debugging, breakpoint, to skip the step, you must be familiar with the GDB command, but it can indeed be debugged.
2. this problem has nothing to do with the Android system. If you use socket to implement TCP connection, you can define the structure of the data packet, for example, send Buffer: 4 bytes (int mark command number) + 2 bytes (short command parameter) + 1 byte (char command parameter) structure, or you can refer to the encapsulation mode structure such as FTP and HTTP, of course, this must also work with your server logic. If your server is a Web server, it will be over HTTP.
Personal opinion, for reference only.

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.