android基礎---->NDK的使用

來源:互聯網
上載者:User

標籤:

  NDK的發布,使“Java+C”的開發方式終於轉正,成為官方支援的開發方式。NDK將是Android平台支援C開發的開端,今天我們開始ndk的學習。

 

NDK的簡要說明

ndk是什麼:

  The Native Development Kit (NDK) is a set of tools that allow you to leverage C and C++ code in your Android apps. You can use it either to build from your own source code, or to take advantage of existing prebuilt libraries.

  The NDK is not appropriate for most novice Android programmers, and has little value for many types of Android apps. It is often not worth the additional complexity it inevitably brings to the development process. However, it can be useful in cases in which you need to:

  • Squeeze extra performance out of a device for computationally intensive applications like games or physics simulations.
  • Reuse your own or other developers‘ C or C++ libraries.

什麼情境可以應用ndk:

  • 代碼的保護,由於apk的java層代碼很容易被反編譯,而C/C++庫反編譯難度較大
  • 在NDK中調用第三方C/C++庫,因為大部分的開源庫都是用C/C++代碼編寫的
  • 便於移植,用C/C++寫的庫可以方便在其他的嵌入式平台上再次使用
  • 提高程式的執行效率。將要求高效能的應用邏輯使用C開發,從而提高應用程式的執行效率。

 

使用NDK的Android項目

一、 先下載NDK的jar包,地址:http://192.168.10.75/android-ndk-r10e-windows-x86_64.exe。

點擊直接解壓,得到android-ndk-r10e檔案夾,配置path系統變數:D:\android\ndk\android-ndk-r10e;

在cmd中測試:如下表示環境變數配置成功

 

二、 我們建立一個Android的項目,和普通的android項目一樣。這裡我們用的IDE是eclipse。

我們建立jni檔案夾:

MainActivity中增加native方法,此時MainActivity內容如下:

package com.example.firstndk;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {    // 定義一個native方法    public static native String getStringFromNDK();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

 

二、 根據MainActivity產生標頭檔:

在命令列中做以下的操作:

C:\Users\Linux>cd J:\android\andridtest\eclipseTools\2016-4-13\FirstNDK // 項目的本地目錄C:\Users\Linux>J:J:\android\andridtest\eclipseTools\2016-4-13\FirstNDK>javah -classpath bin/classes;D:\android\ndk\jar\android.jar;D:\android\ndk\jar\android-support-v7-appcompat.jar;D:\android\ndk\jar\android-support-v4.jar -d jni com.example.firstndk.MainActivity

 上述的效果如下,在jni檔案夾中產生com_example_firstndk_MainActivity.h檔案,內容如下:

命令列的格式說明:javah -classpath 編譯項目所需的jar包路徑 -d 編譯檔案的存放位置(相對路徑jni) 編譯的對象類

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_firstndk_MainActivity */#ifndef _Included_com_example_firstndk_MainActivity#define _Included_com_example_firstndk_MainActivity#ifdef __cplusplusextern "C" {#endif#undef com_example_firstndk_MainActivity_BIND_ABOVE_CLIENT#define com_example_firstndk_MainActivity_BIND_ABOVE_CLIENT 8L#undef com_example_firstndk_MainActivity_BIND_ADJUST_WITH_ACTIVITY#define com_example_firstndk_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L#undef com_example_firstndk_MainActivity_BIND_ALLOW_OOM_MANAGEMENT#define com_example_firstndk_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L#undef com_example_firstndk_MainActivity_BIND_AUTO_CREATE#define com_example_firstndk_MainActivity_BIND_AUTO_CREATE 1L#undef com_example_firstndk_MainActivity_BIND_DEBUG_UNBIND#define com_example_firstndk_MainActivity_BIND_DEBUG_UNBIND 2L#undef com_example_firstndk_MainActivity_BIND_IMPORTANT#define com_example_firstndk_MainActivity_BIND_IMPORTANT 64L#undef com_example_firstndk_MainActivity_BIND_NOT_FOREGROUND#define com_example_firstndk_MainActivity_BIND_NOT_FOREGROUND 4L#undef com_example_firstndk_MainActivity_BIND_WAIVE_PRIORITY#define com_example_firstndk_MainActivity_BIND_WAIVE_PRIORITY 32L#undef com_example_firstndk_MainActivity_CONTEXT_IGNORE_SECURITY#define com_example_firstndk_MainActivity_CONTEXT_IGNORE_SECURITY 2L#undef com_example_firstndk_MainActivity_CONTEXT_INCLUDE_CODE#define com_example_firstndk_MainActivity_CONTEXT_INCLUDE_CODE 1L#undef com_example_firstndk_MainActivity_CONTEXT_RESTRICTED#define com_example_firstndk_MainActivity_CONTEXT_RESTRICTED 4L#undef com_example_firstndk_MainActivity_MODE_APPEND#define com_example_firstndk_MainActivity_MODE_APPEND 32768L#undef com_example_firstndk_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING#define com_example_firstndk_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L#undef com_example_firstndk_MainActivity_MODE_MULTI_PROCESS#define com_example_firstndk_MainActivity_MODE_MULTI_PROCESS 4L#undef com_example_firstndk_MainActivity_MODE_PRIVATE#define com_example_firstndk_MainActivity_MODE_PRIVATE 0L#undef com_example_firstndk_MainActivity_MODE_WORLD_READABLE#define com_example_firstndk_MainActivity_MODE_WORLD_READABLE 1L#undef com_example_firstndk_MainActivity_MODE_WORLD_WRITEABLE#define com_example_firstndk_MainActivity_MODE_WORLD_WRITEABLE 2L#undef com_example_firstndk_MainActivity_DEFAULT_KEYS_DIALER#define com_example_firstndk_MainActivity_DEFAULT_KEYS_DIALER 1L#undef com_example_firstndk_MainActivity_DEFAULT_KEYS_DISABLE#define com_example_firstndk_MainActivity_DEFAULT_KEYS_DISABLE 0L#undef com_example_firstndk_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL#define com_example_firstndk_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L#undef com_example_firstndk_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL#define com_example_firstndk_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L#undef com_example_firstndk_MainActivity_DEFAULT_KEYS_SHORTCUT#define com_example_firstndk_MainActivity_DEFAULT_KEYS_SHORTCUT 2L#undef com_example_firstndk_MainActivity_RESULT_CANCELED#define com_example_firstndk_MainActivity_RESULT_CANCELED 0L#undef com_example_firstndk_MainActivity_RESULT_FIRST_USER#define com_example_firstndk_MainActivity_RESULT_FIRST_USER 1L#undef com_example_firstndk_MainActivity_RESULT_OK#define com_example_firstndk_MainActivity_RESULT_OK -1L#undef com_example_firstndk_MainActivity_HONEYCOMB#define com_example_firstndk_MainActivity_HONEYCOMB 11L#undef com_example_firstndk_MainActivity_MSG_REALLY_STOPPED#define com_example_firstndk_MainActivity_MSG_REALLY_STOPPED 1L#undef com_example_firstndk_MainActivity_MSG_RESUME_PENDING#define com_example_firstndk_MainActivity_MSG_RESUME_PENDING 2L/* * Class:     com_example_firstndk_MainActivity * Method:    getStringFromNDK * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_example_firstndk_MainActivity_getStringFromNDK  (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif
com_example_firstndk_MainActivity.h

 主要有用的內容是聲明了一個Java_com_example_firstndk_MainActivity_getStringFromNDK的方法:

JNIEXPORT jstring JNICALL Java_com_example_firstndk_MainActivity_getStringFromNDK  (JNIEnv *, jclass);

 

三、 在jni檔案夾下,建立一個C檔案,我們命令為:hellondk.c

#include<stdio.h>#include "com_example_firstndk_MainActivity.h"JNIEXPORT jstring JNICALL Java_com_example_firstndk_MainActivity_getStringFromNDK  (JNIEnv * env, jclass jclass) {    return (*env)->NewStringUTF(env, "Hello, NDK.");}

 在jni中建立一個Android.mk檔案(可以從官方文檔的項目複製過來,做修改就可)。

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hellondkLOCAL_SRC_FILES := hellondk.cinclude $(BUILD_SHARED_LIBRARY)

 LOCAL_SRC_FILES就是我們建立的C代碼的源檔案hellondk.c,而LOCAL_MODULE就是hellondk。其它的不需要做改動。

 

四、 在MainActivity中增加對JNI的調用,此時的MainActivity.java內容如下:

package com.example.firstndk;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private TextView textView;    // 定義一個native方法    public static native String getStringFromNDK();        static {        System.loadLibrary("hellondk");    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                textView = (TextView) findViewById(R.id.textView);        textView.setText(getStringFromNDK());    }}

命令列中編譯項目:ndk-build,產生obj檔案夾

J:\android\andridtest\eclipseTools\2016-4-13\FirstNDK>ndk-buildAndroid NDK: WARNING: APP_PLATFORM android-21 is larger than android:minSdkVersion 11 in ./AndroidManifest.xml[armeabi] Compile thumb  : hellondk <= hellondk.c[armeabi] SharedLibrary  : libhellondk.so[armeabi] Install        : libhellondk.so => libs/armeabi/libhellondk.so

 產生的obj內容大致如下:

五、 在模擬器中運行項目,得到結果:

最終的項目圖:

 

友情連結

 具體的視頻地址:  http://pan.baidu.com/s/1dEEO5Mh

android基礎---->NDK的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.