compiling_for_android.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. .. _doc_compiling_for_android:
  2. Compiling for Android
  3. =====================
  4. .. highlight:: shell
  5. Note
  6. ----
  7. For most cases, using the built-in deployer and export templates is good
  8. enough. Compiling the Android APK manually is mostly useful for custom
  9. builds or custom packages for the deployer.
  10. Also, you still need to do all the steps mentioned in the
  11. :ref:`doc_exporting_for_android` tutorial before attempting your custom
  12. export template.
  13. Requirements
  14. ------------
  15. For compiling under Windows, Linux or OSX, the following is required:
  16. - Python 2.7+ (3.0 is untested as of now).
  17. - SCons build system.
  18. - Android SDK version 19 [Note: Please install all Tools and Extras of sdk manager]
  19. - Android build tools version 19.1
  20. - Android NDK
  21. - Gradle
  22. - OpenJDK 6 or later (or Oracle JDK 6 or later)
  23. Setting up SCons
  24. ----------------
  25. Set the environment variable ANDROID_HOME to point to the Android
  26. SDK.
  27. Set the environment variable ANDROID_NDK_ROOT to point to the
  28. Android NDK.
  29. To set those environment variables on Windows, press Windows+R, type
  30. "control system", then click on **Advanced system settings** in the left
  31. pane, then click on **Environment variables** on the window that
  32. appears.
  33. To set those environment variables on Linux, use
  34. ``export ANDROID_HOME=/path/to/android-sdk`` and
  35. ``export ANDROID_NDK_ROOT=/path/to/android-ndk``.
  36. Compiling
  37. ---------
  38. Go to the root dir of the engine source code and type:
  39. ::
  40. C:\godot> scons platform=android
  41. This should result in a regular .so in ``\bin`` folder as if it was
  42. compiled with flags: ``tools=no target=debug``. The resulting file will
  43. be huge because it will contain all debug symbols, so for next builds,
  44. using ``target=release_debug`` or ``target=release`` is recommended.
  45. Copy the .so to the ``libs/armeabi`` Android folder (or symlink if you are
  46. in Linux or OSX). Note: Git does not support empty directories so you
  47. will have to create it if it does not exist:
  48. ::
  49. C:\godot> mkdir platform/android/java/libs
  50. C:\godot> mkdir platform/android/java/libs/armeabi
  51. Then copy:
  52. ::
  53. C:\godot> copy bin/libgodot.android.<version>.so platform/android/java/libs/armeabi/libgodot_android.so
  54. Or alternatively, if you are under a Unix system you can symlink:
  55. ::
  56. user@host:~/godot$ ln -s bin/libgodot.android.<version>.so platform/android/java/libs/armeabi/libgodot_android.so
  57. Remember that only *one* of libgodot_android.so must exist for each
  58. platform, for each build type (release, debug, etc), it must be
  59. replaced.
  60. **Note**: The file inside ``libs/armeabi`` must be renamed to
  61. **"libgodot_android.so"**, or else unsatisfied link error will happen
  62. at runtime.
  63. If you also want to include support for x86 Android, add the following
  64. compile flag: ``android_arch=x86``, then copy/symlink the resulting binary to
  65. the ``x86`` folder:
  66. ::
  67. C:\godot> copy bin/libgodot.android.<version>.x86.so platform/android/java/libs/x86/libgodot_android.so
  68. This will create a fat binary that works in both platforms, but will add
  69. about 6 megabytes to the APK.
  70. Toolchain
  71. ---------
  72. We usually try to keep the Godot Android build code up to date, but
  73. Google changes their toolchain versions very often, so if compilation
  74. fails due to wrong toolchain version, go to your NDK directory and check
  75. the current number, then set the following environment variable:
  76. ::
  77. NDK_TARGET (by default set to "arm-linux-androideabi-4.9")
  78. Building the APK
  79. ----------------
  80. To compile the APK, go to the Java folder and run ``gradlew.bat build``
  81. (or ``./gradlew build`` on Unix):
  82. ::
  83. C:\godot\platform\android\java> gradlew.bat build
  84. In the ``java/bin`` subfolder, the resulting apk can be used as export
  85. template.
  86. **Note:** If you reaaaally feel oldschool, you can copy your entire game
  87. (or symlink) to the assets/ folder of the Java project (make sure
  88. engine.cfg is in assets/) and it will work, but you lose all the
  89. benefits of the export system (scripts are not byte-compiled, textures
  90. not converted to Android compression, etc. so it's not a good idea).
  91. Compiling export templates
  92. --------------------------
  93. Godot needs the freshly compiled APK as export templates. It opens the
  94. APK, changes a few things inside, adds your file and spits it back. It's
  95. really handy! (and required some reverse engineering of the format).
  96. Compiling the standard export templates is done by calling scons with
  97. the following arguments:
  98. - (debug)
  99. ::
  100. C:\godot> scons platform=android target=release_debug
  101. C:\godot> cp bin/libgodot_android.opt.debug.so platform/android/java/libs/armeabi/libgodot_android.so
  102. C:\godot> cd platform/android/java
  103. C:\godot\platform\android\java> gradlew.bat build
  104. Resulting APK is in:
  105. ::
  106. platform/android/java/bin/Godot-release-unsigned.apk
  107. - (release)
  108. ::
  109. C:\godot> scons platform=android target=release
  110. C:\godot> cp bin/libgodot_android.opt.so platform/android/java/libs/armeabi/libgodot_android.so
  111. C:\godot> cd platform/android/java
  112. C:\godot\platform\android\java> gradlew.bat build
  113. Resulting APK is in:
  114. ::
  115. platform/android/java/bin/Godot-release-unsigned.apk
  116. (same as before)
  117. They must be copied to your templates folder with the following names:
  118. ::
  119. android_debug.apk
  120. android_release.apk
  121. However, if you are writing your custom modules or custom C++ code, you
  122. might instead want to configure your APKs as custom export templates
  123. here:
  124. .. image:: /img/andtemplates.png
  125. You don't even need to copy them, you can just reference the resulting
  126. file in the ``bin\`` directory of your Godot source folder, so the next
  127. time you build you automatically have the custom templates referenced.
  128. Troubleshooting
  129. ---------------
  130. Application not installed
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~
  132. Android might complain the application is not correctly installed. If
  133. so, check the following:
  134. - Check that the debug keystore is properly generated.
  135. - Check that jarsigner is from JDK6.
  136. If it still fails, open a command line and run logcat:
  137. ::
  138. C:\android-sdk\platform-tools> adb logcat
  139. And check the output while the application is installed. Reason for
  140. failure should be presented there.
  141. Seek assistance if you can't figure it out.
  142. Application exits immediately
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. If the application runs but exits immediately, there might be one of the
  145. following reasons:
  146. - libgodot_android.so is not in ``libs/armeabi``
  147. - Device does not support armv7 (try compiling yourself for armv6)
  148. - Device is Intel, and apk is compiled for ARM.
  149. In any case, ``adb logcat`` should also show the cause of the error.