日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jni调试

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jni调试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

12年的時候寫過JNI但是又忘記得差不多了,現在重新寫了一次,發現碰到了幾個問題,寫下來記錄一下


第一步

應用程序java代碼

package com.example.helloworld;import java.util.Calendar;import android.os.Bundle; import android.app.Activity; import android.app.AlarmManager; import android.util.Log;public class AlarmTest extends Activity {AlarmManager aManager;Calendar currentTime = Calendar.getInstance();public static final int POWER_OFF_WAKE_UP = 8;//用來設置關機啟動的參數 平臺這邊已經設置好了@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.e("weiqifa", test());}public native String test();static {try{Log.i("JNI", "Trying to load libhelloworld.so");System.loadLibrary("helloWorld");}catch(UnsatisfiedLinkError ule){Log.e("JNI", "Warning : could not load the libhelloworld.so");}} }

編譯一下,可以是eclipse編譯,也可以是java命令編譯

編譯后有下面的代碼結構和目錄

weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ ls
AndroidManifest.xml? classes? classes.dex? dexedLibs? helloWorld.apk? res? resources.ap_
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$


第二步

生成共享頭文件 這一步是最關鍵的關系到成功和失敗


weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ javah -d header -classpath classes -jni com.example.helloworld.AlarmTest
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$


這個命令比較關鍵 javah -d header 指的是在當前目錄下面生成這個頭文件,-classpath是指定classes的目錄就是第一步里面生成的


-jni指的是生成jni頭文件 com.example.helloworld這個是包名 AlarmTest是類名 這些都不能有錯


生成后如下

weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ ls
AndroidManifest.xml? classes? classes.dex? dexedLibs? header? helloWorld.apk? res? resources.ap_
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ ls header/
com_example_helloworld_AlarmTest.h
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$


weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ cat header/com_example_helloworld_AlarmTest.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_helloworld_AlarmTest */#ifndef _Included_com_example_helloworld_AlarmTest #define _Included_com_example_helloworld_AlarmTest #ifdef __cplusplus extern "C" { #endif #undef com_example_helloworld_AlarmTest_POWER_OFF_WAKE_UP #define com_example_helloworld_AlarmTest_POWER_OFF_WAKE_UP 8L /** Class: com_example_helloworld_AlarmTest* Method: test* Signature: ()Ljava/lang/String;*/ JNIEXPORT jstring JNICALL Java_com_example_helloworld_AlarmTest_test(JNIEnv *, jobject);#ifdef __cplusplus } #endif #endif
剛開始不知道,運行經常出錯如下:

weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$ javah -jni com.example.helloworld
error: cannot access com.example.helloworld
class file for com.example.helloworld not found
javadoc: error - Class com.example.helloworld not found.
Error: No classes were specified on the command line.? Try -help.
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/bin$


可以看到自動生成對應的函數:Java_com_example_helloworld_AlarmTest_test

    Java_ + 包名(com_example_helloworld+ 類名(AlarmTest) + 接口名(test):必須要按此JNI規范來操作;


第三步

通過.h來制作.c

看看我們的.c 我們只是需要.h里面那個函數名字,.h沒有被引用,也沒有參與編譯和預編譯

helloworld.c如下:

#include <string.h> #include <jni.h>JNIEXPORT jstring JNICALL Java_com_example_helloworld_AlarmTest_test(JNIEnv *env, jobject thiz) {return (*env)->NewStringUTF(env,"weiqifa hello for JNI"); }

第四步

生成so文件

看一下我們的mk文件

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS)LOCAL_MODULE := helloWorld LOCAL_SRC_FILES := helloWorld.cinclude $(BUILD_SHARED_LIBRARY)

會生成如下的so文件:
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/libs/armeabi$ ls
libhelloWorld.so
weiqifa@weiqifa-Inspiron-3847:~/workspace/helloWorld/libs/armeabi$



第五步

執行驗證

直接用eclipse安裝 或者用 adb push 進去 用adb的話要手動安裝so 文件并且改一下權限


10-20 03:06:45.241: E/weiqifa(12194): weiqifa hello for JNI
10-20 03:06:45.241: V/ActivityThread(12194): activityCreated r=ActivityRecord{41e7a890 token=android.os.BinderProxy@41e7a060 {com.example.helloworld/com.example.helloworld.AlarmTest}}


問題:

剛開始的時候,我在Android.mk里面寫的LOCAL_MODULE生成的模塊名稱是大寫W,helloWorld

LOCAL_MODULE := helloWorld

但是在android源碼里面,我寫的

System.loadLibrary("helloWorld");

里面的helloworld是小寫w,然后運行一直加載不成功,后面有個朋友幫我看了一下,找到原因,粗心大意啊。













總結

以上是生活随笔為你收集整理的jni调试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。