Hoops Visualize Integration In Android Platform

#1. Hoops Visualize SDK Review

  1. 首先前往Hoops官网开发者中心下载Hoops Viusalize系列Android平台的SDK工具包,目前Hoops官网最新支持[HOOPS_Visualize_2018_SP2_Android.zip]。
  2. 解压缩Zip包可以得到下述列表:


    Hoops解压缩.png

    i). bin目录下存放的是基于ANDROID_ABI的动态库。


    Hoops_Visualize_bin.png

    ii). include下存放的是项目依赖的一些头文件
    Hoops_Visualize_include.png

    iii). samples下包含了于官方给我们提供的demo——android_sandbox。


    Hoops_Visualize_samples.png

#2. Import Project Demo

  1. 打开AS,SDK intaller路径找到samples文件,导入android_sandbox工程


    image.png

编译工程过程中碰到如下问题:

i). google仓库依赖缺少
Could not find com.android.tools.build:aapt2:3.3.1-5013011.
Searched in the following locations:
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011.pom
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011-windows.jar
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011.pom
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011-windows.jar
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011.pom
  - file:/C:/Users/hhu/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011-windows.jar
  - https://jcenter.bintray.com/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011.pom
  - https://jcenter.bintray.com/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011-windows.jar
Required by:
    project :app

下述项找不到:

 - https://jcenter.bintray.com/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011.pom
  - https://jcenter.bintray.com/com/android/tools/build/aapt2/3.3.1-5013011/aapt2-3.3.1-5013011-windows.jar

原因是在module下的build.gradle配置文件中的缺少对google仓库的依赖:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

添加项目对g

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}
ii). APP_STL gnustl_shared is no longer supported

Application.mk

#APP_ABI := armeabi-v7a arm64-v8a
APP_ABI := armeabi-v7a
APP_STL := gnustl_shared
APP_PLATFORM=android-14
NDK_TOOLCHAIN_VERSION := 4.9

Application.mk的配置参数如上所示,在externalNativeBuildDebug模式下,提示的错误信息如下:

> Task :app:generateJsonModelDebug FAILED
process_begin: CreateProcess(NULL, "", ...) failed.
C:/Users/hhu/AppData/Local/Android/sdk/ndk-bundle/build//../build/core/add-application.mk:178: *** Android NDK: APP_STL gnustl_shared is no longer supported. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.    .  Stop.
process_begin: CreateProcess(NULL, "", ...) failed.
C:/Users/hhu/AppData/Local/Android/sdk/ndk-bundle/build//../build/core/add-application.mk:178: *** Android NDK: APP_STL gnustl_shared is no longer supported. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.    .  Stop.
process_begin: CreateProcess(NULL, "", ...) failed.
C:/Users/hhu/AppData/Local/Android/sdk/ndk-bundle/build//../build/core/add-application.mk:178: *** Android NDK: APP_STL gnustl_shared is no longer supported. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.    .  Stop.

FAILURE: Build failed with an exception.

gnustl_shared为C++运行时库,从下述信息可以得知NDK不再支持gnustl_shared,建议我们改用c++_static 或者 c++_shared. Android C++ library support

NDK: APP_STL gnustl_shared is no longer supported. Please switch to either c++_static or c++_shared. 
iii). GCC is no longer supported

将Application.mk更改为如下参数继续编译:

#APP_ABI := armeabi-v7a arm64-v8a
APP_ABI := armeabi-v7a
APP_STL := c++_shared
APP_PLATFORM=android-14
NDK_TOOLCHAIN_VERSION := 4.9

externalNativeBuildDebug模式下,提示的错误信息如下:

* What went wrong:
Execution failed for task ':app:generateJsonModelDebug'.
> Build command failed.
  Error while executing process C:\Users\hhu\AppData\Local\Android\sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\src\main\jni\Android.mk NDK_APPLICATION_MK=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\src\main\jni\Application.mk APP_ABI=armeabi-v7a NDK_ALL_ABIS=armeabi-v7a NDK_DEBUG=1 APP_PLATFORM=android-16 NDK_OUT=E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\build\intermediates\ndkBuild\debug\lib DUMMY_VAR=1 APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
  
  process_begin: CreateProcess(NULL, "", ...) failed.
  C:/Users/hhu/AppData/Local/Android/sdk/ndk-bundle/build//../build/core/setup-toolchain.mk:52: *** Android NDK: Invalid NDK_TOOLCHAIN_VERSION value: 4.9. GCC is no longer supported. See https://android.googlesource.com/platform/ndk/+/master/docs/ClangMigration.md.    .  Stop.

信息中提示GCC不再被android支持,由于 Android OS 放弃GCC转向了 Clang 编译器, 所以 NDK 将移除GCC, 所以建议用Clang编译你的程序。删除NDK_TOOLCHAIN_VERSION编译项即可。

#APP_ABI := armeabi-v7a arm64-v8a
APP_ABI := armeabi-v7a
APP_STL := c++_shared
APP_PLATFORM=android-14
#NDK_TOOLCHAIN_VERSION := 4.9
iii). Invalid HOOPS license

Problem:

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process C:\Users\hhu\AppData\Local\Android\sdk\ndk-bundle\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\src\main\jni\Android.mk NDK_APPLICATION_MK=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\src\main\jni\Application.mk APP_ABI=x86 NDK_ALL_ABIS=x86 NDK_DEBUG=1 APP_PLATFORM=android-16 NDK_OUT=E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\build\intermediates\ndkBuild\debug\lib DUMMY_VAR=1 E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/build/intermediates/ndkBuild/debug/obj/local/x86/libandroid_sandbox.so}
  
  [x86] Compile++      : android_sandbox <= AndroidMobileSurfaceViewJNI.cpp
  [x86] Compile++      : android_sandbox <= AndroidUserMobileSurfaceViewJNI.cpp
  [x86] Compile++      : android_sandbox <= MobileAppJNI.cpp
  [x86] Compile++      : android_sandbox <= MobileApp.cpp
  
  E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
  In file included from E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/shared/MobileApp.cpp:5:
  E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../include\hoops_license.h:6:5: error: Go to developer.techsoft3d.com to generate a valid HOOPS license
  #   error Go to developer.techsoft3d.com to generate a valid HOOPS license

Solution:
前往developer.techsoft3d.com生成License文件,然后替换掉hoops_license.h中的内容

至此NDK编译通过,项目可以正常run起来
4:00:24 PM: Executing task 'externalNativeBuildDebug'...

Executing tasks: [externalNativeBuildDebug]


> Configure project :app
ndkABIs: [armeabi-v7a, arm64-v8a, x86]
ndkArgs: DUMMY_VAR=1
WARNING: The specified Android SDK Build Tools version (26.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.3.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '26.0.3'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.

> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE

> Task :app:generateJsonModelDebug
process_begin: CreateProcess(NULL, "", ...) failed.
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
process_begin: CreateProcess(NULL, "", ...) failed.
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
process_begin: CreateProcess(NULL, "", ...) failed.
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"

> Task :app:externalNativeBuildDebug
Build android_sandbox x86
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
[x86] Compile++      : android_sandbox <= OnLoadJNI.cpp
[x86] Compile++      : android_sandbox <= AndroidMobileSurfaceViewJNI.cpp
[x86] Compile++      : android_sandbox <= AndroidUserMobileSurfaceViewJNI.cpp
[x86] Compile++      : android_sandbox <= MobileAppJNI.cpp
[x86] Compile++      : android_sandbox <= MobileApp.cpp
[x86] Compile++      : android_sandbox <= MobileSurface.cpp
[x86] Compile++      : android_sandbox <= UserMobileSurface.cpp
[x86] Prebuilt       : libA3DLIBS.so <= E:/Hoops/HOOPS_Exchange_2018_SP2_U2/bin/android/x86/
[x86] Prebuilt       : libhps_sprk_exchange.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_x86/
[x86] Prebuilt       : libhps_sprk_ops.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_x86/
[x86] Prebuilt       : libhps_sprk.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_x86/
[x86] Prebuilt       : libhps_core.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_x86/
[x86] Prebuilt       : libgnustl_shared.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_x86/
[x86] Prebuilt       : libc++_shared.so <= /sources/cxx-stl/llvm-libc++/libs/x86/
[x86] SharedLibrary  : libandroid_sandbox.so
Build android_sandbox arm64-v8a
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
[arm64-v8a] Compile++      : android_sandbox <= OnLoadJNI.cpp
[arm64-v8a] Compile++      : android_sandbox <= AndroidMobileSurfaceViewJNI.cpp
[arm64-v8a] Compile++      : android_sandbox <= AndroidUserMobileSurfaceViewJNI.cpp
[arm64-v8a] Compile++      : android_sandbox <= MobileAppJNI.cpp
[arm64-v8a] Compile++      : android_sandbox <= MobileApp.cpp
[arm64-v8a] Compile++      : android_sandbox <= MobileSurface.cpp
[arm64-v8a] Compile++      : android_sandbox <= UserMobileSurface.cpp
[arm64-v8a] Prebuilt       : libA3DLIBS.so <= E:/Hoops/HOOPS_Exchange_2018_SP2_U2/bin/android/arm64-v8a/
[arm64-v8a] Prebuilt       : libhps_sprk_exchange.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_arm64-v8a/
[arm64-v8a] Prebuilt       : libhps_sprk_ops.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_arm64-v8a/
[arm64-v8a] Prebuilt       : libhps_sprk.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_arm64-v8a/
[arm64-v8a] Prebuilt       : libhps_core.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_arm64-v8a/
[arm64-v8a] Prebuilt       : libgnustl_shared.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_arm64-v8a/
[arm64-v8a] Prebuilt       : libc++_shared.so <= /sources/cxx-stl/llvm-libc++/libs/arm64-v8a/
[arm64-v8a] SharedLibrary  : libandroid_sandbox.so
Build android_sandbox armeabi-v7a
E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/hps.mk:43: "the value of HEXCHANGE_INSTALL_DIR is E:/Hoops/HOOPS_Exchange_2018_SP2_U2"
[armeabi-v7a] Compile++ thumb: android_sandbox <= OnLoadJNI.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= AndroidMobileSurfaceViewJNI.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= AndroidUserMobileSurfaceViewJNI.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= MobileAppJNI.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= MobileApp.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= MobileSurface.cpp
[armeabi-v7a] Compile++ thumb: android_sandbox <= UserMobileSurface.cpp
[armeabi-v7a] Prebuilt       : libA3DLIBS.so <= E:/Hoops/HOOPS_Exchange_2018_SP2_U2/bin/android/armeabi-v7a/
[armeabi-v7a] Prebuilt       : libhps_sprk_exchange.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_armeabi-v7a/
[armeabi-v7a] Prebuilt       : libhps_sprk_ops.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_armeabi-v7a/
[armeabi-v7a] Prebuilt       : libhps_sprk.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_armeabi-v7a/
[armeabi-v7a] Prebuilt       : libhps_core.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_armeabi-v7a/
[armeabi-v7a] Prebuilt       : libgnustl_shared.so <= E:/Hoops/HOOPS_Visualize_2018_SP2/samples/android_sandbox/app/src/main/jni/../../../../../../bin/android_armeabi-v7a/
[armeabi-v7a] Prebuilt       : libc++_shared.so <= /sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/
[armeabi-v7a] SharedLibrary  : libandroid_sandbox.so

BUILD SUCCESSFUL in 45s
3 actionable tasks: 2 executed, 1 up-to-date
4:01:10 PM: Task execution finished 'externalNativeBuildDebug'.

3. Hoops Visualize项目迁移

Demo中的工程采用的是ndk-build+.mk编译的工程,实际项目中采用的是CMake+CMakeLists.txt编译的native工程。迁移过程的难点在于就编译工具与参数的理解,以及新编译工具和编译规则的适应。

Demo android_sandbox

Demo中采用了ndk-build+.mk编译ndk,所以重点分析相关的编译脚本配置文件:
#1. hps.mk
hps.mk文件的详细信息如下:

LOCAL_PATH := $(call my-dir)

#设置变量赋值为1,该变量用来控制依赖库的加载模式,release或debug
#MY_USE_DEBUG_HPS_LIBS := 1

# 设置变量赋值为1,该变量用来控制是否加载hoops exchange库所依赖项
USING_EXCHANGE := 1

ifeq ($(MY_USE_DEBUG_HPS_LIBS),1)
    MY_HPS_DEBUG_SUFFIX := d
endif

# 当前hps.mk文件的路径为:#1.E:\Hoops\HOOPS_Visualize_2018_SP2\samples\android_sandbox\app\src\main\jni
# 当前工程依赖的.so文件的路径为[以android_armeabi-v7a系列文件为例]:
#2.E:\Hoops\HOOPS_Visualize_2018_SP2\bin\android_armeabi-v7a
# 这里定义一个变量MY_HPS_LIB_PATH  记录下.so库相对于当前hps.mk文件的路径
# 仔细对比1和2就能发现
MY_HPS_LIB_PATH  := ../../../../../../bin/android_$(TARGET_ARCH_ABI)$(MY_HPS_DEBUG_SUFFIX)

# 添加hps_core依赖库,对应libhps_core.so
include $(CLEAR_VARS)
LOCAL_MODULE := hps_core
LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libhps_core.so
include $(PREBUILT_SHARED_LIBRARY)

# 添加hps_sprk依赖库,对应libhps_sprk.so
include $(CLEAR_VARS)
LOCAL_MODULE := hps_sprk
LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libhps_sprk.so
include $(PREBUILT_SHARED_LIBRARY)

# 添加hps_sprk_ops依赖库,对应libhps_sprk_ops.so
include $(CLEAR_VARS)
LOCAL_MODULE := hps_sprk_ops
LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libhps_sprk_ops.so
include $(PREBUILT_SHARED_LIBRARY)

# 添加gnustl_shared依赖库,对应libgnustl_shared.so
include $(CLEAR_VARS)
LOCAL_MODULE := gnustl_shared
LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libgnustl_shared.so
include $(PREBUILT_SHARED_LIBRARY)

# 如果定义了变量USING_EXCHANGE,并且其赋值为1
# 则加载hoops exchange系列的依赖库:libhps_sprk_exchange.so & libA3DLIBS.so
ifeq ($(USING_EXCHANGE),1)
    # 添加hps_sprk_exchange依赖库, 对应libhps_sprk_exchange.so
    include $(CLEAR_VARS)
    LOCAL_MODULE := hps_sprk_exchange
    LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libhps_sprk_exchange.so
    include $(PREBUILT_SHARED_LIBRARY)
    
    # 添加A3DLIBS依赖库,对应libA3DLIBS.so
    include $(CLEAR_VARS)
    LOCAL_MODULE := A3DLIBS
    HEXCHANGE_INSTALL_DIR := $(subst \,/,E:\Hoops\HOOPS_Exchange_2018_SP2_U2)
    $(warning "the value of HEXCHANGE_INSTALL_DIR is $(HEXCHANGE_INSTALL_DIR)")
    HEXCHANGE_LIB_DIR := $(HEXCHANGE_INSTALL_DIR)/bin/android/$(TARGET_ARCH_ABI)

    LOCAL_SRC_FILES := $(HEXCHANGE_LIB_DIR)/libA3DLIBS.so
    include $(PREBUILT_SHARED_LIBRARY)
endif

#2. android_sandbox.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# 指定编译模块的名字为:android_sandbox
LOCAL_MODULE := android_sandbox

# 指定编译编译器编译C源文件时的参数
LOCAL_CFLAGS := -DANDROID_NDK \
                -DUNIX_SYSTEM \
                -DHPS_CORE_BUILD \
                -DTARGET_OS_ANDROID=1 \
                -DLINUX_SYSTEM \
                -fsigned-char \
                -std=c++11

USING_EXCHANGE := 1

ifeq ($(USING_EXCHANGE),1)
    LOCAL_CFLAGS += -DUSING_EXCHANGE=1
endif

# 使用C++异常
LOCAL_CPP_FEATURES := exceptions

# 指定头文件的路径,这里使用的是相对路径,相对于当前的.mk文件
LOCAL_C_INCLUDES += $(LOCAL_PATH)/shared
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../../../../include

# -- sip --
$(info $(shell python $(TOP_PATH)/../../../../sip/sip.py))
# ---

# --- MobileSurface Base & JNI files ---
LOCAL_SRC_FILES += OnLoadJNI.cpp
LOCAL_SRC_FILES += AndroidMobileSurfaceViewJNI.cpp
LOCAL_SRC_FILES += AndroidUserMobileSurfaceViewJNI.cpp      # Generated
LOCAL_SRC_FILES += MobileAppJNI.cpp                     # Generated
LOCAL_SRC_FILES += shared/MobileApp.cpp
LOCAL_SRC_FILES += shared/MobileSurface.cpp
# ---

# --- User files ---
LOCAL_SRC_FILES += shared/UserMobileSurface.cpp
# ---

# Note: Link order below important

ifeq ($(USING_EXCHANGE),1)
    LOCAL_SHARED_LIBRARIES += A3DLIBS
    LOCAL_SHARED_LIBRARIES += hps_sprk_exchange
endif
LOCAL_SHARED_LIBRARIES += hps_sprk_ops
LOCAL_SHARED_LIBRARIES += hps_sprk
LOCAL_SHARED_LIBRARIES += hps_core
LOCAL_SHARED_LIBRARIES += gnustl_shared
LOCAL_LDLIBS    := -landroid -llog

include $(BUILD_SHARED_LIBRARY)
对其中一些变量和参数的说明:
#
# LOCAL_PATH 
#
# This variable is used to give the path of the current file. You must define it at the start of your Android.mk file. 
# ------------------------------------------------------------------------------------------------------------------------
# 每个Android.mk文件都必须在开头定义 LOCAL_PATH 变量。这个变量可以用来寻找C/C++源文件。
# 在该例中,my-dir 是一个由编译系统提供的宏函数,用于返回hps.mk所在目录的路径。
LOCAL_PATH := $(call my-dir)

#
# CLEAR_VARS
#
# This variable points to a build script that undefines nearly all LOCAL_XXX variables listed in the 
# "Developer-defined variables" section below. 
# Use this variable to include this script before describing a new module.
# ------------------------------------------------------------------------------------------------------------------------
# CLEAR_VARS是编译系统预定义的一个变量,它指向一个特殊的Makefile,
# 这个Makefile负责清除 LOCAL_xxx 的变量(例如 LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_STATIC_LIBRARIES 等)但不会清除 LOCAL_PATH。
# 之所以需要清理这些变量是因为所有的编译控制文件是在一趟make执行过程中完成的,而所有的变量都是全局的,会对其他Android.mk文件产生影响。
include $(CLEAR_VARS)

#
# LOCAL_MODULE 
#
# This variable stores the name of your module. 
# It must be unique among all module names, and must not contain any spaces. 
# You must define it before including any scripts (other than the one for CLEAR_VARS).
# You need not add either the lib prefix or the .so or .a file extension; 
# the build system makes these modifications automatically.
# ------------------------------------------------------------------------------------------------------------------------
# LOCAL_MODULE 用来给每个模块定义一个名字,不同模块的名字不能相同,不能有空格。
# 这里的名字会传给NDK编译系统,然后加上lib前缀和.so后缀 (例如,变成libhello-jni.so)
# 注意,如果你在LOCAL_MODULE定义中自己加上了lib前缀,则ndk在处理的时候就不会再加上lib前缀了(为了兼容Android系统的一些源码)。
LOCAL_MODULE := hps_core

#
# LOCAL_SRC_FILES 
#
# This variable contains the list of source files that the build system uses to generate the module.
# Only list the files that the build system actually passes to the compiler, 
# since the build system automatically computes any associated depencies. 
# Note that you can use both relative (to LOCAL_PATH) and absolute file paths.
# We recommend avoiding absolute file paths; relative paths make your Android.mk file more portable.
# Note: Always use Unix-style forward slashes (/) in build files. 
# The build system does not handle Windows-style backslashes (\) properly.
# ------------------------------------------------------------------------------------------------------------------------
# 在LOCAL_SRC_FILES 变量里面列举出对应于同一个模块的、要编译的那些文件,
# 这里不要把头文件加进来,编译系统可以自动检测头文件依赖关系。
LOCAL_SRC_FILES := $(MY_HPS_LIB_PATH)/libhps_core.so

#
# PREBUILT_SHARED_LIBRARY
#
# Points to a build script used to specify a prebuilt shared library.
# Unlike in the case of BUILD_SHARED_LIBRARY and BUILD_STATIC_LIBRARY,
# here the value of LOCAL_SRC_FILES cannot be a source file.
# Instead, it must be a single path to a prebuilt shared library, such as foo/libfoo.so. 
# ------------------------------------------------------------------------------------------------------------------------
# 用来引入已经编译好的库,这里不能是源文件,必须是预先编译好的依赖库
include $(PREBUILT_SHARED_LIBRARY)

#
# LOCAL_CPP_FEATURES
#
# You can use this optional variable to indicate that your code relies on specific C++ features.
# It enables the right compiler and linker flags during the build process.
# For prebuilt binaries, this variable also declares which features the binary depends on, 
# thus helping ensure the final linking works correctly. 
# For example, to indicate that your code uses RTTI (RunTime Type Information), write:
# LOCAL_CPP_FEATURES := rtti
# To indicate that your code uses C++ exceptions, write:
# LOCAL_CPP_FEATURES := exceptions
# You can also specify multiple values for this variable. For example:
# LOCAL_CPP_FEATURES := rtti features
# ------------------------------------------------------------------------------------------------------------------------
LOCAL_CPP_FEATURES := exceptions

#
# LOCAL_C_INCLUDES
#
# You can use this optional variable to specify a list of paths, relative to the NDK root directory,
# to add to the include search path when compiling all sources (C, C++ and Assembly).
# ------------------------------------------------------------------------------------------------------------------------
LOCAL_C_INCLUDES += $(LOCAL_PATH)/shared

ndk-build编译脚本到CMake编译脚本的转换
官方给出的Demo采用的编译方式是ndk-build+.mk(ndk-build的build-script)的形式,在我们现有的项目中,采用的native工程编译方式是CMake+CMakeLists.txt(CMake的build-script)。所以下述内容重点讲述CMakeLists的配置规则。

#3.CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)

# Set project name
project(hoops_sandbox)

# Open cmake debug messgae
set(CMAKE_VERBOSE_MAKEFILE on)

# Using set command to set a global lib_DIR property which indicates .so file location
# CMAKE_SOURCE_DIR 源码树的顶层路径。
# CMAKE_ANDROID_ARCH_ABI [arm64-v8a|armeabi-v7a|armeabi-v6|armeabi|mips|mips64|x86|x86_64]
set(lib_DIR ${CMAKE_SOURCE_DIR}/../jniLibs)
MESSAGE("CMAKE_SOURCE_DIR is ${CMAKE_SOURCE_DIR}")
MESSAGE("show libdir:${lib_DIR}")

set(cpp_DIR ${CMAKE_SOURCE_DIR})
MESSAGE("cpp_DIR is ${cpp_DIR}")

# Add the given directories to those the compiler uses to search for include files. 
# Relative paths are interpreted as relative to the current source directory.
include_directories(${cpp_DIR}/shared)
include_directories(${cpp_DIR}/include)

# Find all source files in a directory.
# Find all source files in current directory,store those source files into var SRC_LIST
aux_source_directory(. SRC_LIST)
MESSAGE("find source file under . ${SRC_LIST}")

# Find all source files in current dir's sub dir which name is shared,store those files into var SHARED_SRC_LIST
aux_source_directory(./shared SHARED_SRC_LIST)
MESSAGE("find all source file under shared dir. ${SHARED_SRC_LIST}")

# Append  sources in SHARED_SRC_LIST into SRC_LIST 
list(APPEND SRC_LIST ${SHARED_SRC_LIST})
MESSAGE("final SRC_LIST: ${SRC_LIST}")

#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
        ${PROJECT_NAME}
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        ${SRC_LIST})

find_library( # Sets the name of the path variable.
        log-lib
        log)
find_library(android-lib android)

# Import libhps_core.so
add_library(hps_core SHARED IMPORTED)
set_target_properties(hps_core PROPERTIES IMPORTED_LOCATION
        ${lib_DIR}/${ANDROID_ABI}/libhps_core.so)

# Import libhps_sprk.so
add_library(hps_sprk SHARED IMPORTED)
set_target_properties(hps_sprk PROPERTIES IMPORTED_LOCATION
        ${lib_DIR}/${ANDROID_ABI}/libhps_sprk.so)

# Import libhps_sprk_ops.so
add_library(hps_sprk_ops SHARED IMPORTED)
set_target_properties(hps_sprk_ops PROPERTIES IMPORTED_LOCATION
        ${lib_DIR}/${ANDROID_ABI}/libhps_sprk_ops.so)

#Import libgnustl_shared.so
add_library(gnustl_shared SHARED IMPORTED)
set_target_properties(gnustl_shared PROPERTIES IMPORTED_LOCATION
        ${lib_DIR}/${ANDROID_ABI}/libgnustl_shared.so)

target_link_libraries( # Specifies the target library.
        ${PROJECT_NAME}

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${android-lib}

        hps_core
        hps_sprk
        hps_sprk_ops
        gnustl_shared)
对其中一些变量和参数的说明:
#
# project( [...])
#
# Sets the name of the project, and stores it in the variable PROJECT_NAME.
# When called from the top-level CMakeLists.txt also stores the project name in the variable CMAKE_PROJECT_NAME.
# ------------------------------------------------------------------------------------------------------------------------
# 设置native编译工程的名字,可以通过PROJECT_NAME变量引用到设置的名字
project(hoops_sandbox)

#
# set
#
# Set the debug message level as Verbose
# ------------------------------------------------------------------------------------------------------------------------
# 设置调式信息的输出级别为verbose
set(CMAKE_VERBOSE_MAKEFILE on)

#
# include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
#
# Add the given directories to those the compiler uses to search for include files. 
# Relative paths are interpreted as relative to the current source directory.
# ------------------------------------------------------------------------------------------------------------------------
# 给出包含头文件的相对路径,编译器会去指定路径中查找目标文件
include_directories(${cpp_DIR}/shared)

#
# aux_source_directory( )
#
# Collects the names of all the source files in the specified directory and stores the list in the  provided. 
# ------------------------------------------------------------------------------------------------------------------------
# 找出当前路径下的所有源文件.CXX
aux_source_directory(. SRC_LIST)

#
# list(APPEND  [ ...])
#
# Appends elements to the list.
# ------------------------------------------------------------------------------------------------------------------------
# 将SHARED_SRC_LIST中存储的所有源文件append到SRC_LIST中
list(APPEND SRC_LIST ${SHARED_SRC_LIST})

#
# add_library( [STATIC | SHARED | MODULE]
#           [EXCLUDE_FROM_ALL]
#           [source1] [source2 ...])
#
# Adds a library target called  to be built from the source files listed in the command invocation.
# ------------------------------------------------------------------------------------------------------------------------
# 根据给定的源文件列表编译出指定名字的库
add_library( # Sets the name of the library.
        ${PROJECT_NAME}
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        ${SRC_LIST}) 

#
# find_library ( name1 [path1 path2 ...])
# 
# This command is used to find a library.
# A cache entry named by  is created to store the result of this command.
# ------------------------------------------------------------------------------------------------------------------------
# 查找系统库,并将找到的库存储在android-lib变量中
find_library(android-lib android)

#
# add_library(  IMPORTED
#            [GLOBAL])
#
# An IMPORTED library target references a library file located outside the project
# Details about the imported library are specified by setting properties whose names begin in IMPORTED_ and INTERFACE_. 
# The most important such property is IMPORTED_LOCATION
# ------------------------------------------------------------------------------------------------------------------------
# 以动态库的方式导入库,并将库的名字命名为hps_core
add_library(hps_core SHARED IMPORTED)

#
# set_target_properties(target1 target2 ...
#                     PROPERTIES prop1 value1
#                     prop2 value2 ...)
#
# Sets properties on targets. The syntax for the command is to list all the targets you want to change, 
# and then provide the values you want to set next.
# ------------------------------------------------------------------------------------------------------------------------
# 以动态库的方式导入库,并将库的名字命名为hps_core,同时指定导入库的IMPORTED_LOCATION属性
set_target_properties(hps_core PROPERTIES IMPORTED_LOCATION
        ${lib_DIR}/${ANDROID_ABI}/libhps_core.so)

#
# target_link_libraries( ... ... ...)
#
# Specify libraries or flags to use when linking a given target and/or its dependents.
# The named  must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target.
# The named target must be created by add_library() within the project or as an IMPORTED library.
# ------------------------------------------------------------------------------------------------------------------------
# 指定要在链接给定目标和/或其依赖项时使用的库或标志,目标文件必须是通过add_executable() 或者add_library() 创建成功
# 目标库在工程内通过add_library()或者是IMPORTED 库的文件
target_link_libraries( # Specifies the target library.
        ${PROJECT_NAME}

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${android-lib}

        hps_core
        hps_sprk
        hps_sprk_ops
        gnustl_shared)
Project overivew
Demo_3d.png
参考链接如下:

Android SO文件的兼容和适配
Android.mk Google文档
Android.mk语法详解
CMake配置
CMake Command官方文档

你可能感兴趣的:(Hoops Visualize Integration In Android Platform)