我在 ndk/jni android 上试用 Hello World 程序。
这是我的java代码:
package com.example.myndkapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
// load the library - name matches jni/Android.mk
static {
Log.d("TAG1","Before Load");
System.loadLibrary("ndkfoo");
Log.d("TAG2","After Load");
}
// declare the native code function - must match ndkfoo.c
private native String invokeNativeFunction();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("TAG3","Set Content View Called");
// this is where we call the native code
try{
String hello = invokeNativeFunction();
Log.d("TAG4","InvokeFunc Called..");
new AlertDialog.Builder(this).setMessage(hello).show();
}catch(Exception e){
e.printStackTrace();
}
}
}
ndkfoo.c 代码:
#include <string.h>
#include <jni.h>
jstring Java_com_example_ndkfoo_MAinActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
我的 Android.mk 文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c
include $(BUILD_SHARED_LIBRARY)
我的 AndroidMainfest.xml 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myndkapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我收到错误 UnsatisfiedLinking Error 并且应用程序强制关闭。另外,我没有找到本机功能的实现。