java - 在 android : UnsatisfiedLinking Error and Force closing Application 中使用 jni/ndk

标签 java android android-ndk java-native-interface

我正在 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 并且应用程序强制关闭。另外,我得到了 no implementation found for native function。

最佳答案

您的包名称不完全相同。将 ndkfoo.c 中的名称更改为

 #include <string.h>
  #include <jni.h>

  jstring Java_com_example_myndkapp_MainActivity_invokeNativeFunction(JNIEnv* env, jobject      javaThis) {
  return (*env)->NewStringUTF(env, "Hello from native code!");
 }

关于java - 在 android : UnsatisfiedLinking Error and Force closing Application 中使用 jni/ndk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375883/

相关文章:

java - keystore 被篡改,或密码不正确]仍然无法工作

java - spring jpa 集成错误

android - 如何将 signal() 移植到 sigaction()?

Android NDK 将长值传递给 native 方法

java - 如何重置 radio_button.setButtonDrawable(null)

java - 从多个列表生成所有组合

android - 在不使用位图的情况下防止 LayeredList 中的形状缩放

android - 无法从 BroadcastReceiver 调用 IntentService

java - Android,将图像置于特定点的中心

Android gradle-experimental 0.2.0 添加res和aidl