AndroidStudio2.2.x以上使用cMake編譯調用底層c產生依賴庫,

來源:互聯網
上載者:User

AndroidStudio2.2.x以上使用cMake編譯調用底層c產生依賴庫,

最近使用AndroidStudio的最新ndk編譯方式cMake來編譯底層cpp檔案,由於之前沒有接觸過cMake文法,先附上官方學習文檔地址:https://developer.android.com/ndk/guides/cmake.html,以及友情中文翻譯網址:https://www.zybuluo.com/khan-lau/note/254724

底層c檔案一大堆,如所示

 

問題一:

其中native-lib.cpp是提供對外介面的,所以對其他檔案的依賴都寫在了該檔案中,接下來直接編譯嗎?no,那樣你會得到編譯錯誤,在native-lib.cpp中找不到其他c檔案裡邊的函數。所以是cMake編譯的時候沒有把那些c檔案編譯進去,這樣要去CMakeLists.txt中去添加檔案了。

未修改之前的CMakeLists.txt檔案內容是這樣的:

# 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( # Sets the name of the library.                            native-lib                                           # Sets the library as a shared library.                            SHARED                                              EXCLUDE_FROM_ALL                            # Provides a relative path to your source file(s).                            src/main/cpp/native-lib.cpp                             )

學習CMake的官方文檔可以知道,其中的add_library()正是設定產生包的地方,這裡只有native-lib.cpp一個檔案,理論上應該要包括其他那些的,那麼加上其他那幾個檔案就好了吧?是可以這樣,但是那麼多檔案,未免也太長了吧,當然CMake肯定預料到這種問題了,所以我們可以get一個新技能,使用aux_source_directory(),在源檔案位置添加檔案清單,而不是單個檔案,get修改之後的檔案內容如下:

# 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.               aux_source_directory(src/main/cpp SRC_LIST)               add_library( # Sets the name of the library.                            native-lib                            # Sets the library as a shared library.                            SHARED                               EXCLUDE_FROM_ALL                            # Provides a relative path to your source file(s).                            #src/main/cpp/native-lib.cpp                             ${SRC_LIST}                             )

這樣子改完需要Synchronize同步一下,這樣就可以重新編譯了。

 

問題二:

在編譯通過之後,運行調用底層代碼,結果程式崩潰了,提示java.lang.UnsatisfiedLinkError: Native method not found: ***;好吧,沒有找到這個底層方法?可是明明已經產生了,仔細觀察代碼結構可以發現,由於AndroidStudio自動產生的底層檔案是.cpp的,也就是使用了c++的文法規則,而其他的底層檔案都是.c的使用的c語言規則,他們兩者之間還是有區別的,現在需要在cpp檔案中調用c檔案,那麼要在cpp檔案中做些修改,首先標頭檔包含c的是相同的,不同點是在使用c檔案中的函數上方,加一行 extern "C" 即可。這樣重新編譯運行,一切正常!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.