私はAndroidのネイティブ側にまったく慣れていません。Sylvain Ratabouil (PACKT Publishing) による「Android NDK ビギナーズ ガイド」に従って、ネイティブ開発の基礎を学んでいます。私はすでに本の中でいくつかの問題に遭遇し、グーグルで答えを見つけました. しかし、他の誰もこの問題を抱えていないようです。以下は私のコードです
MainActivity.java
package com.jni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
public native String getMyData();
static{
System.loadLibrary("mylib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle(getMyData());
}
@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;
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := com_jni_MainActivity.c
include $(BUILD_SHARED_LIBRARY)
com_jni_MainActivity.c
#include "com_jni_MainActivity.h"
JNIEXPORT jstring Java_com_jni_MainActivity_getMyData(JNIEnv* pEnv, jobject pThis){
return (*pEnv)->NewStringUTF(pEnv, "Sample Title");
}
com_jni_MainActivity.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_MainActivity */
#ifndef _Included_com_jni_MainActivity
#define _Included_com_jni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_jni_MainActivity_MODE_PRIVATE
#define com_jni_MainActivity_MODE_PRIVATE 0L
#undef com_jni_MainActivity_MODE_WORLD_READABLE
#define com_jni_MainActivity_MODE_WORLD_READABLE 1L
#undef com_jni_MainActivity_MODE_WORLD_WRITEABLE
#define com_jni_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_jni_MainActivity_MODE_APPEND
#define com_jni_MainActivity_MODE_APPEND 32768L
#undef com_jni_MainActivity_BIND_AUTO_CREATE
#define com_jni_MainActivity_BIND_AUTO_CREATE 1L
#undef com_jni_MainActivity_BIND_DEBUG_UNBIND
#define com_jni_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_jni_MainActivity_BIND_NOT_FOREGROUND
#define com_jni_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_jni_MainActivity_CONTEXT_INCLUDE_CODE
#define com_jni_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_jni_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_jni_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_jni_MainActivity_CONTEXT_RESTRICTED
#define com_jni_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_jni_MainActivity_RESULT_CANCELED
#define com_jni_MainActivity_RESULT_CANCELED 0L
#undef com_jni_MainActivity_RESULT_OK
#define com_jni_MainActivity_RESULT_OK -1L
#undef com_jni_MainActivity_RESULT_FIRST_USER
#define com_jni_MainActivity_RESULT_FIRST_USER 1L
#undef com_jni_MainActivity_DEFAULT_KEYS_DISABLE
#define com_jni_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_jni_MainActivity_DEFAULT_KEYS_DIALER
#define com_jni_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_jni_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_jni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
/*
* Class: com_jni_MainActivity
* Method: getMyData
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_jni_MainActivity_getMyData
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
プログラムを実行しようとすると、クラッシュします。logcat は次のメッセージを表示します
10-08 10:38:36.093: E/AndroidRuntime(31641): FATAL EXCEPTION: main
10-08 10:38:36.093: E/AndroidRuntime(31641): java.lang.UnsatisfiedLinkError: Couldn't load mylib from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.jni-1.apk,libraryPath=/data/app-lib/com.jni-1]: findLibrary returned null
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.Runtime.loadLibrary(Runtime.java:365)
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.System.loadLibrary(System.java:535)
10-08 10:38:36.093: E/AndroidRuntime(31641): at com.jni.MainActivity.<clinit>(MainActivity.java:11)
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.Class.newInstanceImpl(Native Method)
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.Class.newInstance(Class.java:1319)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.ActivityThread.access$600(ActivityThread.java:150)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.os.Looper.loop(Looper.java:137)
10-08 10:38:36.093: E/AndroidRuntime(31641): at android.app.ActivityThread.main(ActivityThread.java:5191)
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 10:38:36.093: E/AndroidRuntime(31641): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 10:38:36.093: E/AndroidRuntime(31641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-08 10:38:36.093: E/AndroidRuntime(31641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-08 10:38:36.093: E/AndroidRuntime(31641): at dalvik.system.NativeStart.main(Native Method)
10-08 10:38:37.882: I/Process(31641): Sending signal. PID: 31641 SIG: 9
libs->armeabi には libmylib.so があります。私は何が間違っているのか分かりません