Android Development-NDK development Hello world!

Source: Internet
Author: User

First NDK Program:

The function done here is to use Java to call C code to implement the output of a helloworld~ 

Need to download the NDK-Baidu, download and install after the decompression can;

Create a new androidproject, and then create a new JNI package. Create a new Jnitest class under the package.

The structure is as follows:

Start The code in the Jnitest file is as follows:

 Package Com.somnus.androidtest.jni;  Public class jnitest {         publicstaticnative  String helloworldfromjni ();}

Layout file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.somnus.androidtest.MainActivity" > <TextView Android:id= "@+id/tv_hello_jni"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/tv_hello_jni"Android:onclick= "OnAction"Android:text= "Click I invoke JNI"/></relativelayout>

Main file code:

 Packagecom.somnus.androidtest;Importcom.somnus.androidtest.jni.JniTest;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {PrivateTextView Tv_hello_jni; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Tv_hello_jni=(TextView) Findviewbyid (R.ID.TV_HELLO_JNI); }     Public voidonAction (View v) {String mstr=Jnitest.helloworldfromjni ();    Tv_hello_jni.settext (MSTR); }}

Go to the chase: ***********************

Start to get our JNI file---->. h file and. mk File

1: Start building our. h file.

The steps are as follows:

Create a new launch configuration icon in the upper left corner. And then I named it: Androidndkdemo

Location is the Javah.exe path of the JDK; Eg:c:\program Files\java\jdk1.8.0_25\bin\javah.exe (This is my JDK path)

Working drectory: ${project_loc} Select from variables

Arguments:-classpath ${project_loc}/bin/classes-d ${project_loc}/jni jni ${java_type_name}

Note In the tab refresh, check the refresh resources upon completion

Common External Tools selected

finally apply;

At this point, you see a directory of more than one JNI folder; The JNI directory has a xxx_xx_xxx_.h file

At this point, in. h is the method in our jnitest. Then we create a new. c file that copies the methods in. h to the. c file, not the parentheses. Do not copy the Jniexport

At this point my. h file is:

Com_somnus_androidtest_jni_jnitest.h

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_somnus_androidtest_jni_jnitest*/#ifndef _included_com_somnus_androidtest_jni_jnitest#define _included_com_somnus_androidtest_jni_jnitest# Ifdef __cplusplusexternC{#endif/** class:com_somnus_androidtest_jni_jnitest * method:helloworldfromjni * Signature: () ljava/lang/string; */jniexport jstring jnicall java_com_somnus_androidtest_jni_jnitest_helloworldfromjni (JNIEnv*, jobject); #ifdef __cplusplus} #endif #endif

Com_somnus_androidtest_jni_jnitest.c

#include <jni.h><string.h>*env, Jobject mstring) {return (*env)- >newstringutf (env, "helloworld! I am from JNI! " );}

New Android.mk File

Local_path: = $ (call my-dir) include $ (clear_vars) #LOCAL_MODULE表示生成的库的名字, the preceding lib and suffix names do not have to be written local_module    := helloworldlocal_src_files:= com_somnus_androidtest_jni_jnitest.cinclude $ (build_shared_library) 

Now we're going to start compiling the code

Open Jnitest.java

or open the external Tools configurations.

A file in new. loaction path to ndk-build.com (download ndk unzipped directory)

Also pay attention to the configuration in refresh and common

OK, run ....

You will see a new libhellowworld.so file in the Libs in the Armeabi folder directory

The final directory structure:

So now we're going back to our jnitest file, write a call method;

Jnitest.java

 Package Com.somnus.androidtest.jni;  Public class jnitest {    static{
Note the name here: HelloWorld is the name we given in the Mk file instead of Libs under the so filename system.loadlibrary ("HelloWorld"); } Public Static native String Helloworldfromjni ();}

Then we run the trigger button to run the diagram:

Ok! The end of the ~ ~

Android Development-NDK development Hello world!

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.