Android Studio3.5 JNI配置及使用

Android Studio3.5JNI配置及使用

一、下载安装NDK以及CMake

1.1进入SDK Manager

  • 主页右上角

在这里插入图片描述

  • File->Settings->Appearance & Behavior->System Settings->Android SDK

1.2开始下载

  • 选中上方SDK Tools和下方的Show Package Details多选框
  • 选中需要下载的NDK版本以及CMake版本,点击右下角Apply即可

在这里插入图片描述

安装目录会在上方sdk目录中的ndk目录中

二、新建项目

1.1新建Native C++项目

在这里插入图片描述

1.2修改项目信息

在这里插入图片描述

1.3选择C++版本

  • 无特殊要求直接默认即可

在这里插入图片描述

1.4创建完成

  • cpp目录即为c++代码目录

在这里插入图片描述

二、配置CMake

2.1CMakeLists.txt主配置文件

  • 打开app目录下的build.gradle文件
  • path:主配置文件相对路径
  • version:版本号
externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
        version "3.10.2"
    }
}

2.2CMakeLists.txt详解

2.2.1文件解析

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

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

# 创建和命名库,将其设置为静态或共享,并提供到其源代码的相对路径。
# 您可以定义多个库,然后CMake为您构建它们。Gradle自动将共享库打包到你的APK中。

# 添加cpp文件到库中
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.

# 将log库储存为一个名为log-lib的变量
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.

# 在上方定义好的native-lib库中引入log库
target_link_libraries( # Specifies the target library.
        native-lib

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

2.2.2添加多个.cpp文件

add_library(
        native-lib
        SHARED
        native-lib.cpp
        one-lib.cpp
        two-lib.cpp)

2.2.3生成多个.so

  • 在cpp目录下新建文件夹,并在新建的文件夹下新建CMakeLists.txt文件

在这里插入图片描述

  • 新建.cpp文件

在这里插入图片描述

  • one目录下CMakeLists.txt文件内容
add_library(one-lib SHARED one-lib.cpp)
target_link_libraries(one-lib log)
  • two目录下CMakeLists.txt文件内容
add_library(two-lib SHARED two-lib.cpp)
target_link_libraries(two-lib log)
  • 在主配置文件下添加
add_subdirectory(one)
add_subdirectory(two)

参数传入的是指定包含CMakeLists文件的目录的相对路径

  • 点击Build->Make Project
    • 打开app->build->intermediates->cmake->debug->obj中的任意文件夹

在这里插入图片描述

2.3详细配置参阅官方文档

Google Docs

CMake

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章