Getting started with JNI-learning notes and getting started with jni

Source: Internet
Author: User

Getting started with JNI-learning notes and getting started with jni
Getting started with JNI-learning notes

Executable File Windows -*. exe | linux -*. elfc function library file Windows -*. dll | linux -*. so batch file Windows -*. bat | linux -*. sh
Tools

NDK -- native developer kits

Cygwin-Linux System Simulator

  • Installation Process key points: -- can be installed online during installation or through a local file (if there is a local file) -- Select Packages: No need to install all, install Devel and Shells. After installation, double-click the desktop shortcut icon to go to The Cygwin command console. Command: cd .. | cd .. | ls | cd cygdrive
  • To facilitate the execution of the ndk-build command in any directory, You need to configure environment variables: to the installation directory of Cygwin .. /cygwin/etc/profile: Open the profile file and modify the PATH value. Multiple environment variables are separated by colons. For example: PATH = "...:/cygdrive/c/android-ndk-r7b:...", if not, you can also configure the system environment variables in Windows to run with cmd

CDT-to facilitate c code development in eclipse, highlight c code

  • Full name: CDT: c/c ++ developer tools ADT: android developer tools
  • Installation Procedure: eclipse --> help --> install new software --> add --> Name: cdt | Location: adt.zip --> next. There are two plug-ins for you to select CDT Main Features and cdt Optional Features. You only need to select the former and Contact all update sites... this option is for online updates. You can skip this option. next. After installation, there will be a C/C ++ view in eclipse.
HelloWorld for JNI Development

Step 1:Create an Android project and define a local method

Public class MainActivity extends Activity {// defines a local method, which is equivalent to a c interface. You need to use c code to implement public native String helloFromC (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}...}

Step 2:Create a new folder named jni under the project, which must be the file name, and then create a new file under the folder, such as Hello. c. write c code in the C file. The method name is long and Quick Generation Method: cmd -- cd to the src directory of the project, enter the command javah com. example. helloworldformc. mainActivity (full class name) will generate. h header file, copy the file to the jni directory, open the file copy method, remove the semicolon and add {}.

# Include <stdio. h> # include <jni. h> // Java + full class name + method name. Use _ to connect each word jstring encode (JNIEnv * env, jObject obj) {return (* env)-> NewStringUTF (env, "hellofromc ");}

Step 3:Create an Android. mk file in the jni folder

LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) # package the name of the function library. What file name do you want to package into? What is it written here? LOCAL_MODULE: = hello # name of the file corresponding to C code: LOCAL_SRC_FILES: = Hello. cinclude $ (BUILD_SHARED_LIBRARY)

Step 4:

Package C code into a function library: cmd --> cd to the directory where the Android project is located --> ndk-build (cross-compilation command), refresh the project, and there will be one in libs/armeabi. so file, the file name is lib + hello (the Library name defined in mk), and an obj folder will be added in the project, mainly storing intermediate files

Step 5:Introduce function libraries in Java code

// Use a static code block to introduce the function library static {System. loadLibrary ("hello");} to Java code ");}

Step 6:

Public void method () {// call String str = helloFromC (); Toast. makeText (getApplicationContext (), str, 1). show ();}

** Note **

  • When referencing other's. so function libraries, you need to use your native method to match the package name of the corresponding class with that of the. so function library.
  • To print logs in C, you need to introduce the header file.

    Eg: # include <android/log. h>

    However, the log method name is too long to be remembered. We need to handle it.

    Eg: # define LOGTAG "clog" # define LOGD (...)AndroidLogPrint (ANDROIDLOGDEBUG, LOGTAG, _ VAARGS) # Define LOGI (...)AndroidLogPrint (ANDROIDLOGINFO, LOGTAG ,_VAARGS)

    Add LOCAL_LDLIBS + =-llog to the mk file.

    Call LOGD ("x = % d", x) like this in C code );

C. Call Java

Principle: C calls the Java method through the reflection method. C has similar methods to get the class object, get the methodId, and call the Five return value method CallVoidMethod. The above methods can be used in the header file jni. find in h, jni. h Directory: android-ndk-r7b \ platforms \ android-8 \ arch-arm \ usr \ include through these methods can realize C code call Java code to obtain the method signature: the full Class Name of the class where the javap-s method is located under the classes folder under the cd to bin directory

C
The header file *. h library file *. so/*. o is equivalent to the interface file in Java, and the library file is equivalent to the implementation class file in Java.


Tool download: http://pan.baidu.com/s/1mg2zvN6

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.