19

I am working on porting Box2D to learn a little more about android porting.

I can get the project compiling and I see the following....

ls libs/

armeabi armeabi-v7a

Now I want to do something like this but i don't know how to make it smart enough to choose arch (say I wanted to add x86). How do I include the .so without hard coding the .so path to a spec arch?

Community
  • 1
  • 1
Jackie
  • 21,969
  • 32
  • 147
  • 289

5 Answers5

20

This worked...

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)
Jackie
  • 21,969
  • 32
  • 147
  • 289
  • i am facing the same problem but i have only to use the .so file. i have follow your instruction write 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) but this give error in prebuiltlibaray.mk – Mudassir Khan Oct 31 '17 at 14:02
18

Simply define which architectures you would like to support in android.mk and Application.mk as described in the NDK documentation (APPLICATION-MK.html and PREBUILTS.html):

V. ABI Selection of prebuilt binaries:

As said previously, it is crucial to provide a prebuilt shared library that is compatible with the targeted ABI during the build. To do that, check for the value of TARGET_ARCH_ABI, its value will be:

armeabi => when targeting ARMv5TE or higher CPUs armeabi-v7a => when targeting ARMv7 or higher CPUs x86 => when targeting x86 CPUs mips => when targeting MIPS CPUs

Note that armeabi-v7a systems can run armeabi binaries just fine.

Here's an example where we provide two versions of a prebuilt library and select which one to copy based on the target 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)

Here. we assume that the prebuilt libraries to copy are under the following directory hierarchy:

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

NOTE: Remember that you don't need to provide an armeabi-v7a prebuilt library, since an armeabi one can easily run on the corresponding devices.

Erik
  • 804
  • 1
  • 8
  • 18
  • Looks like you were right after my answer which is pretty much the same. Thanks for trying to help! – Jackie Jun 18 '13 at 15:32
4

Android NDK official hello-libs CMake example

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

The key thing is to pack all versions of the .so that you care about, e.g.:

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

and then pick the right one on CMake with: ${ANDROID_ABI}.

I have further explained that example at: How to link a prebuilt shared Library to an Android NDK project?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
3

Anyone looking for good documentation around any android.mk problems, refer this:

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

I found it very nicely explained in simple words and covering most of scenarios around Android.mk. Hope it helps.

sgupta
  • 535
  • 4
  • 8
1

libmath-prebuilt.so is a prebuilt library which has some functions which are used by buyya_read.c. First generate libmath-prebuilt.so using ndk-build and keep in jni folder where buyya_read.c is kept in ur project in elcipse.

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)

You see the project directory structure in below image. Project Dir Structure

Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25