NDK:如何包含预建的共享库,无论架构如何

时间:2013-06-18 14:55:34

标签: android-ndk

我正在移植Box2D以了解更多有关Android移植的信息。

我可以编译项目,我看到以下内容......

  

ls libs /

     

armeabi armeabi-v7a

现在我想做this之类的事情,但我不知道如何让它足够智能选择拱门(比方说我想添加x86)。如何在没有硬编码规范拱的.so路径的情况下包含.so?

5 个答案:

答案 0 :(得分:15)

如NDK文档(APPLICATION-MK.html和PREBUILTS.html)中所述,只需在android.mk和Application.mk中定义您想要支持的体系结构:

  

诉ABI选择预建二进制文件:

     

如前所述,提供预建的共享库至关重要   在构建期间与目标ABI兼容。要做到这一点,   检查TARGET_ARCH_ABI的值,其值为:

     

armeabi =>当针对ARMv5TE或更高CPU的armeabi-v7a时   =>当针对ARMv7或更高CPU时x86 =>当定位x86 CPU时mips =>何时针对MIPS CPU

     

请注意,armeabi-v7a系统可以正常运行armeabi二进制文件。

     

以下是我们提供两个版本的预构建库的示例   并根据目标ABI选择要复制的那个:

include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
     

下面。我们假设要复制的预构建库是在   以下目录层次结构:

Android.mk            --> the file above
armeabi/libfoo.so     --> the armeabi prebuilt shared library
armeabi-v7a/libfoo.so --> the armeabi-v7a prebuilt shared library
include/foo.h         --> the exported header file
     

注意:请记住,您不需要提供armeabi-v7a预建         库,因为armeabi可以很容易地运行相应的         设备

答案 1 :(得分:14)

这有效......

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := box2D-prebuilt
LOCAL_SRC_FILES := ../Box2D/libs/$(TARGET_ARCH_ABI)/libbox2D.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/..
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := box2DHello
LOCAL_SRC_FILES := \
    $(subst $(LOCAL_PATH)/,, \
    $(wildcard $(LOCAL_PATH)/*.cpp))
LOCAL_LDLIBS := -lm -llog
LOCAL_SHARED_LIBRARIES := box2D-prebuilt
include $(BUILD_SHARED_LIBRARY)

答案 2 :(得分:1)

任何寻找有关任何android.mk问题的好文档的人都可以参考:

https://github.com/Parrot-Developers/alchemy/blob/master/doc/android-mk.html

我发现它用简单的单词解释得非常好,涵盖了围绕Android.mk的大部分场景。希望它有所帮助。

答案 3 :(得分:1)

Android NDK官方hello-libs CMake示例

https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs

关键是要打包您关注的.so的所有版本,例如:

  • distribution/gperf/lib/arm64-v8a/libgperf.so
  • distribution/gperf/lib/x86_64/libgperf.so

然后在CMake上选择正确的:${ANDROID_ABI}

我在How to link a prebuilt shared Library to an Android NDK project?

进一步解释了这个例子

答案 4 :(得分:0)

libmath-prebuilt.so是一个预建的库,它有一些由buyya_read.c使用的函数。首先使用ndk-build生成libmath-prebuilt.so并保存在jni文件夹中,其中buyya_read.c保存在elcipse中的ur项目中。

Android.mk(To generate libbuyya_read.so to use as jni library using prebuilt library)
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := math-prebuilt
    LOCAL_SRC_FILES = libmath-prebuilt.so
    include $(PREBUILT_SHARED_LIBRARY)

    include $(CLEAR_VARS)
    LOCAL_MODULE    := buyya_read
    LOCAL_SRC_FILES := buyya_read.c
    LOCAL_LDLIBS    := -llog
    LOCAL_SHARED_LIBRARIES := math-prebuilt
    include $(BUILD_SHARED_LIBRARY)

buyya_read.c
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>
#include <stdio.h>

JNIEXPORT jint JNICALL Java_com_buyya_BuyyaJava_sum(JNIEnv *env,
    jclass thiz, jint a, jint b) {
hello();
int res = add(a, b);
return res;
}

math.c // using this generate libmath-prebuilt.so
#include <stdio.h>
void hello()
{
    printf("Hello world!\n");
}
int add(int a, int b)
{
    printf("Hello world!\n");
    return a+b;
}

Make file to generate libmath-prebuilt.so
Android.mk
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE    := math-prebuilt
    LOCAL_SRC_FILES := math.c
    LOCAL_LDLIBS    := -llog
    #LOCAL_EXPORT_C_INCLUDES:=../jni/
    include $(BUILD_SHARED_LIBRARY)

您可以在下图中看到项目目录结构。 Project Dir Structure