Skip to content
brlbr edited this page Mar 27, 2022 · 17 revisions

Installation

  • Download latest Android Studio.
  • Install it (Android SDK and latest SDK Platform / Build Tools will be installed automatically). You can skip emulator virtual device download in custom mode installation if you only want to test on real device.
  • After (or before) installation finished, build your project to android-native in Kode Studio, other IDE or with node Kha/make android-native.
  • Now show Android Studio back, click Open project and open MyProject/build/android-native-build/ProjectName/ folder in it. Android Studio will start downloading Gradle. Wait for completion.
  • Because Kha generates Android Studio projects with specific android sdk version (older than latest), you can see error like ERROR: Failed to install the following Android SDK packages in bottom-right side of window: [screenshot] In this case just click to link Install missing SDK package(s) and accept licenses to install previous SDK version. You can safely uninstall them in Android Studio after Kha will update SDK version number in project generator. Same for Gradle and other stuff versions.
  • Now starts project configuration. It lasts about 5 minutes, because Android Studio start downloading NDK bundle (~3 GB) to SDK folder. If you see error about NDK, open Preferences > Appearance & Behavior > System Settings > Android SDK, SDK Tools tab and install NDK or NDK (Side by side).
  • After configuration complete, open Build Variants tab (bottom-left screen side) and select variant you need. Almost every android device supports armeabi-v7a (arm7), newest androids also support arm64-v8a (arm8), so common choice is debug/release armeabi-v7a.
  • Now you should see active buttons for Run/Build/Profile/etc on IDE toolbar. You can press Run and start building for emulator, or connect real device with USB, allow debug window on your Android device, and build for it.

Troubleshooting:

  • If you see INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES error, check that you don't have apk with same package name (default is tech.kode.kore) and different signing certificate on device.

After you tested that build works, you can continue using Android Studio for building (now it will be enough to open generated project after same Kha/make command and click Run) with debugger and other tools support, or start using Kha console interface for faster apk testing.

CLI setup

First of all, i want to say that you in theory can setup CLI without Android Studio installation at all (downloading Command line tools only from Android Studio page), but it can be more painful to reproduce all steps before in this manual. So, lets continue this part after you do all Android Studio setup steps before.

Define environment variables for Windows or for Unix:

export ANDROID_HOME=/YourPathToAndroid/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH

Run and debug

Now you can use adb and other android tools. Connect your mobile device or emulator and run:

node Kha/make android-native --compile --debug --arch arm7
# change __PROJECT_NAME__ to your khafile project name
adb install -r -d build/android-native-build/__PROJECT_NAME__/app/build/outputs/apk/debug/app-debug.apk
# if you want to see application log in console:
adb logcat -s kinc kore DEBUG AndroidRuntime

arch argument will build your app only for one architecture. Check possible arch options in --help. About logcat tags:

  • kinc - logs from current Kha native part and your trace calls
  • kore - for older (non-kinc) Kha version
  • DEBUG - Stacktrace from native crashes (for Haxe and Kinc), so you can see basic info without IDE debugger usage.
  • AndroidRuntime - Stacktrace from java-side crashes. Mostly useful when you write or use Java/JNI libs.

Remember that --debug version is a lot slower than release. Lets make release build works too:

Signing app

In --debug mode (first line in command before), you do not need a signature for your device. But in order to build the release version, you need to sign apk.

Create a private signing keystore with single key:

keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

To have global keystore/password, open or create USER_HOME/.gradle folder and create gradle.properties file:

RELEASE_KEY_ALIAS=my-key-alias
RELEASE_STORE_PASSWORD=<The password you choose earlier with the keytool>
RELEASE_KEY_PASSWORD=<The password you choose earlier with the keytool>
RELEASE_STORE_FILE=PathTo/my-release-key.keystore

The path to the keystore file can only be relative. So you have to add a copy of the keystore file to build/android-native-build/__PROJECT_NAME__/app/, or add some number of ../ before path (starting directory is app). For example, if you put key.keystore to main MyProject/ folder, then you need to set ../../../../key.keystore path, because default directory is app.

Never publish a certificate or password to it!

In addition, it is recommended to have one certificate for all your applications. Be careful, if you lose a keystore file or password to it, you will not be able to update your applications on Google Play without completely losing their data. Have a few backups of this file.

Conditional Compilation

To execute android-native-specific code you can use conditional compilation flag:

#if kha_android_native
// This code will be run by android-native builds
#else
// For other platforms
#end
// Beware, this is separated flag for android java backend:
#if kha_android && !kha_android_native
// This code will be discarded by android-native builds
#end

See also System Defines.