Androidでシステムライブラリを引用する方法と問題
混同を避けるために、まず私のコードの構造を貼り付けます.
まず、新しいAndroidプロジェクト:Android JniTest、Android JniTestActivity.JAvaのコードは次のとおりです.
package mars.com;
import android.app.Activity;
import android.os.Bundle;
public class AndroidJniTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.loadLibrary("test");
CallNative callnative = new CallNative();
byte[] cmd = {(byte)'\u004c'};
int a = callnative.writeCmd(cmd, "/system/lib");
}
}
バッグに入れてcomの下にファイルを作成:CallNative.JAvaコード:
package mars.com;
public class CallNative {
public native byte[] readCmd(String path);
public native int writeCmd(byte[] cmd, String path);
}
Javahツールを使用してローカルライブラリファイルのヘッダファイルmars_を作成com_CallNative.hコード:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class mars_com_CallNative */
#ifndef _Included_mars_com_CallNative
#define _Included_mars_com_CallNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: mars_com_CallNative
* Method: readCmd
* Signature: (Ljava/lang/String;)[B
*/
JNIEXPORT jbyteArray JNICALL Java_mars_com_CallNative_readCmd
(JNIEnv *, jobject, jstring);
/*
* Class: mars_com_CallNative
* Method: writeCmd
* Signature: ([BLjava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_mars_com_CallNative_writeCmd
(JNIEnv *, jobject, jbyteArray, jstring);
#ifdef __cplusplus
}
#endif
#endif
, ,
, :jni, mars_com_CallNative.h , test.c, :
#include"mars_com_CallNative.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include"include/telephony/ril.h"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "keymatch", __VA_ARGS__)
JNIEXPORT jbyteArray JNICALL Java_mars_com_CallNative_readCmd(JNIEnv *evn, jobject obj, jstring jstr)
{
exit(0);
}
JNIEXPORT jint JNICALL Java_mars_com_CallNative_writeCmd(JNIEnv *evn, jobject jobj, jbyteArray jba, jstring jstr)
{
return 56;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
LOGD("JNI ONLOAD success!");
char *path = "/system/lib/libril.so";
void* filehandle0 = dlopen(path, RTLD_LAZY|RTLD_GLOBAL ); // path :/system/lib/libril.so
char *ll;
int pid;
if(filehandle0)
{
LOGD("open so success!");
char*(*requesttostring)(int);
if( 0 == pid)
{
sleep(1);//
requesttostring = (char *(*)(int))dlsym(filehandle0, "requestToString");
pid = fork();
if( requesttostring )
{
LOGD("call function requesttostring OK!");
ll = requesttostring(RIL_REQUEST_GET_NEIGHBORING_CELL_IDS);
//RIL_REQUEST_GET_NEIGHBORING_CELL_IDS include/telephony/ril.h
LOGD("the value of requesttostring is %s", *ll);
}
else
{
LOGD("call function getinformation! ERROR!");
}
LOGD("ok");
}
else if(0 < pid)
{
LOGD("in the parent %s
",getpid());
}
else LOGD("fork error");
}
}
Android hardware/ril include jni ( requesttostring(RIL_REQUEST_GET_NEIGHBORING_CELL_IDS) RIL_REQUEST_GET_NEIGHBORING_CELL_IDS include ril.h )。
jni Android.mk , :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
LOCAL_LDLIBS := -llog
LOCAL_CERTIFICATE := platform
include $(BUILD_SHARED_LIBRARY)
, 。 cygwin $ndk/ndk-build
,F5 , :libs, :libtest.so
, 。 Logcat :
。。。。。。
, 。 、 , /system/lib/libril.so ,
test.c , fork , sleep(1), , ,
else if(0 < pid)
{
LOGD("in the parent %s
",getpid());
}
Logcat : in the parent + pid , , why??
、 Logcat ,
? (libril.so )? , , 。
, , !