android module 引用libs里面的so文件_Android中的JNI开发,你了解多少?
任務是用戶在執(zhí)行某項工作時與之互動的一系列 Activity 的集合。
一、步驟,修改build.gradle,添加cmakelists,寫JNI接口,寫c++,這個是不是流水線的方式集成,不了解每一步是做什么的;
.so文件
so是shared object的縮寫,見名思義就是共享的對象,機器可以直接運行的二進制代碼。
我們通過C/C++開發(fā)的軟件,如果以動態(tài)鏈接庫的形式輸出,那么在Android中它的輸出就是一個.so文件。
相比于.a,.so文件是在運行時,才會加載的。所以,當我們將.so文件放入工程時,一定有一段Java代碼在運行時,load了這個native庫,并通過JNI調用了它的方法。
所以,當我們使用JNI開發(fā)時,我們就是在開發(fā)一個.so文件。不論我們是開發(fā)一個工程,還是開發(fā)一個庫,只要當我們使用C++開發(fā),都會生成對應的.so文件。
所以JNI開發(fā)的核心是,我們生成so的過程,和使用so的過程。
生成.so文件
當我們在新建工程過程中,選中support c++時,系統(tǒng)會為我們寫好一些配置
build.gradle
android { ? ?compileSdkVersion 29 ? ?defaultConfig { ? ? ? ?minSdkVersion 22 ? ? ? ?targetSdkVersion 29 ? ? ? ?versionCode 1 ? ? ? ?versionName "1.0" ? ? ? ?testInstrumentationRunner "android.test.runner.AndroidJUnitRunner" ? ? ? ?consumerProguardFiles 'consumer-rules.pro' ? ? ? ?externalNativeBuild { ? ? ? ? ? ?cmake { ? ? ? ? ? ? ? ?cppFlags "-fexceptions" ? ? ? ? ? ? ? ?abiFilters "armeabi-v7a" ? ? ? ? ? ?} ? ? ? ?} ? ? ? ?ndk{ ? ? ? ? ? ?abiFilters"armeabi-v7a" ? ? ? ?} ? ?} ? ?externalNativeBuild { ? ? ? ?cmake { ? ? ? ? ? ?path "src/main/cpp/CMakeLists.txt" ? ? ? ? ? ?version "3.10.2" ? ? ? ?} ? ?} ? ?compileOptions { ? ? ? ?sourceCompatibility = 1.8 ? ? ? ?targetCompatibility = 1.8 ? ?} ? ?sourceSets{ ? ? ? ?main{ ? ? ? ? ? ?jniLibs.srcDirs'libs' ? ? ? ?} ? ?} ? ?repositories { ? ? ? ?flatDir { ? ? ? ? ? ?dirs 'libs' ? ? ? ?} ? ?}}這是module的build.gradle中的一段。其中,兩個externalNativeBuild與ndk是與JNI相關的配置。
我們寫好的.cpp/.h文件需要經(jīng)過編譯才能生成.so,讓apk得以調用。在編譯生成.so文件的過程中,我們可以添加如下一些配置。
cppFlags
cmake時的額外參數(shù),
cppFlags "-fexceptions"來支持C++異常。
abiFilters
設置 執(zhí)行 gradle assembleRelease 時,支持的 SO 庫構架。如果像上面的代碼這樣設置,我們打出來的包,就會包含三種架構的.so包。這必然會使APK包體積變大。可以考慮使用productFlavors進行優(yōu)化。
cmake.path
指定cmakelists文件的路徑。
除了這些必須的標簽外,externalNativeBuild中還有很多可以配置的東西,因為不是必需,不在此贅述。如ndkBuild中可以設置c++的版本等配置。
CMakeLists.txt
# For more information about using CMake with Android Studio, read the# documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.4.1)# Creates and names a library, sets it as either STATIC# or SHARED, and provides the relative paths to its source code.# You can define multiple libraries, and CMake builds them for you.# Gradle automatically packages shared libraries with your APK.add_library(comm_lib SHARED IMPORTED)set_target_properties(comm_lib ? ? ? ?PROPERTIES ? ? ? ?IMPORTED_LOCATION ? ? ? ?../../../../libs/${ANDROID_ABI}/libcomm_lib.so)
add_library( # Sets the name of the library. ? ? ? ? ? ? native-lib ? ? ? ? ? ? # Sets the library as a shared library. ? ? ? ? ? ? SHARED ? ? ? ? ? ? # Provides a relative path to your source file(s). ? ? ? ? ? ? native-lib.cpp )# Searches for a specified prebuilt library and stores the path as a# variable. Because CMake includes system libraries in the search path by# default, you only need to specify the name of the public NDK library# you want to add. CMake verifies that the library exists before# completing its build.find_library( # Sets the name of the path variable. ? ? ? ? ? ? ?log-lib ? ? ? ? ? ? ?# Specifies the name of the NDK library that ? ? ? ? ? ? ?# you want CMake to locate. ? ? ? ? ? ? ?log )# Specifies libraries CMake should link to your target library. You# can link multiple libraries, such as libraries you define in this# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library. ? ? ? native-lib ? ? ? ?comm_lib ? ? ? # Links the target library to the log library ? ? ? # included in the NDK. ? ? ? ${log-lib} )
在cpp目下編寫c++的代碼
在Java中聲明native方法,c++自動生成方法(Android studio3.2測試)
Java native聲明的方法及類型在C++中的方法轉換如下:
可直接調用在Java層調用方法執(zhí)行C++代碼;
C/C++調用Java代碼,比如說Java需要知道C++執(zhí)行的回調結果,異步處理回調數(shù)據(jù)是和我們在Java中寫callback是一樣,數(shù)據(jù)回調之后set值通知實現(xiàn)類的事件; 這個在C中如果被Native調用的Java類是靜態(tài)類:FindClass通過傳java中完整的類名來查找java的class,如果是非靜態(tài)類:GetObjectClass通過傳入jni中的一個java的引用來獲取該引用的類型,他們之間的區(qū)別是,前者要求你必須知道完整的類名,后者要求在Jni有一個類的引用;
Java代碼
在C中定義的jobect和jmethodid及賦值:
調用Java方法
總結
以上是生活随笔為你收集整理的android module 引用libs里面的so文件_Android中的JNI开发,你了解多少?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android插件丢失怎么办,Andro
- 下一篇: 详解TCP协议三次握手四次挥手