AndroidStudio uses Cmake to compile the so library of armeabi-v7a and arm64-v8a

Use AndroidStudio to compile armeabi-v7a, arm64-v8a library file steps:

1. Create a new project

2. Modify the CMakeLists.txt file

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

# 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的最小版本号

cmake_minimum_required(VERSION 3.4.1)

#定义项目名称,可以不定义

#project(demo)

# 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.

#设置so库的输出路径

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})

#设置编译类型

#add_executable(demo demo.cpp) # 生成可执行文件

#生成动态共享库

add_library( # 设置编译成so库的名称

             native-lib

             # 生成动态库或共享库,此处如果SHARED改为STATIC,其含义是生成静态库

             SHARED

             # 提供一个需要编译的源文件的相对路径,native-lib.cpp就是需要编译的源文件

             native-lib.cpp )

#明确指定编译时需要编译哪些源文件

#add_library(demo demo.cpp test.cpp util.cpp)

#aux_source_directory(dir VAR) 发现一个目录下所有的源代码文件并将列表存储在一个变量中。

#例如:aux_source_directory(. SRC_LIST) # 搜索当前目录下的所有.cpp文件

#add_library(demo ${SRC_LIST})

# 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需要链接的库

target_link_libraries( # Specifies the target library.目标库

                       native-lib

                       # Links the target library to the log library 目标库需要链接的库,log-lib是上面find_library指定的变量名

                       # included in the NDK.

                       ${log-lib} )

  

3. Modify the build.gradle file of the app

1

2

3

4

//ndk必须定义在defaultConfig目录下,设置需要生成的cpu平台,以下这两个就能够兼容绝大多数的Android平台

       ndk{

           abiFilters 'armeabi-v7a','arm64-v8a'

       }

  

4. Execute compilation

 After compiling, a libs file will be automatically created under the cpp folder, which will store the dynamic shared so library corresponding to each platform

Guess you like

Origin blog.csdn.net/Vincent20111024/article/details/129327310