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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android module 引用libs里面的so文件_Android中的JNI开发,你了解多少?

發布時間:2025/3/15 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android module 引用libs里面的so文件_Android中的JNI开发,你了解多少? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一,什么是任務及管理

任務是用戶在執行某項工作時與之互動的一系列 Activity 的集合。

一、步驟,修改build.gradle,添加cmakelists,寫JNI接口,寫c++,這個是不是流水線的方式集成,不了解每一步是做什么的;

.so文件

so是shared object的縮寫,見名思義就是共享的對象,機器可以直接運行的二進制代碼。

我們通過C/C++開發的軟件,如果以動態鏈接庫的形式輸出,那么在Android中它的輸出就是一個.so文件。

相比于.a,.so文件是在運行時,才會加載的。所以,當我們將.so文件放入工程時,一定有一段Java代碼在運行時,load了這個native庫,并通過JNI調用了它的方法。

所以,當我們使用JNI開發時,我們就是在開發一個.so文件。不論我們是開發一個工程,還是開發一個庫,只要當我們使用C++開發,都會生成對應的.so文件。

所以JNI開發的核心是,我們生成so的過程,和使用so的過程。

生成.so文件

當我們在新建工程過程中,選中support c++時,系統會為我們寫好一些配置

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文件需要經過編譯才能生成.so,讓apk得以調用。在編譯生成.so文件的過程中,我們可以添加如下一些配置。

cppFlags

cmake時的額外參數,

cppFlags "-fexceptions"

來支持C++異常。

abiFilters

設置 執行 gradle assembleRelease 時,支持的 SO 庫構架。如果像上面的代碼這樣設置,我們打出來的包,就會包含三種架構的.so包。這必然會使APK包體積變大??梢钥紤]使用productFlavors進行優化。

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層調用方法執行C++代碼;

C/C++調用Java代碼,比如說Java需要知道C++執行的回調結果,異步處理回調數據是和我們在Java中寫callback是一樣,數據回調之后set值通知實現類的事件; 這個在C中如果被Native調用的Java類是靜態類:FindClass通過傳java中完整的類名來查找java的class,如果是非靜態類:GetObjectClass通過傳入jni中的一個java的引用來獲取該引用的類型,他們之間的區別是,前者要求你必須知道完整的類名,后者要求在Jni有一個類的引用;

Java代碼

在C中定義的jobect和jmethodid及賦值:

調用Java方法

總結

以上是生活随笔為你收集整理的android module 引用libs里面的so文件_Android中的JNI开发,你了解多少?的全部內容,希望文章能夠幫你解決所遇到的問題。

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