• 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 .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,), you search the directories in
    #   NDK_MODULE_PATH. (This variable is defined in Application.mk). If you find a
    #   /Android.mk you in a directory , you run:
    #   include //Android.mk
    #
    # What does it mean to include an Android.mk file?
    #   Whenever you encounter an include //Android.mk, you
    #   tell andorid-ndk to compile the module in / according
    #   to the specification inside //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)
    
  • 相关阅读:
    Python程序生成斐波那契数列
    ChatGPT 4.0 升级指南
    【深度学习】6-卷积过程中数据的结构变化
    小程序+Php获取微信手机号
    优思学院|六西格玛绿带和黑带的年薪收入有多少?
    NC61 两数之和
    rosbag遍历数据出错:(unicode error) 'utf-8' codec can't decode byte 0xcd in position 31: invalid continuation byte
    蓝牙智能音箱采用哪些音频功放芯片
    java 静态方法里边如何使用spring的注入对象
    【数据结构】手撕归并排序(含非递归)
  • 原文地址:https://blog.csdn.net/mushanshui/article/details/139712582