I have an android project which consists of a java file and two ndk libraries 开发者_如何学Cone C++ and other JNI. JNI shared library loads the C++ static lib and call one of it's method. Here are all the files..
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Lib2
LOCAL_SRC_FILES := Lib2.cpp
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Lib1
LOCAL_SRC_FILES := Lib1.cpp
LOCAL_SHARED_LIBRARIES := Lib2
include $(BUILD_SHARED_LIBRARY)
Lib1.h
#include <jni.h>
extern "C"
{
JNIEXPORT void JNICALL Java_mine_twocpplibtest_TwocpplibtestActivity _TestMethod(JNIEnv * env, jobject obj);
};
Lib1.cpp
#include <jni.h>
#include "Lib1.h"
#include "Lib2.h"
using namespace Lib2ns;
JNIEXPORT void JNICALL Java_mine_twocpplibtest_TwocpplibtestActivity_TestMethod(JNIEnv * env, jobject obj)
{
Lib2::TestChanged();
}
Lib2.h
#ifndef Lib2_HEADER
#define Lib2_HEADER
using namespace std;
namespace Lib2ns
{
class Lib2
{
public:
static void TestChanged();
};
}
#endif
Lib2.cpp
#include "Lib2.h"
using namespace Lib2ns;
void Lib2::TestChanged()
{
}
This is my activity which loads Lib1
public class TwocpplibtestActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
static
{
System.loadLibrary("Lib1");
}
}
Problem is in Android.mk
file when I define Lib2
as include $(BUILD_STATIC_LIBRARY)
everything works fine but when I define Lib2
as include $(BUILD_SHARED_LIBRARY)
my activity crashes while trying to load the Lib1
. Any ideas why this is so? Can't we have two shared libraries at a time in a project?
How about this?
static
{
System.loadLibrary("Lib2");
System.loadLibrary("Lib1");
}
Your apk must include libLib2.so as libLib1.so.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Lib2
LOCAL_SRC_FILES := Lib2.cpp
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Lib1
LOCAL_SRC_FILES := Lib1.cpp
LOCAL_STATIC_LIBRARIES := Lib2
include $(BUILD_SHARED_LIBRARY)
"Lib2" is a static library, you should use LOCAL_STATIC_LIBRARIES
精彩评论