Reputation: 213
I am starting to learn about the Android NDK. I haven't learned much about C/C++ coding and would like to start. I imported the simple HelloJni sample and have been messing around with this to learn as well as the docs. I am stuck and feel it may be how I am using the Android.mk. Looks like it is right but the build is not working on the second module I am testing. Here is what I have in the
<project folder>/jni/
HelloJni.cpp
Android.mk
Application.mk
mytools/
simplemath.h
simplemath.cpp
I would like to use my java to call a method in HelloJni.cpp which in turn calls a method in simplemath.cpp and return a value. Simple test to just carry over number added from simplemath.
Below is all my source for each
---------------------Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
TOOLS_DIR := $(LOCAL_PATH)/mytools
LOCAL_C_INCLUDES := $(TOOLS_DIR)
LOCAL_MODULE := simplemath
LOCAL_SRC_FILES := $(TOOLS_DIR)/simplemath.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
---------------------HelloJni.cpp
#include <stl/_istream.h>
#include <stl/_string.h>
#include <stl/_string_fwd.h>
#define DEBUG_TAG "JNI Native Code..."
using namespace std;
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz, jstring str){
simplemath mathTools;
mathTools.add(4, 5);
.....
return env->NewStringUTF(str);
}
};
---------------------simplemath.h
#ifdef __cplusplus
extern "C" {
#endif
#define SIMPLEMATH_H
namespace std {
class simplemath{
public:
simplemath();
int add(int, int);
~simplemath();
};
}
#ifdef __cplusplus
}
#endif
-----------------simplemath.cpp
#include <simplemath.h>
namespace std {
simplemath::simplemath(){
}
int simplemath::add(int a, int b){
return a + b;
}
simplemath::~simplemath(){
}
}
When I do a clean on the project, I get no errors. After a build, I get the following:
"Compile++ thumb : HelloJni <= HelloJni.cpp
Prebuilt : libstlport_static.a <= <NDK>/sources/cxx-stl/stlport/libs/armeabi/
SharedLibrary : libHelloJni.so
C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:88: error: undefined reference to 'std::simplemath::simplemath()'
C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:89: error: undefined reference to 'std::simplemath::add(int, int)'
C:/Users/Geek/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:92: error: undefined reference to 'std::simplemath::~simplemath()'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libHelloJni.so] Error 1
Upvotes: 1
Views: 4355
Reputation: 14463
You haven't declared any dependency between your modules, use LOCAL_SHARED_LIBRARIES
to do it:
LOCAL_PATH := $(call my-dir)
TOOLS_DIR := $(LOCAL_PATH)/mytools
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(TOOLS_DIR)
LOCAL_EXPORT_C_INCLUDES := $(TOOLS_DIR)
LOCAL_MODULE := simplemath
LOCAL_SRC_FILES := $(TOOLS_DIR)/simplemath.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
LOCAL_SHARED_LIBRARIES := simplemath
LOCAL_LDLIBS := -llog
include $(PREBUILT_SHARED_LIBRARY)
Upvotes: 3