Android NDK Development (III): NDK development steps

Source: Internet
Author: User

Start to learn about NDK development. First, start with the output of the Hello World instance.

NDK development steps:

1. Declare the native method in the Java File

Public class TestActivity extends Activity {static {System. loadLibrary ("my-ndk");} // declare the native method of the jni layer and use the native keyword public native String stringFromJNI (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TextView tvText = new TextView (this); tvText. setText (stringFromJNI (); setContentView (tvText );}}


2. Compile a Java File

Compile a Java file that declares the native method using the javac command directly.

The shamoo project directory is in E: \ project \ test

In cmd, enter cd E: \ project \ test

Javac src \ com \ shamoo \ jni \ HelloJni. java-d bin \ classes

-D specifies the output directory of the. class file.

You can also use eclipse to directly generate the generated directory in the bin \ classes of the project directory.


3. Use the javah command to generate the header file

With regard to the javah command, Shamoo has been tossing around for a while and has been failing to use the command. It always prompts that the class of the Android sdk cannot be accessed. Later, you can use-classpath to specify the src location, the specific cause is unknown...

Javah-classpath src-d jni-jni com. shamoo. activity. TestActivity

If the class that declares the native method does not use the Android sdk, you can use the following command. For example, HelloJni declares the navtive method.

Javah-classpath bin/classes-d jni-jni com. shamoo. jni. HelloJni

-Classpath specifies the root directory of the. class file-d specifies the output directory-jni specifies the class that declares the native method. Javah decompile the header file based on the. class file.

The generated header file is as follows:

/* DO NOT EDIT THIS FILE - it is machine generated */#include 
 
  /* Header for class com_shamoo_activity_TestActivity */#ifndef _Included_com_shamoo_activity_TestActivity#define _Included_com_shamoo_activity_TestActivity#ifdef __cplusplusextern "C" {#endif/* * Class:     com_shamoo_activity_TestActivity * Method:    stringFromJNI * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_shamoo_activity_TestActivity_stringFromJNI  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
 


From the original file, we can see that the native method must specify the Java + native method package name (replaced by an underscore) + method name

4. Compile the source file com_shamoo_activty_TestActivity.cpp corresponding to the header file.

#include 
 
  #include 
  
   #include 
   
    JNIEXPORT jstring JNICALL Java_com_shamoo_activity_TestActivity_stringFromJNI  (JNIEnv *env, jobject) {printf("Hello World");return env->NewStringUTF("Hello World");}
   
  
 
Is it familiar? printf ("Hello World"); but printf will not be output to the Log by default. You need to set it. You do not need to write the extern "C" mark again When writing the source file this time, because the automatically generated header file has already been completed for us!


5. Modify the Android. mk File

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := my-ndkLOCAL_SRC_FILES := my-ndk.cpp com_shamoo_activity_TestActivity.cppLOCAL_LALIBS += -lloginclude $(BUILD_SHARED_LIBRARY)

Android. mk is actually a Makefile. Analyze the preceding environment variables.

LOCAL_PATH specifies the source code directory, $ (call my-dir). I have learned Linux Shell and can't get familiar with it anymore.

The. so library compiled by LOCAL_MODULE

LOCAL_SRC_FILES: Specifies the source file to be compiled.

LOCAL_LALIBS specifies other libraries with-l + database names. For example, to use the ndk log, you must use the log library.

Include imports some Makefile fragments. For example, if CLEAR_VARS is imported, the previous environment variables are cleared before configuration to avoid being affected by other environment variables during compilation. BUILD_SHARED_LIBRARY is used to import other shared libraries.


Okay, compile and execute it. If there is no problem, you will see the Hello World text in an Activity!

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.