赞
踩
如果在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开发的支持。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。