{"id":1428,"date":"2012-10-05T13:34:00","date_gmt":"2012-10-05T11:34:00","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=1428"},"modified":"2012-10-05T13:34:00","modified_gmt":"2012-10-05T11:34:00","slug":"building-android-cocos2d-x-ndk-jdk-step","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2012\/10\/building-android-cocos2d-x-ndk-jdk-step\/","title":{"rendered":"Building for Android with Cocos2d-X (NDK+JDK) in one step"},"content":{"rendered":"<h3>One Step to Better Code<\/h3>\n<p>Recently, I&#8217;ve read once again the old but awesome article &#8220;<a href=\"http:\/\/www.joelonsoftware.com\/articles\/fog0000000043.html\">Joel Test : 12 steps to better code<\/a>&#8221; (do it now if you haven&#8217;t). When developing <a href=\"http:\/\/www.frozax.com\/dftt\">Don&#8217;t Feed the Trolls<\/a>, I always had a few steps to build a new android version of the game. I made a few scripts but I still had multiple steps. My workflow was:<\/p>\n<ol>\n<li>Copy the resources in the android assets folder<\/li>\n<li>Compile the C++ code with Gygwin<\/li>\n<li>Launch eclipse<\/li>\n<li>Use option Android \/ Build Signed Package in Eclipse<\/li>\n<li>Type two passwords for signature<\/li>\n<\/ol>\n<p>I recently improved this workflow, and now it is:<\/p>\n<ol>\n<li>Double click the &#8220;build_android.bat&#8221; file<\/li>\n<\/ol>\n<p>Much better!<br \/>\nHere are details of the script. <\/p>\n<h3>1. Copying the resources<\/h3>\n<p>It&#8217;s simple batch script that copies the resources from the multiplatform &#8220;Resource&#8221; folder (used in cocos2d-x templates) to the assets folder used on Android.<\/p>\n<pre><code class=\"cpp\">rmdir \/S \/Q \"D:gamedevGamesswg21Devandroidassets\"\nmkdir \"D:gamedevGamesswg21Devandroidassets\"\nxcopy \"D:gamedevGamesswg21DevResource\" \"D:gamedevGamesswg21Devandroidassets\" \/S \/H\ndel \"D:gamedevGamesswg21Devandroidassetssnd*.wav\"\n<\/code><\/pre>\n<p>I first delete the existing files. Then I create the directory and copy all my resources. Finally, I remove wav files, as these are used only on the windows version. My multiplatform Resource folder contains sound in formats for all platforms.<\/p>\n<h3>2. Compiling the C++ source code<\/h3>\n<p>The native C++ code is compiled in cygwin. I spent a lot of time trying to figure out how to run a script from a batch and having it return immediately (with an error code). For the record, I was missing the &#8211;login parameter to the bash command. Here is the command I use:<\/p>\n<pre><code class=\"cpp\">cd \"d:gamedevcygwinbin\"\nbash --login \".\/swg21.sh\"\n<\/code><\/pre>\n<p>The swg21.sh is a bash script placed in my home folder, calling cocos2d-x build_native script.<\/p>\n<pre><code class=\"cpp\">cd \/cygdrive\/d\/gamedev\/games\/swg21\/dev\/android\n.\/build_native.sh\n<\/code><\/pre>\n<p>build_native.sh is a bash script calling ndk-build after setting up proper environment variables.<\/p>\n<h3>3. Compiling the java and creating the signed apk<\/h3>\n<p>This was the main step I wanted to optimize from my previous workflow. Running Eclipse just to build a version was really slowing down the whole process. For that part, you have to use ant, as explained in the <a href=\"http:\/\/developer.android.com\/tools\/building\/building-cmdline.html\">Google documentation<\/a>.<\/p>\n<pre><code class=\"cpp\">cd \"D:gamedevGamesswg21Devandroid\"\ncall ant release\n<\/code><\/pre>\n<p>The problem was that I was still asked to enter my password to sign the apk. This is fixed by editing the ant.properties file, as shown below (Credit goes to <a href=\"http:\/\/stackoverflow.com\/questions\/9932498\/signing-applications-automatically-with-password-in-ant\">NickT from stackoverflow<\/a>):<\/p>\n<pre><code class=\"cpp\">key.store.password=password\nkey.alias.password=password\nkey.store=path\/to\/your.keystore\nkey.alias=alias\n<\/code><\/pre>\n<h3>4. Dropbox<\/h3>\n<p>I use dropbox to transfer apk to my Android device. Therefore, I need to copy the file to my dropbox folder.<\/p>\n<pre><code class=\"cpp\">copy \"D:gamedevGamesswg21Devandroidbinswg21-release.apk\" \"C:UsersWin7Dropboxtempswg21.apk\"<\/code><\/pre>\n<h3>5. Error checking<\/h3>\n<p>Steps 2 and 3 can fail (compile errors). I added some batch script to change the screen color to green when it&#8217;s successful, and red if not. Here is the final build_android.bat with error management.<\/p>\n<pre><code class=\"cpp\">@echo off\n@echo \"============================================ COPY RESSOURCES\"\nrmdir \/S \/Q \"D:gamedevGamesswg21Devandroidassets\"\nmkdir \"D:gamedevGamesswg21Devandroidassets\"\nxcopy \"D:gamedevGamesswg21DevResource\" \"D:gamedevGamesswg21Devandroidassets\" \/S \/H\ndel \"D:gamedevGamesswg21Devandroidassetssnd*.wav\"\n\n@echo \"============================================ COMPILE NDK\"\ncd \"d:gamedevcygwinbin\"\nbash --login \".\/swg21.sh\"\nIF %ERRORLEVEL% NEQ 0 GOTO FAIL\nGOTO SUCCESS\n:FAIL\nCOLOR 40\nGOTO END\npause\n:SUCCESS\n\n@echo \"============================================ COMPILE JDK+Sign\"\ncd \"D:gamedevGamesswg21Devandroid\"\ncall ant release\nIF %ERRORLEVEL% NEQ 0 GOTO FAIL\n\n@echo \"============================================ COPY FILE TO DROPBOX\"\ncopy \"D:gamedevGamesswg21Devandroidbinswg21-release.apk\" \"C:UsersWin7Dropboxtempswg21.apk\"\nCOLOR 20\n:END\npause\n<\/code><\/pre>\n<h3>Improvements and Conclusion<\/h3>\n<p>I&#8217;m glad I did this script and don&#8217;t understand why I didn&#8217;t do it earlier. I now build a new Android apk almost every night.<br \/>\nIf you&#8217;re working on a team, you might also add source control scripts to get the latest version of your code\/resources automatically. Also, my projects path are hardcoded, I should set &#8220;swg21&#8221; (the project name) in a command line parameter and use relative paths. I&#8217;ll probably do it when I&#8217;ll use this script for a second project.<br \/>\nI&#8217;ll also do the same thing for iOS when my current project will be closer to release (I&#8217;ve read it&#8217;s not that trivial to do, we&#8217;ll see).<br \/>\nFeel free to share and comment this article here or on <a href=\"http:\/\/twitter.com\/Frozax\">twitter<\/a>.<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2012%2F10%2Fbuilding-android-cocos2d-x-ndk-jdk-step%2F&amp;linkname=Building%20for%20Android%20with%20Cocos2d-X%20%28NDK%2BJDK%29%20in%20one%20step\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2012%2F10%2Fbuilding-android-cocos2d-x-ndk-jdk-step%2F&amp;linkname=Building%20for%20Android%20with%20Cocos2d-X%20%28NDK%2BJDK%29%20in%20one%20step\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2012%2F10%2Fbuilding-android-cocos2d-x-ndk-jdk-step%2F&amp;linkname=Building%20for%20Android%20with%20Cocos2d-X%20%28NDK%2BJDK%29%20in%20one%20step\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2012%2F10%2Fbuilding-android-cocos2d-x-ndk-jdk-step%2F&amp;linkname=Building%20for%20Android%20with%20Cocos2d-X%20%28NDK%2BJDK%29%20in%20one%20step\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>One Step to Better Code Recently, I&#8217;ve read once again the old but awesome article &#8220;Joel Test : 12 steps to better code&#8221; (do it now if you haven&#8217;t). When developing Don&#8217;t Feed the Trolls, I always had a few steps to build a new android version of the game. I made a few scripts&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2012\/10\/building-android-cocos2d-x-ndk-jdk-step\/\">Continue reading <span class=\"screen-reader-text\">Building for Android with Cocos2d-X (NDK+JDK) in one step<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,12],"tags":[55,65,97],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/1428"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=1428"}],"version-history":[{"count":0,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/1428\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}