Cross compile for Android - Hello World

From WickyWiki
Revision as of 21:21, 9 August 2016 by Wilbert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Cross compile for Android and create a binary that outputs 'Hello World' to your teminal.

Note: your Android device needs to be rooted.

Sources

Tutorial:

Toolchain download latest version:

Android version history, code numbers and API levels:

About ADB:

CheckInstall:

Create the file "hello.c"

We are going to use this sourcecode to create a binary for your Android device.

 
mkdir $HOME/ndk_hello
cd $HOME/ndk_hello
gedit hello.c
 
#include <stdio.h>
int main(int argc, char** argv) {
   printf("Hello world\n");
   return 0;
}

Simple compile using the repositories cross-compiler (skip this)

This will probably result in an executable file but it would be slow and big. I have seen that this example would create a 590Kb binary while the NDK would result in a 5Kb binary. Read on and use the latest NDK compiler with optimized settings.

 
#install
sudo apt-get install gcc-arm-linux-gnueabi
#compile
arm-linux-gnueabi-gcc -static -march=armv5 hello.c -o hello_unoptimized

Prepare the Android NDK compiler

# checkinstall includes all neccesary components and has some other uses 
sudo apt-get install checkinstall
# select base folder
cd $HOME
# download google toolchain r12b, check online for newer versions and changes
wget http://dl.google.com/android/repository/android-ndk-r12b-linux-x86_64.zip
unzip android-ndk-*-linux-x86_64.zip
# rename folder
mv android-ndk-* ndk
# select ndk folder and build/install the toolchain, modify the parameters to your needs:
# --toolchain=arm-linux-androideabi-4.9
# --platform=android-23 (6.x / Marshmallow)
# --install-dir=$HOME/ndkTC
cd ndk/
build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.9 --platform=android-23 --install-dir=$HOME/ndkTC

Prepare Toolchain environment

The compiler can have many options and you should use them for better results. "JustArchi" made a script for us with default optimization settings. The only thing this does is set a number of environment variables that are used to run the compiler.

Copy past this script, we will save it as 'configTC.sh':

 
cd $HOME/ndk_hello
#past the text of the 'Toolchain configuration script' and save
gedit configTC.sh
#execute the script and read the output
source configTC.sh

Compile

 
# compile the sourcecode
$CCC hello.c -o hello
# strip unnecessary data
$SSTRIP hello
# analyse the binary with
readelf -A hello

"readelf" output:

Tag_CPU_name: "ARM v7"
Tag_CPU_arch: v7
Tag_CPU_arch_profile: Application
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-2
Tag_FP_arch: VFPv2
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_enum_size: int
Tag_ABI_optimization_goals: Aggressive Speed
Tag_CPU_unaligned_access: v6

Note that you might run into problems, for example because of system dependencies. Someone might be able to help you if you go here or here.

Android device settings

You need root privileges on your Android device to copy your binary to the right location.

  • Enable developer menu in settings:
    • Settings > go to bottom > About > Press "Build number" 7 times
    • The developer menu will appear directly before the About menu item in Settings
  • Go into the developer menu and enable Android USB debugging (ADB)
  • Allow Root access for ADB to be able to use "su" later on

Install ADB tools

 
sudo apt-get install android-tools-adb

Sometimes ADB seems to have some difficulties. Reconnect USB cable and or restart it like this:

 
adb kill-server
adb start-server
adb devices -l

Copy to the Android device and run

The file needs to be in a location where it can be made executable (like /data/local). Furthermore, you need to have root privileges (su) to copy and make it executable.

 
#copy to device
cd $HOME/ndk_hello
adb push hello /sdcard/tmp/hello
#on the device copy it to the executable location (as root)
adb shell su -c "cp /sdcard/tmp/hello /data/local/hello"
#make executable (as root)
adb shell su -c "chmod 751 /data/local/hello"
#run
adb shell /data/local/hello