Androidstudio现有文件中添加C、C++文件 (NDK)

发布时间 2023-09-08 13:42:16作者: 第9527号嵌入式菜鸟

创建新的 C/C++ 源代码文件

1. 如果应用的主源代码集内还没有 cpp/ 目录,请按如下所示的方法创建一个:
    1.1 打开 Android Studio 左侧的 Project 窗格,然后从菜单中选择 Project 视图。
    1.2 依次选择 your-module > src。
    1.3 右键点击 main 目录,然后依次选择 New > Directory。
    1.4 输入 cpp 作为目录名称,然后点击 OK。
2. 右键点击 cpp/ 目录,然后依次选择 New > C/C++ Source File。
3. 为您的源代码文件输入一个名称,例如 native-lib。
4. 从 Type 菜单中,选择源代码文件的文件扩展名,例如 .cpp。
    点击 Edit File Types 图标 ,向菜单中添加其他文件类型,例如 .cxx 或 .hxx。在弹出的 New File Extensions 对话框中,从 Source Extension 和 Header Extension 菜单中选择其他文件扩展名,然后点击 OK。
5. 如需创建头文件,请勾选 Create an relevantheader 复选框。
6. 点击 OK。

配置 CMake

创建 CMake 构建脚本

1. 从 IDE 的左侧打开 Project 窗格,然后从下拉菜单中选择 Project 视图。
2. 右键点击 your-module 的根目录,然后依次选择 New > File。
3. 输入“CMakeLists.txt”作为文件名,然后点击 OK。

添加 CMake 命令来配置您的构建脚本


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

cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.

project("usb-serial-for-android_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).
        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})

将 Gradle 关联到您的原生库

使用 Android Studio 界面

您可以使用 Android Studio 界面将 Gradle 关联到外部 CMake 或 ndk-build 项目:

1.  从 IDE 左侧打开 Project 窗格,然后选择 Android 视图。
2. 右键点击您想要关联到原生库的模块(例如 app 模块),然后从菜单中选择 Link C++ Project with Gradle。您会看到一个类似于图 4 所示的对话框。
3. 从下拉菜单中,选择 CMake 或 ndk-build。
    3.1如果您选择 CMake,请使用 Project Path 旁的字段为您的外部 CMake 项目指定 CMakeLists.txt 脚本文件。
    3.2如果您选择 ndk-build,请使用 Project Path 旁的字段为您的外部 ndk-build 项目指定 Android.mk 脚本文件。如果 Application.mk 文件与您的 Android.mk 文件位于同一目录下,Android Studio 也会包含此文件。

    ![img](https://img2023.cnblogs.com/blog/3184764/202308/3184764-20230828174628218-187911932.png)
     图 4. 使用 Android Studio 对话框关联外部 C++ 项目。
4. 点击 OK。

手动配置 Gradle

将 externalNativeBuild 块添加到模块级 build.gradle 文件中

android {
  ...
  defaultConfig {...}
  buildTypes {...}

  // Encapsulates your external native build configurations.

    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
    buildFeatures {
        viewBinding true
    }



}
'''