Android-Getting Started with NDK development

Source: Internet
Author: User

The first step is to create a generic Android project HELLONDK, and then create a new JNI directory in the same directory as SRC;

In the second step, create a new hello_ndk.c file under the JNI directory with the following code:

#include <string.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/ioctl.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<jni.h>#include<android/log.h>/******** Gets the string **************/jstring java_com_example_hellondk_mainactivity_readjnistring (jnienv*env, Jobject obj) {    return(*env)->newstringutf (env,"Hello from JNI---22222!");}

The description is as follows:

// method name, consisting of three parts, Java + [com_example_hellondk_hellondkactivity] (package name +activity name) + [Readjnistring] (method name called in Java code)
" Hello from JNI---22222! " // The return value received in Java cannot be returned directly, be sure to call (*env)->newstringutf () This method returns

In the third step, create a new android.mk file under the JNI directory with the following code:

Local_path: = $ (call my-dir)    include $ (clear_vars)    local_module    := hello_ndk  Local_src_files:= hello_ndk.c    local_ldlibs    :=-llog    include $ (build_shared_ LIBRARY)

The description is as follows:

Local_module    : = HELLO_NDK  // This is related to generating. So file names, and importing native support for. so files
// This is read-related c file name

Fourth step, configure the Android project Native support, select the project right-click android tools--add Native Support, open the ADD Android Native Support dialog box, enter the Loacl configured in the third step The value of _module, such as:

The fifth step, modify the Mainactivity.java file, the code is as follows:

 Packagecom.example.hellondk;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {TextView Mtesttv; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                    Setcontentview (R.layout.activity_main); Mtesttv=(TextView) Findviewbyid (R.ID.TEST_TV);      Mtesttv.settext (Readjnistring ()); }            Private nativeString readjnistring (); Static{system.loadlibrary ("Hello_ndk");//introduction of Hello_ndk.so} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

The description is as follows:

When the project runs, it first executes the static Inside method, introduces Hello_ndk.so, then executes the method in the OnCreate, reads the method in the C file to get the return value and displays it on the TextView control.

Sixth step, run the Android Application project, the program will first generate the. so file, after successful continue to run the Android project, the process of generating. So is as follows:

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Hellondk.zip

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.