AndroidStudio java项目中添加cpp支持

如果在AndroidStudio中创建android项目时未设置c/cpp属性,则需要如下步骤来添加native cpp的支持:

1. 添加cpp目录。添加文件一定要小心,目录不要弄错。注意此目录要放在app/src/main目录下,同时完成添加cpp文件和代码编写等。其余的,比如java代码中调用native函数的语法、cpp中java调用函数的命名规则(注意这些函数不用导出)等编程细节不再赘述。

2. 添加CMakeLists.txt。注意CMakeLists.txt文件要放在app目录下。根据cpp文件的名称,修改CMakeLists.txt中的原文件全路径名称和so模块名称。该文件模板如下:

#CMakeLists.txt 内容如下:
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies 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 )
			 
# Specifies a path to native header files.
# include_directories(src/main/cpp/include/)

3. 修改build.gradle 文件。注意android中有两个gradle文件,要修改的文件在app目录下面的,而不是根目录下的gradle文件。(疑问:为何一个工程需要两个同名文件,此两个同名文件有何区别?)
若工程未添加过cpp文件,则gradle中android字段只需要添加如下字段:

    externalNativeBuild {

        // Encapsulates your CMake build configurations.
        cmake {

            // Provides a relative path to your CMake build script.
            path "CMakeLists.txt"
        }
    }

如此操作后,将实现android stuidio对native开发的支持。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值