編輯:關於Android編程
1. 不能正常加載編譯出的so文件,異常如下
02-18 21:18:50.241 17027-17027/com.android.encrypt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load Encrypt from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.android.encrypt-2.apk,libraryPath=/data/app-lib/com.android.encrypt-2]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.android.encrypt.CommonEncrypt.
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
分析之後,是因為編譯出的so沒有打包到apk中,因此是Android Studio的打包過程可能出現了問題
查閱相關資料之後,發現打包相應的jar或者so,在build.gradle文件中配置,相應參考文檔:http://tools.android.com/tech-docs/new-build-system/user-guide
要把so庫打包到apk中,需要在build.gradle加入下面內容:
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: 'armeabi/*.so') into 'build/lib'
}
tasks.withType(Compile) {
compileTask -> compileTask.dependsOn copyNativeLibs
}
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = [new File(buildDir, 'lib')]
}
2. java層找不到native方法,異常如下:
02-18 21:27:21.802 18785-18785/com.android.encrypt E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Native method not found: com.android.encrypt.CommonEncrypt.encrypt:([B)[B
at com.android.encrypt.CommonEncrypt.encrypt(Native Method)
at com.android.encrypt.CommonEncrypt.Encrypt(CommonEncrypt.java:51)
at com.android.encrypt.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$600(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5071)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
at dalvik.system.NativeStart.main(Native Method)
02-18 21:27:21.862 18785-18785/com.android.encrypt I/Process﹕ Sending signal. PID: 18785 SIG: 9
解決方法:如果是C++文件(.cpp或者.cc),要使用extern "C" { } 把本地方法括進去。
3.待續
前言開發過程中,為了數據交互安全,決定對數據進行des加密,然後進行前後交互;但是,如果密鑰放置在android代碼裡面,就算是混淆,反編譯也很容易讓人拿到密鑰,數據加密
1.創建HelloWorld項目任何編程語言寫出的第一個程序毫無疑問都會是Hello World,這已經是自20世紀70年代一直流傳下來的傳統,在編程界已成為永恆的經典,
https://github.com/daimajia/AndroidViewHover import android.support.v8.renderscrip
上一篇文章Android 中的 Service 全面總結詳解【下】 介紹了Service的一些知識以及本地Service的使用,如果對Service還不太了解的建議先看下