Android.mk

I have to say that I loved the new script that automates creating a cocos2d-x project for all platforms, and ready to use out of the box!

I am still finding the need to update the android.mk file manually to include all the classes in the Classes folder a bit irritating. Isn’t there an automation coming up for this?

And, also, in iOS I can easily make folders in the Classes directory, then #include them in any source file as follows:

In Classes/dir1/class1.cpp
#include "class2.h"

But, in android, I have to explicitly do:
#include "../dir2/class2.h"

Can this issue be fixed, too?

Thanks!

Anyone?

As far as the #include’s go I think it won’t be possible (or will be extremely hard) to do.
When you add class files in XCode it stores their paths in its project file, so in the end it automatically provides the compiler and linker with necessary information “behind your back”.
With Android you have to give relative paths, because the compiler there is not integrated with the IDE, so it doesn’t know where to look for included files.

As to the Android.mk autmation, I just had an idea I want to try, and if successful I will post it here.

Well, I’ve tried, but couldn’t do it…
I tried writing a script that would automatically generate the Android.mk file, and I got this part, but for unknown reasons to me it broke my project - to be specific, when I introduced the automatically generated file to my project and tried to run it, I suddenly said that the file “jni/hellocpp/main.cpp” cannot find “#include”AppDelegate.h“”. I haven’t changed anything in this project, and I checked before introducing the generated file that it built & run correctly.

I have also found this post on this forum : http://www.cocos2d-x.org/boards/6/topics/5321

And it seems that when I introduce their solutions it also generates the AppDelegate error…

If the devs could comment on probable cause it would be great.

Holly Bits of Bytes! I’ve made a typo :smiley:

It works, and is real simple :smiley: Here it is :

///mkHelper.sh
#!/bin/sh

echo 'LOCAL_PATH := $(call my-dir)'
echo ""
echo 'include $(CLEAR_VARS)'
echo ""
echo 'LOCAL_MODULE := game_shared'
echo""
echo 'LOCAL_MODULE_FILENAME := libgame'
echo ""

echo "LOCAL_SRC_FILES := hellocpp/main.cpp \\"

while read data; do
    echo "                   ../$data \\"
done

echo ""
echo 'LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes'
echo""
echo 'LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static'
echo""
echo 'include $(BUILD_SHARED_LIBRARY)'
echo ""
echo '$(call import-module,CocosDenshion/android) \'
echo '$(call import-module,cocos2dx) \'
echo '$(call import-module,extensions)'

to make it work you also have to add this line at the beggining of your build_native.sh file :

find -L ../Classes -name \*.cpp -print | ./mkHelper.sh > jni/Android.mk

And then everytime you hit “Build” in Eclipse it will update your class list.

Hope you enjoy!

:smiley: :smiley: Awesome stuff, man! :smiley:
This looks really clean and simple, and easy to incorporate!

I have to mention there was an earlier attempt to automate the process, but it wasn’t perfect, cause it didn’t allow for subfolders to exist within the Classes directory.
You can fin it here:
http://www.cocos2d-x.org/boards/6/topics/16846

Well, whad’ya know, it works perfectly!

I have very few notes for other developers looking to use this in their project:

1- create the mkHelper.sh in your proj.android folder, with your build_native.sh.
2- make sure you chmod u+x mkHelper.sh to avoid sudo’ing the ./build_native.sh
3- make sure you update the mkHelper.sh script to whatever your Android.mk file contains initially. So, in my case, I had to add the chipmunk, box2d, and libcurl imports.

but, that’s it! It works like a charm!

Something broke in the script for some reason, and the game builds successfully, but started crashing on launch with the following error:
@
AndroidRuntime The import org.cocos2dx cannot be resolved
@

The solution was, to replace:
@
echo ‘LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static chipmunk_static box2d_static’
@

with:
@
echo ‘LOCAL_WHOLE_STATIC_LIBRARIES = cocos2dx_static’
echo ’LOCAL_WHOLE_STATIC_LIBRARIES
= cocosdenshion_static’
echo ‘LOCAL_WHOLE_STATIC_LIBRARIES = box2d_static’
echo ’LOCAL_WHOLE_STATIC_LIBRARIES
= chipmunk_static’
echo ‘LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static’
@

Well, I wouldn’t be so sure, as if this was actually a solution to this, as this error seems to be java-based. Have you restarted Eclipsed when figuring this out?
Maybe it just got stuck during some maintenance-ish task while you tried to run your project and simply couldn’t access the cocos library.

I don’t use eclipse at all. All my development is in Xcode, and then deploy to android through terminal.

With that said, I have uninstalled the app, restarted everything without any progress, until I changed the lines above. I am using v2.1.2.

One way to tell for sure! echo LOCAL_WHOLE_STATIC_LIBRARIES.

Why not just replace := with +=?

Simple! Because I have no idea which is better, I just grabbed that syntax from my old Android.mk file.

Sergey : I don't think it would make much difference, just a few iterations more for the parser.Mazyod : Well, I’m glad it worked for you.

Thanks so much! This is a huge help. :smiley:

I found out the hard way that modifying Android.mk forces rebuild of all sources. To avoid unnecessary Android.mk modifications from your otherwise fantastic mkHelper script, I do a simple check:

mkHelperBuilder.sh

#!/bin/bash

if diff <(find -L ../Classes -name \*.cpp -print | ./mkHelper.sh) <(cat jni/Android.mk); then
  echo "Generated Android.mk the same as previous, not updating."
else
  echo "Generated Android.mk different than previous, updating."
  find -L ../Classes -name \*.cpp -print | ./mkHelper.sh > jni/Android.mk
fi