ndk-build

一、运行ndk

  • NDK_APPLICATION_MK:指定Application.mk文件所在位置,每个ndk工程都需要有一个Application.mk文件来配置全局的编译信息。
  • -C:编译开始的目录。
ndk-build NDK_DEBUG=1 NDK_PROJECT_PATH=null NDK_APPLICATION_MK=src/main/jni/Application.mk NDK_OUT=build/out NDK_LIBS_OUT=build/react-ndk/all THIRD_PARTY_NDK_DIR=build/third-party-ndk REACT_COMMON_DIR=../ReactCommon REACT_SRC_DIR=$projectDir/src/main/java/com/facebook/react -C src/main/jni/react/jni

二、Application.mk

  • APP_BUILD_SCRIPT:指定编译脚本
  • APP_ABI:指定编译的CPU架构
  • APP_PLATFORM:指定编译目标Android版本号
  • NDK_MODULE_PATH:ndk编译时搜索模块时要查找的目录
  • APP_STL:编译使用的C++标准库
  • APP_CFLAGS:全局的C编译flags
  • APP_CPPFLAGS:全局的C++编译flags
  • APP_LDFLAGS:全局的链接flags
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

APP_BUILD_SCRIPT := Android.mk

APP_ABI := armeabi-v7a x86 arm64-v8a x86_64
APP_PLATFORM := android-16

APP_MK_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

# What is NDK_MODULE_PATH?
#   This is comparable to the PATH environment variable in Linux. The purpose
#   of NDK_MODULE_PATH is to provide a list of directories that contain modules
#   we want ndk-build to compile.
#
# What is HOST_DIRSEP?
#   In PATH, the directories are separated by a ':'.
#   In NDK_MODULE_PATH, the directories are separated by $(HOST_DIRSEP).
#
# Where are APP_MK_DIR, THIRD_PARTY_NDK_DIR, etc. defined?
#   The directories inside NDK_MODULE_PATH (ex: APP_MK_DIR, THIRD_PARTY_NDK_DIR,
#   etc.) are defined inside build.gradle.
NDK_MODULE_PATH := $(APP_MK_DIR)$(HOST_DIRSEP)$(THIRD_PARTY_NDK_DIR)$(HOST_DIRSEP)$(REACT_COMMON_DIR)$(HOST_DIRSEP)$(APP_MK_DIR)first-party$(HOST_DIRSEP)$(REACT_SRC_DIR)

APP_STL := c++_shared

APP_CFLAGS := -Wall -Werror -fexceptions -frtti -DWITH_INSPECTOR=1
APP_CPPFLAGS := -std=c++1y
# Make sure every shared lib includes a .note.gnu.build-id header
APP_LDFLAGS := -Wl,--build-id

NDK_TOOLCHAIN_VERSION := clang

三、Android.mk

3.0、模块名定义

LOCAL_MODULE

3.1、源码

要参与编译的c、cpp文件,以及so、.a文件都可以做为src文件。
LOCAL_SRC_FILES

3.2、头文件搜索

编译源码时什么哪里搜索文件
LOCAL_C_INCLUDES

3.3、头文件导出

导出头文件给其他模块使用
LOCAL_EXPORT_C_INCLUDES

3.4、编译、链接flags配置

LOCAL_CFLAGSLOCAL_CPPFLAGSLOCL_LDFLAGS

3.5、产物类型

  • 可执行文件
    include $(BUILD_EXECUTABLE)
  • 动态库
    include $(BUILD_SHARED_LIBRARY)
  • 静态库
    include $(BUILD_STATIC_LIBRARY)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

# Include . in the header search path for all source files in this module.
LOCAL_C_INCLUDES := $(LOCAL_PATH)

# Include ./../../ in the header search path for modules that depend on
# reactnativejni. This will allow external modules to require this module's
# headers using #include <react/jni/<header>.h>, assuming:
#   .     == jni
#   ./../ == react
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../..

LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture

LOCAL_LDLIBS += -landroid

# The dynamic libraries (.so files) that this module depends on.
LOCAL_SHARED_LIBRARIES := libfolly_json libfb libfbjni libglog_init libyoga

# The static libraries (.a files) that this module depends on.
LOCAL_STATIC_LIBRARIES := libreactnative libcallinvokerholder

# Name of this module.
#
# Other modules can depend on this one by adding libreactnativejni to their
# LOCAL_SHARED_LIBRARIES variable.
LOCAL_MODULE := reactnativejni

# Compile all local c++ files.
LOCAL_SRC_FILES := $(wildcard *.cpp)

ifeq ($(APP_OPTIM),debug)
  # Keep symbols by overriding the strip command invoked by ndk-build.
  # Note that this will apply to all shared libraries,
  # i.e. shared libraries will NOT be stripped
  # even though we override it in this Android.mk
  cmd-strip :=
endif

# Build the files in this directory as a shared library
include $(BUILD_SHARED_LIBRARY)

# Compile the c++ dependencies required for ReactAndroid
#
# How does the import-module function work?
#   For each $(call import-module,<module-dir>), you search the directories in
#   NDK_MODULE_PATH. (This variable is defined in Application.mk). If you find a
#   <module-dir>/Android.mk you in a directory <dir>, you run:
#   include <dir>/<module-dir>/Android.mk
#
# What does it mean to include an Android.mk file?
#   Whenever you encounter an include <dir>/<module-dir>/Android.mk, you
#   tell andorid-ndk to compile the module in <dir>/<module-dir> according
#   to the specification inside <dir>/<module-dir>/Android.mk.
$(call import-module,folly)
$(call import-module,fb)
$(call import-module,fbjni)
$(call import-module,jsc)
$(call import-module,fbgloginit)
$(call import-module,yogajni)
$(call import-module,cxxreact)
$(call import-module,jsi)
$(call import-module,jsiexecutor)
$(call import-module,callinvoker)
$(call import-module,hermes)

include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk

# TODO(ramanpreet):
#   Why doesn't this import-module call generate a jscexecutor.so file?
# $(call import-module,jscexecutor)

include $(REACT_SRC_DIR)/jscexecutor/Android.mk
include $(REACT_SRC_DIR)/../hermes/reactexecutor/Android.mk
include $(REACT_SRC_DIR)/../hermes/instrumentation/Android.mk
include $(REACT_SRC_DIR)/modules/blob/jni/Android.mk

四、模块依赖处理

1、源码模块依赖

使用LOCAL_SHARED_LIBRARIESLOCAL_STATIC_LIBRARIES进行依赖,但需要使用以下同种方式中的一种,将依赖的模块添加到当前模块。

  • 使用include xxx/Android.mk方式,直接依赖具体的某个mk文件,如include $(REACT_SRC_DIR)/turbomodule/core/jni/Android.mk
  • 使用import-module方式,通过Application.mk文件中,NDK_MODULE_PATH变量配置的路径进行搜索。如$(call import-module, yoga)

2、预编译库依赖

如果预编译库是系统库,可以直接使用LOCAL_LDLIBS引入。对于非系统库,需要做特殊处理:在ndk编译系统里,非系统预编译库需要使用模块(module)方式封闭起来,再按源码模块依赖方式添加到目标模块为其使用。这样做可以将预编译库,及其相关头文件形成一个整体,简化其他模块的引用。预编译库没有源码,只有编译好的.so或.a文件,封装模块时的LOCL_SRC_FILES直接使用编译好的库文件即可。

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= jsc
LOCAL_SRC_FILES := jni/$(TARGET_ARCH_ABI)/libjsc.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_SHARED_LIBRARY)

相关推荐

  1. ndk-build

    2024-06-16 13:46:04       6 阅读
  2. android使用ndk开发

    2024-06-16 13:46:04       37 阅读
  3. Android_NDK调试

    2024-06-16 13:46:04       14 阅读
  4. go build

    2024-06-16 13:46:04       30 阅读
  5. Ndk编译hevc静态库

    2024-06-16 13:46:04       43 阅读
  6. 安卓交叉编译——ndk

    2024-06-16 13:46:04       7 阅读
  7. Arkts build函数

    2024-06-16 13:46:04       34 阅读
  8. Docker build 命令

    2024-06-16 13:46:04       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 13:46:04       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 13:46:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 13:46:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 13:46:04       18 阅读

热门阅读

  1. AI学习指南机器学习篇-KNN基本原理

    2024-06-16 13:46:04       7 阅读
  2. XML XSLT:技术与应用解析

    2024-06-16 13:46:04       5 阅读
  3. 【C++】priority_queue的用法(模板参数的实例)

    2024-06-16 13:46:04       6 阅读
  4. 决策树算法介绍 - 原理与案例实现

    2024-06-16 13:46:04       8 阅读
  5. Web前端设计培训机构:深度解析与实战指南

    2024-06-16 13:46:04       8 阅读
  6. Mysql-题目02

    2024-06-16 13:46:04       6 阅读
  7. Web前端实战教学:深度剖析与技能进阶

    2024-06-16 13:46:04       8 阅读