//////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////// COMPILE NATIVE APPLICATIONS FOR ANDROID ////////////////////////////////// // // // Author : brainsandwich // // History : // // - 26/03/2015 : // // [+] append to old information // // - 27/03/2015 : // // [+] project prerequisistes // // [-] old information // // - 22/04/2015: // // [+] TADP info // // // //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// content I. General Prerequisites .. I.1 JDK - Java SE Development Kit .. I.2. Android SDK - Software Development Kit .. I.3. Android NDK - Native Development Kit .. I.4. Apache ANT - Another Neat Tool .. I.5. CMake - CMake-gui .. I.6. Add *some* variables to your environment .. I.7. Then, append the previous variables, with "extensions", to the PATH variable .. I.8. Enabling HAXM - Hardware Accelerated Execution Manager II. Project Prerequisites .. II.1. Boost .... II.1.a Compiling Boost .... II.1.b Compiling something depending on Boost with CMake .. II.1. Ogre .... II.1.a Prepare dependencies .... II.1.b Compile Ogre III. Compilation .. III.1. Compile with CMake .... III.1.a *NEW* The real way applications should be made to be used with cmake .. III.2. Compile a Native Android application "manually" .... III.2.a Preparation .... III.2.b Compilation .... III.2.c Get some information about execution .. III.3. You can develop Native applications for Android with some IDEs .... III.3.a Android Studio .... III.3.b Eclipse + ADT .... III.3.c Visual Studio + vs-android IV. Execution V. Native Applications for Android //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// body O. If you're lazy and want to debug C/C++ code running on Android //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// . If you don't have Visual Studio, buy it or download the Community Edition (free as long as you don't sell your work and provided you have a Microsoft account -- free too). . Download the TADP -- Tegra Android Development Pack (see III.3.d) @ https://developer.nvidia.com/nvidia-nsight-tegra . GOTO I.5 for CMake and II. because all other requirements are fulfilled. I. General Prerequisites : //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// First step towards embedding C/C++ applications onto an Android device is to get the SDKs. You'll need : I.1. JDK - Java SE Development Kit (8u40) : -------------------------------------------- -------------------------------------------- ** Development kit for Java applications, ie allows you to develop java apps. Goto : - http://www.oracle.com/technetwork/java/javase/downloads/ Then go to page to download JDK according to your OS and proc architecture. For any reason, check if you have the JRE (Java Runtime Environment) installed on your computer. It could matter. Actually, 99% of the programs to download here should be shipping it but check again to avoid issues. I.2. Android SDK - Software Development Kit (API 22) : ------------------------------------------------------- ------------------------------------------------------- The SDK is updated super often. In order to stay up-to-date, little Google elves prepared a SDK-Manager providing download links to every Android SDK that has ever existed, tools, documentations and emulator images. It is bundled in the Android Studio software, but you can get it separately -- it won't change anything getting it with Android Studio : - http://developer.android.com/sdk The AVD (Android Virtual Device) Manager should also be shipped with the SDK, but check that to be sure. It should be located at the root of the sdk folder. I don't recall a way to get it manually so if it's not there, download Android Studio. I.3. Android NDK - Native Development Kit (r10d) : --------------------------------------------------- --------------------------------------------------- Changing quite often too, but less. There's no magic downloader for it, so go to : - http://developer.android.com/tools/sdk/ndk and get the latest version of the NDK. There's only the latest there. If somehow you feel you need a older version, you can find one at : - https://github.com/taka-no-me/android-cmake/blob/master/ndk_links.md I.4. Apache ANT - Another Neat Tool (1.9.4) : ---------------------------------------------- ---------------------------------------------- - http://ant.apache.org/ I.5. CMake - CMake-gui on win$ (3.1.3) : ----------------------------------------- ----------------------------------------- Goto CMake webpage : - http://www.cmake.org I.6. Add *some* variables to your environment : ------------------------------------------------ ------------------------------------------------ - ANDROID_HOME : path/to/sdk - ANDROID_SDK_HOME : path/to/sdk - ANDROID_SDK : path/to/sdk -- yes, put all three of them :| - ANDROID_NDK : path/to/ndk - ANT_HOME : path/to/ant - JAVA_HOME : path/to/jdk -- should be "C:\Progra~1\Java\" -- note the ~1 because of spaces in path - CMAKE : path/to/cmake -- for convinience only (run from cmd) I.7. Then, append the previous variables, with "extensions", to the PATH variable : ------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------ %ANDROID_NDK%; %ANT_HOME%\bin; %ANDROID_SDK%\tools; %ANDROID_SDK%\platform-tools; %CMAKE%; %JAVA_HOME%\bin; Your Android Native development should now be operational. If you're lucky (have intel processor with some recent virtualization technology), you should enable HAXM for android device emulation. It boosts performances of x86 Android emulated devices (it's a whole lot faster). And you can't run x86 emulators without it. I.8. Enabling HAXM - Hardware Accelerated Execution Manager : -------------------------------------------------------------- -------------------------------------------------------------- Get into the SDK Manager and to the very bottom of the list of downloadable content. You should see "Intel HAXM driver" or something similar, related to HAXM. It doesn't install HAXM, only downloads the installer. So you have to go to : /extras/intel/Hardware_Accelerated_Execution_Manager/ and then try to install with the provided installer. If your processor is not compatible, you will be noticed. The needed virtualization has to be enabled in your BIOS. It should be called "intel VT" / "intel VT-D". Look for something with VT / virtualization + intel in the name. II. Project Prerequisites : //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// Running a Scol virtual machine on an Android device is considered experimental for the moment (27/03/2015). However, to make a good start, doing the following is required. II.1. Boost (1.57.0) : ----------------------- ----------------------- Boost is required by many dependencies and the scol machine. It adds support for threading, date & time, regex, filesystem operations and some other modern C++ stuff. II.1.a Compiling Boost (1.57.0) : ---------------------------------- There are patches for Boost to make it run on Android and the Crystax NDK modified NDK) ships Boost, however, it is possible to compile any* version of Boost manually. In the Dependencies/Boost folder, a .bat script is provided to compile Boost for ARM and ARM-V7 Android targets. To do the same with an other version of Boost, download the sources at http://www.boost.org/, then copy the .bat file at the root of the Boost folder and run it. If you're on Linux, you can try making a shell script the same way the batch has been made :) * works for version 1.57.0, you should consider using at least this version if you want things to work properly, nothing has been tested. However there's no reason it shouldn't compile. II.1.b Compiling something depending on Boost with CMake : ----------------------------------------------------------- To compile something for Android, please first refer to 3.a (Workflow."Compile with CMake") It's rather tricky for the moment. It's not automated so when you want to compile you will have to add some information in the CMake-gui configuration : . Configure the project once. . When it's done, go to the Boost section and change Boost_INCLUDE_DIR from "NOT_FOUND" to the Boost root dir (for this project it's /dependencies/boost) and configure the project again. . Now setup Boost librairies : Boost_DATE_TIME_LIBRARY -> "/dependencies/boost/stage/lib//libboost_date_time-gcc-mt-s-1_57.a" Same for all librairies. . Configure once again. Your CMake project should have new variables concerning Boost integration. Configure again and generate. II.2. Ogre (1.9) : ------------------- ------------------- The Ogre library can be built quite easily now but you still need to do some manual work on the CMake configuration before getting things working properly. II.2.a Prepare dependencies : ---------------------------- The newest dependencies can be found at "https://bitbucket.org/cabalistic/ogredeps/src/a9a856daa857?at=default" (look for "cabalistic ogredeps" if the link doesn't work anymore). You need to compile Ogre dependencies first. LibRaw has been removed from FreeImage, because it was causing issues at compilation & link. It's not used in Ogre so there shouldn't be any problem on this side. . Compile the dependencies in the Ogre/Sources/Dependencies folder to "Ogre/Sources/Dependencies/BuildAndroid". The CMake Install should place the output libraries in "/toolchains//prebuilt/windows-x86_64/user". It's recommended to do it like so. Don't forget to use the toolchain provided : "Ogre/Sources/Dependencies/cmake/android.toolchain.cmake" . Once the dependencies' makefiles are configured and generated, go to the BuildAndroid folder and build with "nmake INSTALL". Nmake itself will only compile the libraries but INSTALL will place them into the right folder. . When everything is done for the deps, rename the CMakeFiles.txt to something else (e.g. "CMakeLists.txt_") so the Ogre CMake won't try to recompile them. II.2.b Compile Ogre : -------------------- . As for the dependencies, use "Ogre/Sources/cmake/android.toolchain.cmake" as toolchain. . Compile the library from Ogre/Sources to Ogre/BuildAndroid. Adjust Android sdk version to something > 15 (17 is used generally for the whole project). . Do as mentionned for Boost compilation. . Do a static compilation (Ogre -> static) III. Compilation : //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// III.1. Compile with CMake : ---------------------------- ---------------------------- In the scol project, all the compilation processes are managed with CMake. This allows convinient compilation from almost any OS targeting any specific parameter. The scol project is almost completely prepared to cross compile to Android targets, but, for information, here are the things to prepare to start compiling : . Get the android.toolchain.cmake at : https://github.com/taka-no-me/android-cmake The toolchain will be used in cooperation with NMake (on windows) to compile a project. . The AndroidNdkModules.cmake is necessary too. The toolchain has to be modified a bit to incorporate the modules. GDB modules can be added too. Add (android.toolchain.cmake : ~lines 204 - 205): include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/toolchain/AndroidNdkModules.cmake") include( "${CMAKE_CURRENT_SOURCE_DIR}/CMake/toolchain/AndroidNdkGdb.cmake") Add (android.toolchain.cmake : ~line 1625): android_ndk_import_module_ndk_helper() Then you can configure your project with cmake -- or cmake-gui. After first configuration, some new vars should have appeared. In the Android section, choose the version of Android you target (recommended is > 15 for this project) and the ABI (arm / arm-v7 / x86 / same with 64bit proc). III.1.a. *NEW* The real way applications should be made to be used with cmake : ------------------------------------------------------------------------------ The order of build should follow this one : -> Compile "core" functions -> Compile needed plugins -> Compile a Dummy library depending on all the above, and needed by all the following -> Generate a NDK project -> Push the .apk on your device. As you can see by compiling the Scol project, there's now almost no configuration to add to make the project work. The plugins are compiled and added to the final apk smoothly. The dummy library is here to force the wanted generator to compile your libraries first and then produce the Android stuff. Indeed, the NDK builder (ndk-build) is not used here to effectively compile files, but just to fetch libraries to put in the final apk. The AndroidMacros.cmake is dependent on the project we've made, but there's not much stuff to change to get something customized for your needs. It generates configurations, manifests, makefiles and hierarchy automagically to get a "standard" android project. It also adds custom build commands used while compiling the target. These commands are described in [3.b.2] on "manual" compilation. III.2. Compile a Native Android application "manually" : --------------------------------------------------------- --------------------------------------------------------- . First thing you should do is launching an emulator or connecting an Android device on a USB port. The building system always tries to install the application on the device (virtual or not) at the end of the process, which allows you to quickly check if the app works or not. . The tools used here are ndk-build, ant and the android utility. You can use these tools from any command line. III.2.a Preparation : ------------------- In order to be considered a NDK project, your hierarchy should follow this one : / |- jni |- Application.mk -- defines c files & librairies to be used |- Android.mk -- defines ABI and some general stuff |- src |- -- not necessary; contains java classes (MainActivity.java) |- res -- ressources to be shipped in the .apk |- values |- string.xml -- localization values (required :/) |- AndroidManifest.xml -- manifest defining properties of the application -- similar to conventional java manifests but it's for Android You should check NDK samples to follow the same structure. III.2.b Compilation : --------------------- When you have your project filled up, you can start compiling. In a terminal : > android update project -p . -t android-$N [create build.xml for this project, for Ant] ---------------------- ---- ------------- ^ /tools/android.bat ^ specify target android ($N : version of android, e.g. 22) ^ specify project path ("." = this folder) > ndk-build [build native .c / .cpp files for arm android] --------- ^ /ndk-build.cmd > ant debug install [builds java project and installs it on the device] --- ----- ------- ^ /bin/ant.bat ^ debug / release ^ install on connected device -- you can skip this to just contruct the .apk [optional] > adb install bin/.apk [just installs the apk on connected device] --- ^ /platform-tools/adb.exe Hurray, your application is installed on your device \o/ To test it, navigate to the main menu and launch the newly installed app. III.2.c Get some information about execution : -------------------------------------------- There's 2 tools to obtain debug information about the applications currently running on your device : . Android Device Monitor : /tools/monitor.bat Gives you plenty of information about everything running on the device (threads, memory, filesystem ; you can send events like sms/mms ...) . ndk-gdb : III.3. You can develop Native applications for Android with some IDEs ---------------------------------------------------------------------- ---------------------------------------------------------------------- Instead of relying on CMake, you can develop native applications with some IDEs. There's 3 working / tested environments : . Android Studio : official Google IDE . Eclipse + ADT : old official Google IDE . Visual Studio + vs-android : unofficial . Visual Studio + Nvidia TADP : complete solution Microsoft is integrating Android development in Visual Studio 2015 III.3.& Android Studio : ---------------------- Favorite solution of Google developpers. It's been made on top of the Intelij'Idea IDE. It features access to SDK & AVD manager, the Android Monitor, and easy organisation of a Java + Native project. The default build tool is gradle, which doesn't support NDK yet. However there are some tricks to use it anyway, and others to use NDK build tools directly. III.3.b Eclipse + ADT : ----------------------- Recently outdated way to make native android applications. III.3.c Visual Studio + vs-android : ------------------------------------ It does some stuff but it's not extra cool. You can compile applications but there's not much intensive support. III.3.d Visual Studio + Nvidia TADP -- Tegra Android Development Kit : ------------------------------------- Since the Tegra boards arrived, Nvidia made much efforts to support Native development on Android (arm) devices. It integrates almost all of the requirements of Android Native development out of the box. . TADP : The package contains a lot of stuff needed to develop for Android on Windows. It includes the Android NDK, the Android SDK, the JDK and other librairies like OpenCV. . Nsight Visual Studio for Tegra : It's a plugin for Visual Studio to build projects for Android platforms. There is some samples in the Nsight folder to show what's it capable of. To build Scol with NSight, you have to follow quite the same process as the one described earlier with CMake. So, first, launch CMake and prepare to configure Scol. When you start configuring, set the target to Visual Studio (2010 here) and use a toolchain. Use the android.nsight.toolchain.cmake in scol/CMake/Toolchain folder. When it's configured, open the project Scol.sln in the output folder (build folder) and build the project (INSTALL project). IV. Execution : //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// Now you've managed to build something, it's time to try it. You can try on a real or virtual device. IV.1. AVD Manager -- Android Virtual Device Manager : ----------------------------------------------------- ----------------------------------------------------- The AVD is bundled with the Android SDK, so you're bound to have it somewhere if you followed this manual in the correct order. If you're using the TADP it should be here too. You can create Virtual Devices with multiple versions of Android, depending on which ones you got from the SDK Manager. It can be found in /"AVD Manager.exe". You should launch one before everything related to Android development and fancy at your brand new Nexus 6 on Android 5.1 ! IV.2. Monitor.bat : ------------------- ------------------- The monitor monitors (lel) all activities on connected devices (real/virtual). It provides information about the files in the device and logs of running applications. IV.3. Debugging : ----------------- ----------------- There's currently 2 options for debugging a program : Nsight debugger and NDK-GDB. For any project, the AndroidManifest.xml must contain a 'android:debuggable="true"' in the "application" markup, and the ndk-build tool must be used with the option "NDK_DEBUG=1". IV.3.a. Nsight debugger : ------------------------- This debugger should be included into Visual Studio if you installed the TADP. For the Scol project, build the project, select the ScolLauncher as "Startup Project" and start your debug session with the "Nsight Tegra Debugger" command. Just make sure that you selected "Make Application (ndk-build -> .apk|.so|.a)" in ScolLauncher -> Properties -> Configuration Properties -> General -> Configuration Type. IV.3.b. NDK-GDB -- Gnu Project Debugger : ----------------------------------------- First, make sure you built your android project (either after the Scol nmake or manual build), and installed it onto your device (nmake does it, use ant debug install otherwise). On Windows platform, ndk-gdb is made with Python, so install it if it's not on your computer (https://www.python.org/). Now in a terminal, go to the NDK project (one with AndroidManifest.xml and a jni folder) and run ndk-gdb(-py on windows) --start --force --verbose It should be enough. Though I never managed to use it (it always tells that it couldn't connect to device). V. Native Applications for Android : //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// the end