当前位置:   article > 正文

AndroidStudio java项目中添加cpp支持_android studio cpp no file

android studio cpp no file

如果在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/)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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"
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/572758
推荐阅读
相关标签
  

闽ICP备14008679号