You are here

Tutorial : cross-compile from Linux to Android (Vala)

Objectives : compile a Vala application under Linux, targeting Android.

We have a Vala console application, without any additional libraries.
We want to create an Android version of it without modifying the source code.

(PS : the targeted phone/tablet needs to be "rooted" and to have an installed terminal emulator)



Prerequisites

A Linux Debian or Ubuntu distro, with Vala installed.

To install Vala, please read this previous article, part 1).

1) Install the compilation toolchain

- Download the Android NDK r7b for Linux :
android-ndk-r7b-linux-x86.tar.bz2 (61.3 Mb)

(and extract it to "/opt" :
mv android-ndk-r7b-linux-x86.tar.bz2 /opt
cd /opt
tar xfvj android-ndk-r7b-linux-x86.tar.bz2)

- Download the agcc script (thanks to Giulio Lunati for his work) :
agcc.pl (5 Kb)

(et install it to "/opt", making it executable :
mv agcc.pl /opt
chmod a+x /opt/agcc.pl
ln -s /opt/agcc.pl /usr/bin/agcc
)

- Configure these environment variables (either by adding these commands at the end of "/etc/profile" then rebooting, either by typing them directly in a terminal) :
export NDK=/opt/android-ndk-r7b
export PATH=$PATH:/opt/android-ndk-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin


2) Install the GLib libraries for Android

- Download GLib 2.28.1 for Android :
glib-2.28.1-android_(TARNYKO).tar.bz2 (1.81 Mb)

(and extract them to a new "/opt/android" folder :
mkdir /opt/android
mv glib-2.28.1-android_\(TARNYKO\).tar.bz2 /opt/android
tar xfvj glib-2.28.1-android_\(TARNYKO\).tar.bz2
)


3) Compile

- We will tell "pkg-config" to locate Android libraries in our custom path :
export PKG_CONFIG_PATH=/opt/android/lib/pkgconfig

We are ready ! We will use some Vala source code (not using any additional library).

- Compile :
valac --cc=agcc -o myprogram myprogram.vala

A new executable (here myprogram) should have been created if everything went well.
It won't run on our Linux system, because it's targeting Android !

4) Release

- Create a folder containing the binary and all library files (.so) found in this archive :
unzip glib-2.28.1-android_runtime_(TARNYKO).zip
mkdir ~/distri
cp myprogram ~/distri
cp *.so.* ~/distri

- Connect your Android tablet/phone to your computer. Using the adb program, which you naturally already have on your system :-), send these files one by one to a directory of the device :
adb remount
adb push monprogramme /system
adb push libasprintf.so.0 /system
adb push libgio-2.0.so.0 /system
...
adb push libintl.so.8 /system


5) Run

- On the device itself, open a terminal, add the program path to a var and run it !
export LD_LIBRARY_PATH=/system
cd /system
./myprogram

Well done !

Comments

Very good job! It's a great break-trough for Vala.

(originally posted by Tal Hadad)

Thans for your job! I will try it under HuaWei's Balong platform.

(originally posted by Matrix)

You're welcome ! Keep me informed if possible, always interested in such experiments ^^.

What is the minimum android system version required?

(originally posted by PedroHLC)

I ran it successfully under Android 2.2 (Froyo), so we seem to be safe.
Online documentation says it should work down to 1.5, thus below API Level 8, but you'd have to tweak then. I wouldn't try personally.

I've tried the executable way and it worked fine; now I'm trying to make a shared library and add it to the Java code, it compiles the shared library, but when I test it, it's throwing unsatisfatory link (throught LogCat). Can you do a new tutorial for creating shared librarys and linking them into the App?

(originally posted by PedroHLC)

Hi PedroHLC,

You're very lucky, I just finished the new Cairo-Vala tutorial, and it precisely uses a shared library ^^.

Look at the sample project files, especially the compile scripts and the cairo-android.glue.c source, if you want to know how it works.
I just realized I didn't attach a Java sample, here's the one I used to create my APK.
See my former message on Vala-list to get more details.
Hope this helps.

PS : you'll need to re-download the GLib & Cairo archives from this new tuto, these are different, they are static libraries. I'll try to make that more obvious.

Tarnyko, I do not have much experience with Android yet. Why does the device have to be rooted? Will a program created in this manner still be able to be sold/used by a person without a rooted device?

Hi Myrddin,

In fact, it doesn't have to. More complete answer is here.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.