这是我实际上很容易解决问题的方法。感谢@auselen 的帮助。
您有一个在 x86 架构上失败的常规 Android.mk,因为您使用的库 (libExternalLibrary.so) 仅提供给 arm 架构。您想基于此库构建一个 .so (libMyLibraryBasedOnExternalLibrary.so)。
1) 创建 2 个虚拟 .cpp 文件 Dummy0.cpp 和 Dummy1.cpp 示例 Dummy0.cpp 如下所示:
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <string>
#ifdef __cplusplus
extern "C"
{
#endif
int dummy0 = 0;
#ifdef __cplusplus
}
#endif
然后,编辑构建你的库的 Android.mk 并像这样修改它:
LOCAL_PATH := $(call my-dir)
ifeq ($(TARGET_ARCH_ABI), armeabi)
# In this condtion block, we're compiling for arm architecture, and the libExternalLibrary.so is avaialble
# Put every thing the original Android.mk was doing here, importing the prebuilt library, compiling the shared library, etc...
# ...
# ...
else
# In this condtion block, we're not compiling for arm architecture, and the libExternalLibrary.so is not availalble.
# So we create a dummy library instead.
include $(CLEAR_VARS)
# when LOCAL_MODULE equals to ExternalLibrary, this will create a libExternalLibrary.so, which is exactly what we want to do.
LOCAL_MODULE := ExternalLibrary
LOCAL_SRC_FILES := Dummy0.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
# This will create a libMyLibraryBasedOnExternalLibrary.so
LOCAL_MODULE := MyLibraryBasedOnExternalLibrary
# Don't forget to tell this library is based on ExternalLibrary, otherwise libExternalLibrary.so will not be copied in the libs/x86 directory
LOCAL_SHARED_LIBRARIES := ExternalLibrary
LOCAL_SRC_FILES := Dummy1.cpp
include $(BUILD_SHARED_LIBRARY)
endif
当然,请确保在您的代码中,当您的应用在仅 x86 的设备上运行时,您永远不会调用该库:
if ((android.os.Build.CPU_ABI.equalsIgnoreCase("armeabi")) || (android.os.Build.CPU_ABI2.equalsIgnoreCase("armeabi"))) {
// Good I can launch
// Note that CPU_ABI2 is api level 8 (v2.2)
// ...
}