AS项目添加C/C++代码

项目添加 C 和 C++ 代码

创建新的Native工程

勾选支持c++即可,生成的项目结构如下:
AS项目添加C/C++代码

现有工程中添加Native代码

  • 创建cpp目录:在src/main目录下创建cpp目录;
  • 创建cpp文件:New > C/C++ Source File,想要创建头文件需勾选 Create an associated header 复选框;
  • 创建CMakeLists.txt:在module根目录下创建txt文件;
  • 关联gradle:见【使用GUI关联gradle】和【手动关联gradle】两个章节;
  • 应用更改和更新:首次关联CMakeLists.txt或者Android.mk之后,需点击Sync Project 图标应用修改;后面修改相关脚本后应该从菜单栏中依次选择 Build > Refresh Linked C++ Projects将更改进行同步。

使用GUI关联gradle

在module根目录右键,选择 Link C++ Project with Gradle,可以选择CMake 或 ndk-build。如果选择CMake则需指定CMakeLists.txt 脚本文件,如果选择 ndk-build指定Android.mk 脚本文件(如果 Application.mk 文件与您的 Android.mk 文件位于同一目录下,Android Studio 也会包含此文件)

AS项目添加C/C++代码
AS项目添加C/C++代码

手动关联gradle

只需将 externalNativeBuild 块添加到模块级 build.gradle 文件中,并使用 cmakendkBuild 块对其进行配置:

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

  // Encapsulates your external native build configurations.
  externalNativeBuild {

    // Encapsulates your CMake build configurations.
    cmake {

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

还支持利用变体为每个变体指定可选配置:

android {
  ...
  defaultConfig {
    ...
    // This block is different from the one you use to link Gradle
    // to your CMake or ndk-build script.
    externalNativeBuild {

      // For ndk-build, instead use the ndkBuild block.
      cmake {

        // Passes optional arguments to CMake.
        arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"

        // Sets a flag to enable format macro constants for the C compiler.
        cFlags "-D__STDC_FORMAT_MACROS"

        // Sets optional flags for the C++ compiler.
        cppFlags "-fexceptions", "-frtti"
      }
    }
  }

  buildTypes {...}

  productFlavors {
    ...
    demo {
      ...
      externalNativeBuild {
        cmake {
          ...
          // Specifies which native libraries or executables to build and package
          // for this product flavor. The following tells Gradle to build only the
          // "native-lib-demo" and "my-executible-demo" outputs from the linked
          // CMake project. If you don't configure this property, Gradle builds all
          // executables and shared object libraries that you define in your CMake
          // (or ndk-build) project. However, by default, Gradle packages only the
          // shared libraries in your app.
          targets "native-lib-demo",
                  // You need to specify this executable and its sources in your CMakeLists.txt
                  // using the add_executable() command. However, building executables from your
                  // native sources is optional, and building native libraries to package into
                  // your app satisfies most project requirements.
                  "my-executible-demo"
        }
      }
    }

    paid {
      ...
      externalNativeBuild {
        cmake {
          ...
          targets "native-lib-paid",
                  "my-executible-paid"
        }
      }
    }
  }

  // Use this block to link Gradle to your CMake or ndk-build script.
  externalNativeBuild {
    cmake {...}
    // or ndkBuild {...}
  }
}

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/)

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the log library to the target library.
                       ${log-lib} )

调试原生代码

使用 LLDB 来调试代码

参考资料

上一篇:CMake 输出路径的设置


下一篇:CF451C Predict Outcome of the Game 水题