Android -- NDK開發入門

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   os   io   

第一步,建立一個普通的Android項目HelloNDK,然後在與src同一級的目錄下建立一個jni目錄;

第二步,在jni目錄下建立一個hello_ndk.c檔案,代碼如下:

#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>/********擷取字串**************/jstring Java_com_example_hellondk_MainActivity_readJNIString( JNIEnv* env, jobject obj){    return (*env)->NewStringUTF(env, "Hello from JNI --- 22222 !");}

說明如下:

Java_com_example_hellondk_MainActivity_readJNIString // 方法名,由三部分組成,Java + [com_example_hellondk_HelloNDKActivity](包名+activity名) + [readJNIString](在java代碼中調用的方法名)
(*env)->NewStringUTF(env, "Hello from JNI --- 22222 !"); //在java中接收的傳回值,不能直接return,一定要調用(*env)->NewStringUTF()這個方法返回

第三步,在jni目錄下建立一個Android.mk檔案,代碼如下:

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)

說明如下:

LOCAL_MODULE    := hello_ndk  //這個與產生.so檔案名稱有關,和匯入Native Support的.so檔案有關
LOCAL_SRC_FILES := hello_ndk.c //這個是讀取相關的C檔案名稱

第四步,配置android項目的native support,選中項目右鍵Android Tools--Add Native Support, 開啟Add Android Native Support對話方塊,輸入第三步中配置的LOACL_MODULE的值,如:

第五步,修改MainActivity.java檔案,代碼如下:

package com.example.hellondk;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {    TextView mTestTv;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);                    mTestTv = (TextView)findViewById(R.id.test_tv);          mTestTv.setText(readJNIString());      }            private native String readJNIString();            static {          System.loadLibrary("hello_ndk");//引入hello_ndk.so      }      @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

說明如下:

項目啟動並執行時候首先會執行static裡面的方法,引入hello_ndk.so,然後執行onCreate裡的方法,讀取C檔案裡的方法擷取傳回值並顯示在TextView控制項上。

第六步,運行Android Application項目,程式會首先產生.so檔案,成功後繼續運行android項目,產生.so的過程如下:

我是天王蓋地虎的分割線                                                             

原始碼:http://pan.baidu.com/s/1dD1Qx01

HelloNDK.zip

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.