Android CMake

首先了解几个名词

NDK

The Android Native Development Kit

The Android NDK is a toolset that lets you implement parts of your app in native code, using languages such as C and C++. For certain types of apps, this can help you reuse code libraries written in those languages.

CMake

跨平台外部编译工具,跟Gradle一起编译本地库文件(libarary)。如果只是使用ndk-build编译,无需安装。Ndk-build和CMake不可混用

An external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.

在gradle中指定cmake版本

android {
    ...
    externalNativeBuild {
        cmake {
            ...
            version "cmake-version"
        }
    }
}

CMakeLists.txt

A CMake build script 
 

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

通过上述配置,将按规则  liblibrary-name.so  生成  libnative-lib.so

java代码里的引用

static {
    System.loadLibrary("native-lib");
}

LLDB

Android Studio 通过它来调试native code。LLDB跟Android Studio一起安装

The debugger Android Studio uses to debug native code. By default, LLDB will be installed alongside Android Studio.

SDK

AGP  Android Gradle plugin

gradle

        externalNativeBuild {
            cmake {

                // Sets optional flags for the C++ compiler.
                cppFlags "-frtti -fexceptions -std=gnu++0x -DHAVE_PTHREADS -fpermissive -O3 -Wall -Werror -###"
                // Passes optional arguments to CMake.
                arguments "-DPRODUCT_CONFIG=effects"
                arguments "-DANDROID_STL=c++_shared"

                // Sets a flag to enable format macro constants for the C compiler.
                cFlags "-D__STDC_FORMAT_MACROS"
            }
        }
        ndk {
            abiFilters 'arm64-v8a','armeabi-v7a'
        }

https://developer.android.com/studio/projects/gradle-external-native-builds

 

Ninja

JNI

Java Native Interface 

Java或者Kotlin代码通过JNI调研Navtive Library的函数

Your Java or Kotlin code can then call functions in your native library through the Java Native Interface (JNI).

参考

https://developer.android.com/studio/projects/install-ndk#groovy 

Android CMake_仰望XX的博客-优快云博客

Android 开发:CMake 使用_android cmake_zxy_de_android的博客-优快云博客

Android-CMake语法 - 简书

Android NDK开发扫盲及最新CMake的编译使用 - 知乎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值