知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
深入理解Android(1)——理解Android中的JNI(上)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):43
我參加了CSDN博客之星評(píng)選,如不雅在以前的一段時(shí)光里陽(yáng)光小強(qiáng)的博客對(duì)你有所贊助,在這里欲望能投上您寶貴的一票,天天都可以投一次:http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content
一、什么是JNI
JNI是Java Native Interface的縮寫(Java本地調(diào)用),Java法度榜樣中的函數(shù)可聲調(diào)用Native說話寫的函數(shù)(一般指的是C/C++編寫的函數(shù)),Native說話寫的函數(shù)可聲調(diào)用Java層的函數(shù)。
二、為什么要有JNI
Java說話的跨平臺(tái)是因?yàn)樵诓缓掀脚_(tái)上可以運(yùn)行Java虛擬機(jī),而虛擬機(jī)是跑在具體平滔喔贍,而本質(zhì)上Java是經(jīng)由過程JNI技巧實(shí)現(xiàn)的跨平臺(tái),很多基層的模塊在Java說話出生之前已經(jīng)有了比較優(yōu)良的實(shí)現(xiàn),為了避免反復(fù)造輪子所以我們要應(yīng)用JNI技巧來應(yīng)用已有的模塊。
[img]http://img.blog.csdn.net/20150104210435843?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
三、Mac OS上的情況搭建
在這里解釋一下Max OS上的所需情況搭建,Windows和Linux的請(qǐng)搜刮相干材料。
1、安裝JDK(此處省略)。
2、安裝ADT(Android Develop Tools),包含SDK和ADT插件,下載地址:http://pan.baidu.com/s/1o6OBIHG
3、安裝Xcode可以去蘋不雅市廛下載安裝(免費(fèi))。
4、安裝Apache ANT(下載地址:http://ant.apache.org/bindownload.cgi)具體安裝過程可以參考:http://blog.csdn.net/song_hui_xiang/article/details/14315529
5、安裝GNU Make(默認(rèn)已經(jīng)安裝,所以不消安裝)可以應(yīng)用 make -version敕令驗(yàn)證是否安裝。
6、安裝NDK(下載地址:http://pan.baidu.com/s/1i3l5L8T),解壓后在用戶根目次下新建文件.bash_profile然后添加如下兩行(設(shè)備情況變量,可以臨時(shí)不設(shè)備)。
export ANDROID_NDK_HOME=/Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10c export PATH=${PATH}:${ANDROID_NDK_HOME}留意:后面的地址是你解壓后的目次
關(guān)于膳綾擎部分開辟對(duì)象扼要介紹:
1、Apache Ant,是一個(gè)將軟件編譯、測(cè)試、安排等步調(diào)聯(lián)系袈溱一路加以主動(dòng)化的一個(gè)對(duì)象,大年夜多用于Java情況中的軟件開辟。
2、NDK是Android原生開辟對(duì)象包,可以支撐C/C++等原生編程說話開辟Android應(yīng)用,它供給頭文件、庫(kù)和交叉編譯對(duì)象鏈。
四、第一個(gè)示例法度榜樣
轉(zhuǎn)載請(qǐng)解釋出處:http://blog.csdn.net/dawanganban
1、為eclipse指定NDK路徑
[img]http://img.blog.csdn.net/20150104212549675?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
2、導(dǎo)入Android NDK中的示例代碼(導(dǎo)入hello-jni工程),做過Android開辟的同伙應(yīng)當(dāng)很熟悉,這里就不煩瑣了。
[img]http://img.blog.csdn.net/20150104212738236?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
3、向項(xiàng)目中添加原生支撐
項(xiàng)目——>右擊——>Android Tools——>Add Native Support
該項(xiàng)目其實(shí)已經(jīng)包含了一個(gè)原生項(xiàng)目,所以這一步可以跳過,我們直接Finish持續(xù)。
4、插上手機(jī)(模仿器太慢了,建議應(yīng)用真機(jī))運(yùn)行項(xiàng)目。在C/C++界面視圖我們可以看到如下信息
**** Build of configuration Default for project HelloJni **** /Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10c/ndk-build all Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 3 in ./AndroidManifest.xml [arm64-v8a] Gdbserver : [aarch64-linux-android-4.9] libs/arm64-v8a/gdbserver [arm64-v8a] Gdbsetup : libs/arm64-v8a/gdb.setup [x86_64] Gdbserver : [x86_64-4.9] libs/x86_64/gdbserver [x86_64] Gdbsetup : libs/x86_64/gdb.setup [mips64] Gdbserver : [mips64el-linux-android-4.9] libs/mips64/gdbserver [mips64] Gdbsetup : libs/mips64/gdb.setup [armeabi-v7a] Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver [armeabi-v7a] Gdbsetup : libs/armeabi-v7a/gdb.setup [armeabi] Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver [armeabi] Gdbsetup : libs/armeabi/gdb.setup [x86] Gdbserver : [x86-4.6] libs/x86/gdbserver [x86] Gdbsetup : libs/x86/gdb.setup [mips] Gdbserver : [mipsel-linux-android-4.6] libs/mips/gdbserver [mips] Gdbsetup : libs/mips/gdb.setup [arm64-v8a] Compile : hello-jni <= hello-jni.c [arm64-v8a] SharedLibrary : libhello-jni.so [arm64-v8a] Install : libhello-jni.so => libs/arm64-v8a/libhello-jni.so [x86_64] Compile : hello-jni <= hello-jni.c [x86_64] SharedLibrary : libhello-jni.so [x86_64] Install : libhello-jni.so => libs/x86_64/libhello-jni.so [mips64] Compile : hello-jni <= hello-jni.c [mips64] SharedLibrary : libhello-jni.so [mips64] Install : libhello-jni.so => libs/mips64/libhello-jni.so [armeabi-v7a] Compile thumb : hello-jni <= hello-jni.c [armeabi-v7a] SharedLibrary : libhello-jni.so [armeabi-v7a] Install : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so [armeabi] Compile thumb : hello-jni <= hello-jni.c [armeabi] SharedLibrary : libhello-jni.so [armeabi] Install : libhello-jni.so => libs/armeabi/libhello-jni.so [x86] Compile : hello-jni <= hello-jni.c [x86] SharedLibrary : libhello-jni.so [x86] Install : libhello-jni.so => libs/x86/libhello-jni.so [mips] Compile : hello-jni <= hello-jni.c [mips] SharedLibrary : libhello-jni.so [mips] Install : libhello-jni.so => libs/mips/libhello-jni.so **** Build Finished ****這個(gè)過程其實(shí)就是在構(gòu)建原生組件并和Java應(yīng)用法度榜樣打包的過程。此時(shí)在我們手機(jī)上就可以看到一行文字
[img]http://img.blog.csdn.net/20150104220026008?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
五、項(xiàng)目構(gòu)造及重要目次介紹
[img]http://img.blog.csdn.net/20150104220319324?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
1、jni目次:包含原生組件的源代碼及描述原生組件構(gòu)建辦法的Make文件(Android.mk),該目次作為NDK構(gòu)建項(xiàng)目時(shí)的構(gòu)建目次。
2、libs目次:包含指定目標(biāo)平臺(tái)的自力子目次,在打包時(shí)該目次被包含在apk文件中。
3、obj目次:這是一個(gè)中心目次,編譯源碼后產(chǎn)生的目標(biāo)文件都保存在該目次下,我們最好不消拜訪該目次。
六、 實(shí)例工程解析
Android.mk的內(nèi)容如下
# Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY)有關(guān)Makefile的常識(shí)請(qǐng)參考我的另一篇博文:http://blog.csdn.net/dawanganban/article/details/38750151
第一行:Android.mk文檔必須以LOCAL_PATH變量的定義開首,my-dir是一個(gè)體系的宏定義,來定義源文件的目次地位。
第二行:Android構(gòu)建體系將CLEAR_VARS變量設(shè)置為clear-vars.mk片段的地位,更多片段的makefile文件請(qǐng)看ndk\build\core目次,如下:
[img]http://img.blog.csdn.net/20150104222306062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
感化是清除除了LOCAL_PATH以外的LOCAL_<name>變量,如許可以避免沖突。
第三行:每一個(gè)原生組件被稱為一個(gè)模塊,LOCAL_MODULE變量用來給這些模塊設(shè)定一個(gè)獨(dú)一的名稱。
第四行:LOCAL_SRC_FILES變量定義用來建立和組裝這個(gè)模塊的源文件列表,用空格隔開。
第五行:指清楚明了build-shared-library.mk文件的保存地位,該片段包含了將源文件構(gòu)建成共享庫(kù)的須要過程。
下面我們來看看HelloJni.java
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.hellojni; import android.app.Activity; import android.widget.TextView; import android.os.Bundle; public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); } /* A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. */ public native String stringFromJNI(); /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.hellojni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni"); } }大年夜膳綾擎可以看到調(diào)用了原生的stringFromJNI()辦法,應(yīng)用關(guān)鍵字native來通知Java編譯器,這個(gè)是用另一種說話實(shí)現(xiàn)的,再經(jīng)由過程加裝共享庫(kù)(static語(yǔ)句塊)hello-jni來告訴虛擬機(jī)原生辦法的具體實(shí)現(xiàn)。java.lang.System類供給了兩個(gè)靜態(tài)辦法,load和loadLibrary用來運(yùn)行時(shí)加載共享庫(kù)。下面我們來看看具體的實(shí)現(xiàn)。
#include <string.h> #include <jni.h> /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */ jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { #if defined(__arm__) #if defined(__ARM_ARCH_7A__) #if defined(__ARM_NEON__) #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a/NEON (hard-float)" #else #define ABI "armeabi-v7a/NEON" #endif #else #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a (hard-float)" #else #define ABI "armeabi-v7a" #endif #endif #else #define ABI "armeabi" #endif #elif defined(__i386__) #define ABI "x86" #elif defined(__x86_64__) #define ABI "x86_64" #elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */ #define ABI "mips64" #elif defined(__mips__) #define ABI "mips" #elif defined(__aarch64__) #define ABI "arm64-v8a" #else #define ABI "unknown" #endif return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI "."); }第一個(gè)參數(shù)JNIEnv是指向可用JNI函數(shù)表的接口指針,第二個(gè)參數(shù)jobject是HelloJni類實(shí)例的java對(duì)象。最后一句代碼是用c字符串創(chuàng)建UTF-8的Java字符串。
相關(guān)案例查看更多
相關(guān)閱讀
- 定制小程序開發(fā)
- 軟件定制
- 小程序開發(fā)公司
- web服務(wù)
- 手機(jī)網(wǎng)站建設(shè)
- 報(bào)廢車管理系統(tǒng)
- 江蘇小程序開發(fā)
- 云南網(wǎng)頁(yè)制作
- 云南建站公司
- SEO
- 云南網(wǎng)站建設(shè)百度官方
- 云南網(wǎng)站開發(fā)
- 前端
- web
- 開發(fā)微信小程序
- 云南網(wǎng)站設(shè)計(jì)
- 日歷組件
- 汽車報(bào)廢回收
- 網(wǎng)站建設(shè)首選
- 網(wǎng)絡(luò)公司
- 百度小程序開發(fā)公司
- 搜索引擎優(yōu)化
- 海報(bào)插件
- 小程序開發(fā)
- 網(wǎng)站建設(shè)報(bào)價(jià)
- 報(bào)廢車管理
- 支付寶小程序被騙
- 云南網(wǎng)站建設(shè)優(yōu)化
- 云南小程序被騙
- 花農(nóng)小程序