Skip to content Skip to sidebar Skip to footer

Android Ndk: Static Library Used Is Different Than Precompiled Library Specified

I build a C++ library manually using the Android SDK compiler. The result is libMyUtils.a. I'm using the following in my Java/JNI test application: LOCAL_PATH := $(call my-dir) in

Solution 1:

A full explanation, as your Android.mk does not make much sense to me: sorry if you already know some of that.

Static libraries should be pure C/C++, and wrapped using the Android NDK in order to be usable from Java.

For example, assuming your static library is built from a simple .c and .h files:

highfive.h:

intgiveMeFive();

highfive.c:

#include"highfive.h"intgiveMeFive(){
  return5;
}

This can be compiled as a static library using the Android NDK compiler, which apparently you already know how to do: this will give us a highfive.a library.

In this form, this library is unusable from Java, but it can be wrapped using the Android NDK. See the Android NDK documentation for naming conventions etc...

highfiveWrapper.c:

#include"highfive.h"jint
Java_your_package_name_HighFive_giveMeFive(JNIEnv *env, jobject o){
  return (jint) giveMeFive();
}

and its corresponding Java file:

package your.package.name;

classHighFive {
  static {
    System.loadLibrary("highfive");
  }

  publicnativeintgiveMeFive();
}

Now, how do we compile all this to get it to work:

Android.mk:

include$(CLEAR_VARS)
LOCAL_MODULE            := libhighfive-prebuilt
LOCAL_SRC_FILES         := path/to/highfive.a
LOCAL_EXPORT_C_INCLUDES := path/to/highfive.h/folder
include$(PREBUILT_STATIC_LIBRARY)include$(CLEAR_VARS)
LOCAL_MODULE      := highfive
LOCAL_SRC_FILES   := path/to/highfiveWrapper.c
LOCAL_STATIC_LIBRARIES := libhighfive-prebuilt
include$(BUILD_SHARED_LIBRARY)

And there you should be able to use your native library as you wished to!

Hope this helps!

Solution 2:

Usually, static libraries contain many objects, which contain many functions, of which many are unused. That's why the linker only pulls the referenced objects from static libraries. So, in the example given by @mbrenon, highfiveWrapper.c defines which components of highfive.a will be linked into highfive.so.

But in your setup, you need whole static library to be loaded. OK, there is a special word for it: LOCAL_WHOLE_STATIC_LIBRARIES.

LOCAL_PATH := $(call my-dir)include$(CLEAR_VARS)
LOCAL_MODULE    := MyUtils
LOCAL_SRC_FILES := ../../../../libs/libMyUtils.a

include$(PREBUILT_STATIC_LIBRARY)

LOCAL_MODULE    := AndroidTests
LOCAL_WHOLE_STATIC_LIBRARIES    := MyUtils

include$(BUILD_SHARED_LIBRARY)

Solution 3:

In the end the solution was to build the MyUtils library as a prebuilt shared library. This way the linker doesn't strip anything. Then I modified my makefile as follows:

LOCAL_PATH := $(call my-dir)include$(CLEAR_VARS)
LOCAL_MODULE    := MyUtils
LOCAL_SRC_FILES := ../../../../libs/libMyUtils.so

include$(PREBUILT_SHARED_LIBRARY)

LOCAL_MODULE    := AndroidTests
LOCAL_SHARED_LIBRARIES    := MyUtils

include$(BUILD_SHARED_LIBRARY)

Notice the .so extention and the PREBUILT_SHARED_LIBRARY and BUILD_SHARED_LIBRARY scripts.

Post a Comment for "Android Ndk: Static Library Used Is Different Than Precompiled Library Specified"