Android Learning Note-windows A simple example of NDK development

Source: Internet
Author: User
Tags mul

This example assumes that the Android development environment has been built and the NDK has been configured successfully;

1. Create a new Android project on Eclipse with the name Ndkdemo. Modify Res\layout\activity_main.xml

<Relativelayoutxmlns: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"Tools:context= "${relativepackage}.${activityclass}" >    <TextViewAndroid:id= "@+id/result"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>

2, in the project src Com.example.ndkdemo directory Right-click New class Nativeload.java content as follows

 PackageCom.example.ndkdemo; Public classNativeload {Static {        /*loading the underlying so library in a static block*/System.loadlibrary ("Calculator"); }        /*declares the native method, in fact the existing C code implementation*/     Public Static native intAddintXinty);  Public Static native intSubintXinty);  Public Static native intMulintXinty);  Public Static native intDivintXinty);}

3. Modify the Mainactivity.java in the Com.example.ndkdemo directory so that the calculation results are displayed on the screen

 PackageCom.example.ndkdemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TextView TextView=(TextView) Findviewbyid (R.id.result); Textview.settext ("+ + =" + nativeload.add (+ +) + "\ n"); Textview.append ("100-10 =" + nativeload.sub (+) + "\ n"); Textview.append ("Ten *" + nativeload.mul (+) + "\ n"); Textview.append ("100/10 =" + nativeload.div (+) + "\ n"); }}

4, command line into the project directory, execute javah command, generate header file

Javah-classpath src com.example.ndkdemo.NativeLoad
Javah-classpath bin\classes Com.example.ndkdemo.NativeLoad

This step took a long time, some hints com.example.ndkdemo.NativeLoad not found, some hint Android.app.Activity not found.

Some on the internet said to go into the SRC directory to execute the command, some say into the bin directory to execute. <android Framework > said class generated classes file in the Bin\com\example\ndkdemo\ directory, consensus, now according to their own understanding summarized as follows

    • In which directory execution is not critical, primarily the directory specified after the-classpath parameter, you can specify the package where the class file resides, or the package where the Java file resides
    • Perhaps the previous version of the directory structure is Bin\com\example\ndkdemo, but this version of me in the Bin directory has a classes directory, is such a bin\classess\com\example\ndkdemo
    • Each of the two above specifies the package where the Java file resides and the generated class file.
    • Classpath is not the directory where the specified class resides, but the path of the package that specifies the class, and the SRC or bin\classess directory

5, in the project right-click New JNI directory, in the JNI directory to create a new calculator.c file, the content is as follows

#include <jni.h>jniexport jint jnicall java_com_example_ndkdemo_nativeload_add (jnienv*env, Jclass Jclazz, Jint x, Jint y) {    returnX +y;}/** class:com_example_ndkdemo_nativeload * method:sub * Signature: (II) I*/jniexport jint jnicall java_com_example_ndkdemo_nativeload_sub (jnienv*env, Jclass Jclazz, Jint x, Jint y) {    returnX-y;}/** class:com_example_ndkdemo_nativeload * Method:mul * Signature: (II) I*/jniexport jint jnicall java_com_example_ndkdemo_nativeload_mul (jnienv*env, Jclass Jclazz, Jint x, Jint y) {    returnX *y;}/** class:com_example_ndkdemo_nativeload * method:div * Signature: (II) I*/jniexport jint jnicall java_com_example_ndkdemo_nativeload_div (jnienv*env, Jclass Jclazz, Jint x, Jint y) {    returnx/y;}

6. Create a new compilation script under the JNI directory android.mk

Local_path: = $ (call my-dir) include $ (clear_vars) Local_module:= calculatorlocal_src_files:=  Calculator.cinclude $ (build_shared_library)

Local_path: Specifies the basic file directory that the NDK compiles, My-dir is a macro definition, the user returns the directory where the Mk file is located, which means to append the path of the MK (call My-dir) obtained to Local_path

Include $ (clear_vars): Used to initialize a variable in the Mk file in local_xxx format as a global variable

Local_module: The name of the generated dynamic library must be consistent with the name in System.loadlibrary in Java code

Local_src_files: Source files required to generate a dynamic library

Include $ (build_shared_library): To create a dynamic library in the libxxx.so format

7, the command line into the project directory, the execution of the Ndk-build command, that is, in the Libs directory to generate the corresponding dynamic library files, the directory structure as shown

8, in the project right-click->run as->android application can be run on the real machine, the results of the operation as shown

Android Learning Note-windows A simple example of NDK development

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.