[Android & JNI & ndk] describes each step in detail, allowing you to easily master Android JNI ndk

Source: Internet
Author: User
Tags netbeans

1. Development Environment Configuration

As we all know, it is difficult to start with everything. configuring a development environment is the most basic and necessary step for all development projects.

All software and development platforms used: win7 + netbeans + VMware + Ubuntu + ndk + Android SDK

First of all, in win7 platform to configure the development of Android environment, detailed reference blog: http://blog.csdn.net/tianshuai11/article/details/7367700

Then download the android-ndk-r7b's ndk Development Kit http://developer.android.com/sdk/ndk/index.html from Ubuntu systems in VMware to select the Linux version. Download and unzip to the/usr folder, then configure the environment variable: vi. bashrc Add: ndk =/usr/android-ndk-r7b export ndk --- at the end and then make the change take effect:Source
. Bashrc --- test at the terminal: Echo $ ndk to see whether the output is:/Usr/android-ndk-r7b if output indicates configuration successful

2. Coding

Before reading the following article, I hope you have some knowledge about JNI. If you're not very familiar with the example implementation in the 8-plus blog, http://blog.csdn.net/tianshuai11/article/details/7367881

1) create a project in netbeans: Project name: Hello; package name: tianshuai. Hello; Activity name: Hello; Target Platform: Android 2.3.3

2) Add the following code to the hello file:

Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class Hello extends activity <br/> {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) <br/>{< br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> textview TV = (textview) findviewbyid (R. id. TV); <br/> // textview TV = new textview (this); <br/> // TV. settext ("Sss"); <br/> TV. settext (stringfromjni (); <br/>}< br/> Public native string stringfromjni (); <br/> static {<br/> system. loadlibrary ("hello"); <br/>}< br/>Make corresponding changes in Main. xml

3) Compile the C header file

Right-click-> Hello project-> Generate, and generate hello. Class in Hello \ bin \ Classes \ tianshuai \ hello

Then, in the DOS window of the terminal, CD to the \ Hello \ bin \ Classes Directory: Enter javah-classpath c: \ Users \ tianshuai \ Desktop \ Hello \ bin \ Classes-JNI tianshuai. hello. hello

The tianshuai_hello_hello.h file is generated in the classes Folder:

/* Do not edit this file-it is machine generated */<br/> # include <JNI. h> <br/>/* Header for class tianshuai_hello_hello */</P> <p> # ifndef _ delimiter <br/> # DEFINE _ included_tianshuai_hello_hello <br/> # ifdef _ _ cplusplus <br/> extern "C" {<br/> # endif <br/>/* <br/> * class: tianshuai_hello_hello <br/> * method: stringfromjni <br/> * Signature: () ljava/lang/string; <br/> */<br/> jniexport jstring jnicall java_tianshuai_hello_hello_stringfromjni <br/> (jnienv *, jobject ); </P> <p> # ifdef _ cplusplus <br/>}< br/> # endif <br/>

PS: Suppose you have configured the Java environment variable, see blog http://blog.csdn.net/tianshuai11/article/details/7367700

4) manually change the project

Create a JNI folder under the root directory of the hello project, and thenTianshuai_hello_hello.h
Copy the file

Add interface implementation file: tianshuai_hello_hello.c

# Include <string. h> <br/> # include <JNI. h> <br/> # include "tianshuai_hello_hello.h" <br/> jstring <br/> java_tianshuai_hello_hello_stringfromjni (jnienv * ENV, <br/> jobject thiz) <br/> {<br/> return (* env)-> newstringutf (ENV, "hello from JNI! "); <Br/>}< br/>Add and generate a processing file: Android. mkLocal_path: =$ (call my-DIR) </P> <p> include $ (clear_vars) </P> <p> local_module: = Hello <br/> local_src_files: = tianshuai_hello_hello.c </P> <p> include $ (build_shared_library) <br/>

5) save all the above projects and files and copy them to the Ubuntu Operating System Under VMware.

CD/Hello

./$ Ndk/ndk-build

The armeabi/libhello. So file is generated in/Hello/libs.

6) test:

Copy the changed project file in Linux to Windows, open it with netbeans, and run


Iii. Summary

1) objective: to call the source file that has been written in C through Java

2) Step 1. c files that are known to be written

2> compile Java code and call the c file of a specific function name

3> compile the written Java code into a. Class file and generate a. h file.

4> compile ndk. h.c and call Objective C through Java.


If you see the above results after a great deal of hard work, I believe that you will be able to see the results ......

The joy of programmers is here ......

I hope to leave a message on any suggestions and comments. I will actively listen to your comments and correct the omissions in the blog.





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.