This is my directory structure...
> Classes [dir]
> ---subdir1
> ------ some .cpp and .h files
> ---subdir2
> ------ some .cpp and .h files
> ---more .cpp and .h files
> ---Android.mk
This is the code for Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic_static
LOCAL_MODULE_FILENAME := libgame_logic
LOCAL_SRC_FILES := AppDelegate.cpp \
HelloWorldScene.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_STATIC_LIBRARIES := png_static_prebuilt
LOCAL_STATIC_LIBRARIES += xml2_static_prebuilt
LOCAL_STATIC_LIBRARIES += jpeg_static_prebuilt
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_SHARED_LIBRARIES := cocosdenshion_shared
include $(BUILD_STATIC_LIBRARY)
$(call import-module,cocos2dx/platform/third_party/android/modules/libpng)
$(call import-module,cocos2dx/platform/third_party/android/modules/libxml2)
$(call import-module,cocos2dx/platform/third_party/android/modules/libjpeg)
Can anyone post the correct code (complete if possible) for Android.mk so all .cpp will be compiled in Class directory? Thanks!
Note: Trying to develop cocos2d-x Android with Eclipse
More notes: I already tried these to no avail:
Android.mk file - including all source files in different folders and
subfolders How to write android.mk file with source files in
subdirectories? Android.mk, include all cpp files
Related
I am developing an android application including native code
First I compile a prebuilt library. The Android.mk file is project_path/jni/qt-library/Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := qt5Core-prebuilt
LOCAL_SRC_FILES := libQt5Core.so
LOCAL_EXPORT_C_INCLUDES += qtbase/include
include $(PREBUILT_SHARED_LIBRARY)
Then I compile my library whose the Android.mk file is project_path/jni/my-library/Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myLibrary
LOCAL_SRC_FILES := com_example_qtsignaux_Library.cpp
LOCAL_SRC_FILES += Receiver.cpp
LOCAL_SRC_FILES += Emitter.cpp
LOCAL_SRC_FILES += moc_Receiver.cpp
LOCAL_SRC_FILES += moc_Emitter.cpp
LOCAL_SHARED_LIBRARIES := qt5Core-prebuilt
LOCAL_C_INCLUDES += ../qt-library/qtbase/include
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
When I compile the jni folder using the ndk-build script, everything works well but get the following error when calling :
static {
System.loadLibrary("myLibrary");
}
java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad in "/data/app/com.example.qtsignaux-1/lib/arm/libmyLibrary.so"
Notice I unziped the apk folder and every libaries : libQt5Core.so, libmyLibrary.so and libgnustl_shared.so are in the lib/armeabi/ and lib/armeabi-v7a/ folders
My Application.mk file looks like :
APP_ABI += armeabi armeabi-v7a
APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions
I don't really understand what the error means and why the system try to load a library in the application_package_name/lib/arm folder. Does this folder really exist or this is just armeabi and armeabi-v7a in my situation?
Thank you in advance for your help
I finally fixed my problem. This was because of libQt5Core.so file
Indeed this library implements the JNI_OnLoad() function in which it tries to load some java classes contained in an external jar file.
To solve my problem I included the right jar file in the /libs
I'am using cocos2d-x version 3.3rc
In some of my class I #include "cocos2d/external/json/writer.h"
when I want to compile my project to make android apk, by running build_native.py script, it shows the following error to me:
fatal error: cocos2d/external/json/writer.h: No such file or directory
But I include its file in my android.mk file with this syntax:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../cocos2d/external/json \
And here is the complete android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/Database/CacheData.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../cocos2d/external/json
#LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += json_external_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
How can I fix the problem?
I found the solution.
In cocos2d-x version 3.3 the default android.mk file that generated is sufficient and all other library was imported. Then the default android.mk file in jni folder was completely integrated with other .mk files. i see that the files in json folder that located in 'cocos2s/external/json' are .h files and there is no dependency between them and other cocos2d files and library.
then I simply copy this folder to my Classes folder and include them locally.Now the problem is solved :)
In my Android.mk file i have something like this
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := testmodule
FILE_LIST := $(LOCAL_PATH)/include/md5/md5.с
FILE_LIST += $(LOCAL_PATH)/include/md5/md5main.с
FILE_LIST += $(wildcard $(LOCAL_PATH)/include/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
include $(BUILD_SHARED_LIBRARY)
but when i run ndk-build - i get the following error
Android NDK: WARNING: Unsupported source file extensions in /Users/some/path/jni/Android.mk for module testmodule
Android NDK: include/md5/md5.с include/md5/md5main.с
(I use android-ndk-r8c on OSX 10.9.2)
How can i add *.c file the Android.mk? What could i be doing wrong?
(I can post more of the Android.mk and Application.mk if needed)
Just to move the answer from the comments, like #greenapps said this works:
You could rename them to .cpp and if that is not possible
create a .cpp file in which you include both .c files.
In an effort to reduce bandwidth usage of an application, I tried to make an implementation of opus.
First, I cross-compiled the sources into a shared library. I copied the resulting .so file into an opus folder inside my jni folder. I also copied all opus header files into an include subfolder in the opus folder. Finally, I created an Android.mk file to allow the usage of this .so file in my implementation.
So my file structure is as follows:
/jni
/opus
/include
All opus header files
Android.mk
libopus.so
Android.mk
OpusEncoder.h
OpusDecoder.h
OpusEncoder.c
OpusDecoder.c
The Android.mk file in the opus subfolder has the following content:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := opus
LOCAL_SRC_FILES := jni/opus/libopus.so
LOCAL_C_INCLUDES := jni/opus/include
include $(PREBUILD_SHARED_LIBRARY)
As is recommended to expose the .so file for usage.
Now, in my implementation of the Encoder/Decoder, I import opus.h, and use the appropriate functions in order to expose them through JNI to the Java layer.
The Android.mk in my jni folder (which should build my JNI layer) is as follows:
LOCAL_PATH := $(call my-dir)
MY_DIR := $(LOCAL_PATH)
include $(CLEAR_VARS)
include jni/opus/Android.mk
APP_ABI := armeabi armeabi-v7a
LOCAL_PATH := $(MY_DIR)
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_LDLIBS += -llog -landroid
#LOCAL_LDLIBS += $(LIB_PATH) -lopus
LOCAL_SHARED_LIBRARIES := opus
LOCAL_MODULE := OpusCodec
LOCAL_SRC_FILES := OpusEncoder.c \
OpusDecoder.c
include $(BUILD_SHARED_LIBRARY)
However, when I run the ndk-build command, I get the following 6 error messages:
error: undefined reference to 'opus_encoder_create'
error: undefined reference to 'opus_encode'
error: undefined reference to 'opus_encoder_destroy'
error: undefined reference to 'opus_decoder_create'
error: undefined reference to 'opus_decode'
error: undefined reference to 'opus_decoder_destroy'
Which are all methods exposed by opus.h in libopus.so. Any ideas?
Okay, so this all turned out to be a typographical error on my behalf.
For the keen observer, to make the prebuilt opus library, I used the call include $(PREBUILD_SHARED_LIBRARY) where it should have been include $(PREBUILT_SHARED_LIBRARY) (the difference being the T instead of D at the end of PREBUILT).
As a result, it didn't create the library that I tried to link to with LOCAL_SHARED_LIBRARIES := opus in my main Android.mk file.
So this is how I have my Android.mk file for the prebuilt library:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libopus
LOCAL_SRC_FILES := libopus.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
And this is my Android.mk file for the code I wrote:
MY_PROJECT_PATH := $(call my-dir)
LOCAL_PATH = $(MY_PROJECT_PATH)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
APP_ABI := armeabi armeabi-v7a
LOCAL_PATH = $(MY_PROJECT_PATH)
LOCAL_MODULE := OpusCodec
LOCAL_SHARED_LIBRARIES := libopus
LOCAL_LDLIBS += -llog -landroid
LOCAL_SRC_FILES := OpusEncoder.c OpusDecoder.c
include $(BUILD_SHARED_LIBRARY)
Other than the cosmetic change to the prebuilt library module, and restructuring my main Android.mk file, I didn't have to change a thing (you don't need to specify the LIB_PATH as I originally tried).
I know that to build so file, I should put the source file in /jni/ folder.
But how can I build separate so in different folders.
For example, the structure of my project:
/jni/Android.mk
/jni/submodule1/Android.mk
/jni/submodule1/sub1.c
/jni/submodule2/Android.mk
/jni/submodule2/sub2.c
I have tried to write this in Android.mk in the top level:
$(LOCAL_PATH) :=$(call all-makefiles-under)
then wrote make info in Android.mk in submodule
the error is:
ndk-build
make: *** No rule to make target `/home/../workspace/jni/sub.c', needed b
y `/home/../workspace/obj/local/armeabi/objs/submodule/sub.o'. Stop.
Can someone give me a solution? Thank you!
Update 1:
The code of Android.mk in submoudle:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := sub.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE := sub
LOCAL_LDLIBS := -ldl -llog
LOCAL_STATIC_LIBRARIES := libc
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := debug
include $(BUILD_EXECUTABLE)
SOLVED:
I should use ndk-build in the root directory, but not in jni directory. Thanks all of you!
You should try include $(call all-subdir-makefiles) rather then $(LOCAL_PATH) :=$(call all-makefiles-under) in main Android.mk and make folder under jni folder with there separate c files and Android.mk files
Have you tried something like:
LOCAL_PATH := $(call my-dir)
LOCAL_SRC_FILES := /submodule1/sub1.c