AndroidStudio項目之CmakeLists解析

前言

我們在使用AndroidStudio 3.0開發(fā)NDK項目的時候CmakeLists.txt將是我們必須要用到的文件,如果你不懂怎么用CmakeLists配置NDK請先看之前的一篇博客:AndroidStudio 3.0 NDK環(huán)境搭建,如果已經(jīng)了解CmakeLists配置NDK項目,ok,那我們接下來步入正題~

CmakeLists源碼

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( # 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).
             src/main/cpp/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

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

源碼很簡單除了注釋的代碼外,核心的代碼也就那么幾句.

  • cmake_minimum_required(VERSION 3.4.1)

Sets the minimum version of CMake required to build the native library.

用來設(shè)置編譯本地native library的時候需要的Cmake最小版本.這個是創(chuàng)建AndroidStudio項目的時候自動生成,不需要太在意.

  • add_library()
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).
             src/main/cpp/native-lib.cpp )

native-lib : 設(shè)置本地lib的name
SHARED : 表示編譯生成的是動態(tài)鏈接庫
src/main/cpp/native-lib.cpp : 表示編譯文件的相對路徑,這里可以是一個文件的路徑也可以是多個文件的路徑

  • find_library()

這個的作用是用來讓我們加一些編譯本地NDK庫的時候所用的到一些依賴庫.
log-lib 是這個庫的別名,方便我們以后引用
log 是我們調(diào)試的時候打印log的一個庫

  • target_link_libraries()
    這個的目的是用來關(guān)聯(lián)我們本地的庫跟第三方的庫.這里就是把native-lib庫和log庫關(guān)聯(lián)起來.

自定義NDK的配置

單個C/C++文件

這個在之前的博客里有提到,可以翻看AndroidStudio 3.0 NDK環(huán)境搭建

多個C/C++文件

我們在實際項目中,C++文件可能不止一個,如果有多個C++文件,我們的CmakeLists應(yīng)該怎么配置呢?其實前面說add_library() 的時候提到了,路徑可以是多個文件的路徑.
所以我們可以這么配置:

add_library( # Sets the name of the library.
               hello

               # Sets the library as a shared library.
               SHARED

               # Provides a relative path to your source file(s).
               src/main/cpp/Hello.cpp
               src/main/cpp/Hi.cpp
               )

只需要在路徑處增加一個路徑,就配置好了.可能只說這個,大家會有點迷茫,可以放在項目中來看下,我們基于上一篇文章的項目,實現(xiàn)這個多個NDK文件的配置過程.
先回憶一下創(chuàng)建NDK項目的步驟:

  1. 創(chuàng)建一個Java文件
  2. 在這個類里面寫一個native方法
  3. 生成頭文件(*.h)
  4. 創(chuàng)建c文件并實現(xiàn)頭文件里面的方法
  5. Java文件里面加入靜態(tài)方法塊
  6. 配置grade
  7. 在Activity里面調(diào)用Jni
  8. 配置CmakeLists.txt文件

我們先創(chuàng)建一個Hi.java文件,并在Hi.java文件中寫一個native方法,如下:


image

生成Hi的頭文件

$ cd app/src/main/java

$ javah -d ../cpp com.vv.ndk.Hi

創(chuàng)建一個Hi.cpp c文件實現(xiàn)com_vv_ndk_Hi.h 頭文件里面未實現(xiàn)的方法

image
#include "com_vv_ndk_Hi.h"
JNIEXPORT jstring JNICALL Java_com_vv_ndk_Hi_sayHi(JNIEnv *env, jclass jclass1){
    return env->NewStringUTF("sat Hi");
}

Hi.java文件中加入靜態(tài)代碼塊


image

注意這個System.loadLibrary 加載的是你本地庫的名字

配置CmakeLists.txt文件


cmake_minimum_required(VERSION 3.4.1)

add_library(
               hello
               SHARED
               src/main/cpp/Hello.cpp
               src/main/cpp/Hi.cpp
               )

find_library(
              log-lib
              log )
target_link_libraries(
                       hello
                       ${log-lib} )

activity里面調(diào)用
xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vv.ndk.MainActivity">

    <TextView
        android:id="@+id/sample_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
         />

</android.support.constraint.ConstraintLayout>

activity文件

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(Hello.sayHello());

        textView=findViewById(R.id.textview);
        textView.setText(Hi.sayHi());
    }
}

運行效果


image

可以看到調(diào)用Hi文件的sayHi() 方法已經(jīng)被調(diào)用了.

編譯多個SO庫

編譯多個so庫的cpp目錄結(jié)構(gòu)


image

one 文件夾內(nèi)的CmakeLists.txt 配置如下:

add_library(one SHARED one.cpp)

target_link_libraries(one ${log-lib} )

two 文件夾內(nèi)CmakeLists.txt 配置如下:

add_library(two SHARED two.cpp)

target_link_libraries(two ${log-lib} )

app 項目的CmakeLists.txt 配置如下:


cmake_minimum_required(VERSION 3.4.1)

add_library(hello
            SHARED
            src/main/cpp/Hello.cpp
            src/main/cpp/Hi.cpp)

find_library(log-lib log )
target_link_libraries(hello ${log-lib} )

ADD_SUBDIRECTORY(src/main/cpp/one)
ADD_SUBDIRECTORY(src/main/cpp/two)

CmakeLists.txt文件支持繼承,所以我們只需要在子配置文件中寫不同的配置項就可以完成相應(yīng)的配置.最后需要在項目的CmakeLists.txt文件中增加子配置文件的路徑.

然后我們用Make構(gòu)建Module app生成字節(jié)碼文件

image

這樣就可以在/app/build/intermediates/cmake/debug/obj/arm64-v8a/ 路徑下看到我們剛剛生成的so文件.
image

需要源碼的同學可以直接從github上下載:NDKLearnDemo

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容