首页 > 编程语言 >Androidstudio现有文件中添加C、C++文件 (NDK)

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

时间:2023-09-08 13:44:53浏览次数:40  
标签:文件 NDK CMake library libraries C++ Project Android build

创建新的 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](/i/l/?n=23&i=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
    }



}
'''



标签:文件,NDK,CMake,library,libraries,C++,Project,Android,build
From: https://www.cnblogs.com/zhouxingxing7920/p/17663036.html

相关文章

  • c#开发读取elf文件
    c#开发读取elf文件使用LibObjectFile库文件源码1.创建一个C#项目文件2.下载LibObjectFile库文件源码https://github.com/xoofx/LibObjectFile3.将库文件中的src文件中的LibObjectFile拷贝到项目文件中4.在vs中使用NuGET添加NUnit包5.可以参照src/LibObje......
  • linux 中awk命令同时多多个文件指定分隔符
     001、正确做法[root@pc1test01]#lsa.txtb.txt[root@pc1test01]#cata.txtA:10B:5C:12[root@pc1test01]#catb.txt100A50B42C[root@pc1test01]#awk'{if(NR==FNR){print$2}else{print$1}}'FS=":"a.txtFS=""b......
  • linux 中 awk命令实现文件按列匹配
     001、方法1[root@pc1test01]#lsa.txtb.txt[root@pc1test01]#cata.txtA:10B:5C:12[root@pc1test01]#catb.txt100A50B42C[root@pc1test01]#awk-F"[:]"'{if(NR==FNR){ay[$1]=$2}else{print$2,$1,ay[$2]}}'a.txtb......
  • 使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地
    【问题描述】使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地 【原因分析】实际上文件的流后端已经返回了,只是缺少了Content-Disposition属性返回,前端无法获取到文件名;privatestaticOutputStreamgetOutputStream(StringfileName,HttpServletRespon......
  • 自定义配置文件参数在application可以直接识别Not registered via @EnableConfigurati
    自定义配置文件参数在application可以直接识别Notregisteredvia@EnableConfigurationPropertiesormarkedasSpringcomponent看见很多开源项目的配置文件可以直接配置在application.yaml中,自己也想弄一个,怎么弄呢?这是我的demo,你正常ConfigurationProperties会报错Notregi......
  • 线程安全的队列:使用Monitor模式和C++11多线程库
    线程安全的队列:使用Monitor模式和C++11多线程库引言在多线程编程中,数据共享是一个关键的问题。如果多个线程需要访问同一个数据结构,不正确的管理会导致数据不一致甚至程序崩溃。本文将介绍如何使用C++11的多线程库和Monitor模式来实现一个线程安全的队列。Monitor模式Monitor模式......
  • 样本分析 99eddc2794077f97a5cfe3098f431c4cfc4fd6353957ee715b2eccbff066ce1d 由于.
     https://s.threatbook.com/report/file/99eddc2794077f97a5cfe3098f431c4cfc4fd6353957ee715b2eccbff066ce1d09:30:16:088, 99eddc2794077f97a5cfe3098f431c4cfc4fd6353957ee715b2eccbff066ce1d.exe, 1908:0, 1908, EXEC_create, C:\Users\bonelee\Desktop\99eddc2794077......
  • C#访问本地网络目录及文件
    来自你的消息:怎么通过C#访问局域网目录?用户名和密码怎么设置?来自ChatAI的消息:要使用C#访问局域网目录,可以使用.NETFramework中的System.Net命名空间提供的类和方法来实现。你可以使用NetworkCredential类设置用户名和密码进行身份验证。下面是一个简单的示例代码:usingSyst......
  • effective c++笔记
    一.截图1. public继承,No32,Pg155 2.不重定义继承而来的缺省参数值,No37,Pg183 ......
  • NFS:使⽤ NFS 为远程客户端提供共享文件系统
    写在前面分享一些nfs搭建的笔记考试顺便整理内容涉及nfs服务端客户端的搭建配置理解不足小伙伴帮忙指正对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大众理想的懦弱回归,是随波逐流,是对内心的......