我目前正在尝试在Android中使用JNI,但每次我在设备上启动应用程序时都会遇到问题,它会崩溃。
这是我的主要活动:
public class MainActivity extends ActionBarActivity {
static {
System.loadLibrary("native");
}
public native String invokeNativeFunction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
String hello = invokeNativeFunction();
new AlertDialog.Builder(this).setMessage(hello).show();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}}
我还创建了我的C文件并对其进行了编译:
jstring Java_com_JuniorAndroid_TUNandroid_Hellondk_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");}
我还有.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := native
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)
但是无论何时我在手机上执行该操作,我都有:
06-10 15:18:33.326: D/dalvikvm(12806): Trying to load lib /data/data/com.JuniorAndroid.TUNandroid.hellondk/lib/libnative.so 0x413bead0
06-10 15:18:33.336: D/dalvikvm(12806): Added shared lib /data/data/com.JuniorAndroid.TUNandroid.hellondk/lib/libnative.so 0x413bead0
06-10 15:18:33.336: D/dalvikvm(12806): No JNI_OnLoad found in /data/data/com.JuniorAndroid.TUNandroid.hellondk/lib/libnative.so 0x413bead0, skipping init
06-10 15:18:33.506: W/dalvikvm(12806): No implementation found for native Lcom/JuniorAndroid/TUNandroid/hellondk/MainActivity;.invokeNativeFunction ()Ljava/lang/String;
06-10 15:18:33.516: D/AndroidRuntime(12806): Shutting down VM
06-10 15:18:33.516: W/dalvikvm(12806): threadid=1: thread exiting with uncaught exception (group=0x40aba210)
06-10 15:18:33.526: E/AndroidRuntime(12806): FATAL EXCEPTION: main
06-10 15:18:33.526: E/AndroidRuntime(12806): java.lang.UnsatisfiedLinkError: invokeNativeFunction
06-10 15:18:33.526: E/AndroidRuntime(12806): at com.JuniorAndroid.TUNandroid.hellondk.MainActivity.invokeNativeFunction(Native Method)
06-10 15:18:33.526: E/AndroidRuntime(12806): at com.JuniorAndroid.TUNandroid.hellondk.MainActivity.onCreate(MainActivity.java:34)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.Activity.performCreate(Activity.java:4465)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.os.Looper.loop(Looper.java:137)
06-10 15:18:33.526: E/AndroidRuntime(12806): at android.app.ActivityThread.main(ActivityThread.java:4448)
06-10 15:18:33.526: E/AndroidRuntime(12806): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 15:18:33.526: E/AndroidRuntime(12806): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 15:18:33.526: E/AndroidRuntime(12806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
06-10 15:18:33.526: E/AndroidRuntime(12806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
06-10 15:18:33.526: E/AndroidRuntime(12806): at dalvik.system.NativeStart.main(Native Method)
我已经搜索了很多时间来找到答案,但我找不到任何东西。
感谢您的帮助!
答案 0 :(得分:0)
您需要将C中的函数声明为JNIEXPORT。如果使用C ++而不是C,你还需要声明它为extern“C”。第一个使它在.so中可见,第二个确保C ++编译器不进行名称修改。