星期三, 5月 01, 2013

Android NDK Sample

Create a regular Android project.
The project has a package name of com.miles.ndk with a default Activity name of
AndroidNDKSample1Activity
In Linux,
Create a ndk1 directory, and create a directory name jni in this directory.
Like this:
/var/tmp/miles/ndk1
/var/tmp/miles/ndk1/jni

add native.c in /var/tmp/miles/ndk1/jni
add Android.mk in /var/tmp/miles/ndk1/jni
native.c ==>
// -----------------------------------------------------
#include <jni.h>
#include <string.h>
#include <android/log.h>


#define DEBUG_TAG "NDK_AndroidNDKSample1Activity"
void Java_com_miles_ndk_AndroidNDKSample1Activity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
    jboolean isCopy;
    const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
    (*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
// ----------------------------------------------------

Android.mk ==>
// ----------------------------------------------------
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE    := ndk1
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)
// -----------------------------------------------------
cd /var/tmp/miles/ndk1
/var/tmp/miles/android/NDK/android-ndk-r8/ndk-build
copy /var/tmp/miles/ndk1/libs to Android directory
// ======================================================
AndroidNDKSample1Activity.java ==>
package com.miles.ndk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class AndroidNDKSample1Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    }

    public void but1_onclick(View v){
       TextView tv = (TextView)findViewById(R.id.textView1);
       tv.setText("Hello World!!");
       helloLog("This will log to LogCat via the native call.");
    }

    private native void helloLog(String logThis);

    static {
       System.loadLibrary("ndk1");
    }
}
// =====================================================
main.xml ⇒
// =====================================================
   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:text="Button" android:id="@+id/button1" android:layout_height="wrap_content" android:onClick="but1_onclick" android:layout_width="wrap_content"></Button>

<TextView android:text="TextView" android:layout_weight="0.08" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="99dp"></TextView>

</LinearLayout>

沒有留言: