Running Android applications on Arch using anbox

Hi community,

This is not a question, but an instruction on how to run Android apps on Manjaro in the most comfortable way. After having put quite a lot of effort into this myself, I felt the need to post a simple explenation somewhere.

By means of installation, the easiest way to run Android apps on your computer might be just to set up a virtual machine running Android x86, Phoenix OS, BlissOS or something similar, and it is also the most stable way I guess. But when you want to integrate Android apps into your desktop environment as if they were native linux applications, anbox (“Android in a box”) is the much more convenient way to go.
Whereas an emulator needs to boot up for quite some time whenever you want to use it, makes it only possible to use exactly one Android app at once and just in general does not integrate in your desktop environment as well as a native application would, anbox choses a completely different approach.
In anbox, you can have multiple windows running multiple different apps. For every app, anbox creates an entry in your menu which you simply have to tap in order to launch the app, as you do with every regular application. You simply install apps using the playstore, or using the adb shell if you want to. But anbox needs to boot up to. Still, this taks only a few seconds, and if you add anbox to your autostart it is almost completely omitted.

If anbox caught your attention now, this is probably the instruction to go with.

1. Installing anbox

1.1. Prerequisites

First of all, you should bring your system up to date (this is really important, because the header package you will download later is always designed for the newest kernel version)

sudo pacman -Syyu

If this upgraded your kernel, reboot first of all.
Now, install dkms from the offical repos.

sudo pacman -S dkms

You will need the apropriate header package depending on your kernel. In order to find out your kernel version, execute:

uname -a

The output will look like this:

Linux ASUSN751JK 4.14.56-1-MANJARO #1 SMP PREEMPT Tue Jul 17 13:20:49 UTC 2018 x86_64 GNU/Linux

Depending on the first three digits of your kernel version, you have to install the relating header. In my case it’s 4.14, so I have to run:

sudo pacman -S linux414-headers linux414

You probably have to do this for every kernel you have installed.
Finally reboot again, just to be on the safe side.

1.2. Installing anbox
According to the offical web page, you should install anbox using snap. But I haven’t made any good experiences that way. The way that worked for me was by installing the required packages from the AUR. To download packages from the AUR I use yay, You can of course use any package manager of your choice.

yay -S anbox-git anbox-modules-dkms-git anbox-image

Now, you have to enable the kernel modules you just installed:

sudo modprobe ashmem_linux
sudo modprobe binder_linux

You should have two new devices now, /dev/ashmem and /dev/binder.
Finally, enable the anbox service:

sudo systemctl enable anbox-container-manager.service

After that, you have to reboot again.
You should now be able to run anbox from the menu.

2. Installing apps

You can already install apps by now. Just download the x86 version (or better x86_64 if your PC is 64-bit and such a version exists for a given app) of an app from apkmirror for instance. For example, I downloaded the Google keep apk file, and install it using adb:

adb install com.google.android.keep_5.0.251.04.70-182510470_minAPI21\(x86\)\(nodpi\)_apkmirror.com.apk

2.1. Google Services, Google Play, ARM based apps 3D acceleration

Some apps require the google services to run properly, e.g. Google Keep, Gmail or youtube.
Installing apps using the Playstore is way more convenient than by executing an adb shell command.
Or you just want to use apps compiled for ARM or make use of your 3D acceleration.

All of these problems can be solved using the instructions in [2]. However, this webpage is only available in German. There is also a similar English version [3], but this one is not as extensive as the German source.

If you would like to enable all of those features, you can just follow my instructions here.

On way or another, you have to replace the Android image found at /var/lib/anbox/android.img.

But first of all, we have to install some packages we require in order to build the new image:

sudo pacman -S lzip squashfs-tools wget unzip tar

The most comfortable way to build the new image and replace as well as backup the old one is by executing the following script using bash and sudo:

set -e

OPENGAPPS_RELEASEDATE="20180706"
OPENGAPPS_FILE="open_gapps-x86_64-7.1-mini-$OPENGAPPS_RELEASEDATE.zip"
OPENGAPPS_URL="https://github.com/opengapps/x86_64/releases/download/$OPENGAPPS_RELEASEDATE/$OPENGAPPS_FILE"

HOUDINI_URL="http://dl.android-x86.org/houdini/7_y/houdini.sfs"
HOUDINI_SO="https://github.com/Rprop/libhoudini/raw/master/4.0.8.45720/system/lib/libhoudini.so"

WORKDIR="$(pwd)/anbox-work"

# check if script was started with BASH
if [ ! "$(ps -p $$ -oargs= | awk '{print $1}' | grep -E 'bash$')" ]; then
   echo "Please use BASH to start the script!"
	 exit 1
fi

# check if user is root
if [ "$(whoami)" != "root" ]; then
	echo "Sorry, you are not root. Please run with sudo $0"
	exit 1
fi

# check if lzip is installed
if [ ! "$(which lzip)" ]; then
	echo -e "lzip is not installed. Please install lzip.\nExample: sudo apt install lzip"
	exit 1
fi

# check if squashfs-tools are installed
if [ ! "$(which mksquashfs)" ] || [ ! "$(which unsquashfs)" ]; then
	echo -e "squashfs-tools is not installed. Please install squashfs-tools.\nExample: sudo apt install squashfs-tools"
	exit 1
else
	MKSQUASHFS=$(which mksquashfs)
	UNSQUASHFS=$(which unsquashfs)
fi

# check if wget is installed
if [ ! "$(which wget)" ]; then
	echo -e "wget is not installed. Please install wget.\nExample: sudo apt install wget"
	exit 1
else
	WGET=$(which wget)
fi

# check if unzip is installed
if [ ! "$(which unzip)" ]; then
	echo -e "unzip is not installed. Please install unzip.\nExample: sudo apt install unzip"
	exit 1
else
	UNZIP=$(which unzip)
fi

# check if tar is installed
if [ ! "$(which tar)" ]; then
	echo -e "tar is not installed. Please install tar.\nExample: sudo apt install tar"
	exit 1
else
	TAR=$(which tar)
fi

# use sudo if installed
if [ ! "$(which sudo)" ]; then
	SUDO=""
else
	SUDO=$(which sudo)
fi

echo $WORKDIR
if [ ! -d "$WORKDIR" ]; then
    mkdir "$WORKDIR"
fi

cd "$WORKDIR"

if [ -d "$WORKDIR/squashfs-root" ]; then
  $SUDO rm -rf squashfs-root
fi

# get image from anbox
cp /var/lib/anbox/android.img .
$SUDO $UNSQUASHFS android.img

# get opengapps and install it
cd "$WORKDIR"
if [ ! -f ./$OPENGAPPS_FILE ]; then
  $WGET -q --show-progress $OPENGAPPS_URL
  $UNZIP -d opengapps ./$OPENGAPPS_FILE
fi


cd ./opengapps/Core/
for filename in *.tar.lz
do
    $TAR --lzip -xvf ./$filename
done

cd "$WORKDIR"

$SUDO cp -r ./$(find opengapps -type d -name "PrebuiltGmsCore")						./squashfs-root/system/priv-app/
$SUDO cp -r ./$(find opengapps -type d -name "GoogleLoginService")				./squashfs-root/system/priv-app/
$SUDO cp -r ./$(find opengapps -type d -name "Phonesky")									./squashfs-root/system/priv-app/
$SUDO cp -r ./$(find opengapps -type d -name "GoogleServicesFramework")		./squashfs-root/system/priv-app/

cd ./squashfs-root/system/priv-app/
$SUDO chown -R 100000:100000 Phonesky GoogleLoginService GoogleServicesFramework PrebuiltGmsCore

# load houdini and spread it
cd "$WORKDIR"
if [ ! -f ./houdini.sfs ]; then
  $WGET -q --show-progress $HOUDINI_URL
  mkdir -p houdini
  $SUDO $UNSQUASHFS -f -d ./houdini ./houdini.sfs
fi

$SUDO cp -r ./houdini/houdini ./squashfs-root/system/bin/

$SUDO cp -r ./houdini/xstdata ./squashfs-root/system/bin/
$SUDO chown -R 100000:100000 ./squashfs-root/system/bin/houdini ./squashfs-root/system/bin/xstdata

$SUDO $WGET -q --show-progress -P ./squashfs-root/system/lib/ $HOUDINI_SO
$SUDO chown -R 100000:100000 ./squashfs-root/system/lib/libhoudini.so

$SUDO mkdir -p ./squashfs-root/system/lib/arm
$SUDO cp -r ./houdini/linker ./squashfs-root/system/lib/arm
$SUDO cp -r ./houdini/*.so ./squashfs-root/system/lib/arm
$SUDO cp -r ./houdini/nb ./squashfs-root/system/lib/arm/

$SUDO chown -R 100000:100000 ./squashfs-root/system/lib/arm

# add houdini parser
mkdir -p ./squashfs-root/system/etc/binfmt_misc
echo ":arm_dyn:M::\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x28::/system/bin/houdini:" >> ./squashfs-root/system/etc/binfmt_misc/arm_dyn
echo ":arm_exe:M::\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28::/system/bin/houdini:" >> ./squashfs-root/system/etc/binfmt_misc/arm_exe
$SUDO chown -R 100000:100000 ./squashfs-root/system/etc/binfmt_misc

# add features
C=$(cat <<-END
  <feature name="android.hardware.touchscreen" />\n
  <feature name="android.hardware.audio.output" />\n
  <feature name="android.hardware.camera" />\n
  <feature name="android.hardware.camera.any" />\n
  <feature name="android.hardware.location" />\n
  <feature name="android.hardware.location.gps" />\n
  <feature name="android.hardware.location.network" />\n
  <feature name="android.hardware.microphone" />\n
  <feature name="android.hardware.screen.portrait" />\n
  <feature name="android.hardware.screen.landscape" />\n
  <feature name="android.hardware.wifi" />\n
  <feature name="android.hardware.bluetooth" />"
END
)


C=$(echo $C | sed 's/\//\\\//g')
C=$(echo $C | sed 's/\"/\\\"/g')
$SUDO sed -i "/<\/permissions>/ s/.*/${C}\n&/" ./squashfs-root/system/etc/permissions/anbox.xml

# make wifi and bt available
$SUDO sed -i "/<unavailable-feature name=\"android.hardware.wifi\" \/>/d" ./squashfs-root/system/etc/permissions/anbox.xml
$SUDO sed -i "/<unavailable-feature name=\"android.hardware.bluetooth\" \/>/d" ./squashfs-root/system/etc/permissions/anbox.xml

# set processors
ARM_TYPE=",armeabi-v7a,armeabi"
$SUDO sed -i "/^ro.product.cpu.abilist=x86_64,x86/ s/$/${ARM_TYPE}/" ./squashfs-root/system/build.prop
$SUDO sed -i "/^ro.product.cpu.abilist32=x86/ s/$/${ARM_TYPE}/" ./squashfs-root/system/build.prop

$SUDO echo "persist.sys.nativebridge=1" >> ./squashfs-root/system/build.prop

# enable opengles
$SUDO echo "ro.opengles.version=131072" >> ./squashfs-root/system/build.prop

#squash img
cd "$WORKDIR"
rm android.img
$SUDO $MKSQUASHFS squashfs-root android.img -b 131072 -comp xz -Xbcj x86

# update anbox image
cd /var/lib/anbox
mv android.img android_bkup.img
mv "$WORKDIR/android.img" android.img 

This script was copied from [4] and slightly reworked to work properly with Arch linux.
If you only want to have some of the features this script provides, you have to remove some parts of it.

After successful execution, the image /var/lib/anbox/android.img has been replaced and a backup can be found in/var/lib/anbox/android_bkup.img. You can savely remove the folder ~/anbox-work if you would like to:

sudo rm -rf ~/anbox-work

Now execute the following two commands to restart the anbox services:

sudo systemctl restart anbox-container-manager.service
systemctl --user restart anbox-session-manager.service

Last but not least, in order to make the Google Play Services run properly, you have to open anbox and go to Android Settings > Apps > Google Play Services > Permissions and enable everything.

3. Adding a network connection

But you can’t use the Play Store without having an Internet connection, and same applies to most apps.
In the following I will describe how to set up the virtual network controller for anbox.

Start by downloading anbox-bridge.sh from [5] and move it to /usr/lib/anbox/anbox-bridge.sh (Possibly you have to create the folder anbox if it does not exist). This script as well as the folder should be owned by root (user as well as group). Mark the file executable.
Now we need to create a program that executes /usr/lib/anbox/anbox-bridge.sh start. Copy the following text to a file called anbox-bridge.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
{
	setuid(0);

	char buf[512];
	sprintf(buf, "/usr/lib/anbox/anbox-bridge.sh %s", "start");
	system((char *)buf);
}

Compile using gcc

gcc -o anbox-bridge anbox-bridge.c

And move the compiled file to /usr/local/bin/anbox-bridge. Change the file’s owner to root (user as well as group) and set the setuid bit:

chmod u+s /usr/local/bin/anbox-bridge

Now you just have to run anbox-bridge from terminal in order to enable network connection for anbox. Please consider that you have to run this command before you start anbox; A reboot might be necessary when it is already running.
Simplest solution would be to add an autostart entry (approach dependeing on your desktop environment, e.g. in XFCE it’s Session and Startup > Application Autostart > Add) that executes the command anbox-bridge on startup of the desktop environment.

4. Other stuff

4.1. Autostart anbox

I wanted anbox to boot automatically as soon as my desktop environment starts. Therefore, I added this to my desktop environments autostart:

anbox session-manager&

This will automatically boot up anbox on login in background without showing the boot dialog.
The exact approach of adding a command to your autostart is dependeing on your desktop environment, e.g. in XFCE it’s Session and Startup > Application Autostart > Add.

However, if you have some Android app launchers on your desktop, this might cause their icons not to load (at least for me it did). On XFCE ctrl+R allows to refresh all desktop icons, and when pressing this after the session-manager has completed booting, the icons will be loaded. But if you experience this icon-bug with the above command, you might just instead use this one:

(sleep 1; anbox session-manager)&

4.2. Shutdown / reboot anbox

Although anbox generally runs quite stable, it might happen from time to time that it is just stuck, and you need to reboot it.

In order to shutdown anbox, execute

killall anbox

without root privileges. This will kill all running anbox processes except the container manager (which should not be killed, and will not be killed because it is owned by root) and therefore cause a shutdown. The next time you start an app, you will se a boot screen.

In order to reboot anbox immediately, just restart anbox after having shut it down:

killall anbox; sleep 1; anbox launch --package=trash

This will kill all running apps and then show the reboot screen.

You can also reboot anbox “silently”:

killall anbox; sleep 1; anbox session-manager&

If you need those functionalities more often, just put the required commands in a desktop file and place it in your menu or on your desktop.

5. Sources / See also

[0] https://anbox.io/
[1] https://github.com/anbox/anbox/issues/3 (here, especially the first post of artex-files is quite helpful)
[2] https://geeks-r-us.de/2017/08/26/android-apps-auf-dem-linux-desktop/
[3] https://www.linuxuprising.com/2018/07/anbox-how-to-install-google-play-store.html
[4] https://raw.githubusercontent.com/geeks-r-us/anbox-playstore-installer/master/install-playstore.sh
[5] https://github.com/anbox/anbox/blob/master/scripts/anbox-bridge.sh

6. Bugs / Missing features

  • I haven’t found a way to change the keyboard layout by now. The official telegram group states that it should be possible by installing AnySoftKeyboard, but I had no success there.
  • Mouse scroll only works when you’re moving the mouse at the same time; When the mouse is idle, scrolling using the scroll wheel won’t work
  • Every time I open up an app, it opens in the bottom-right corner of my screen (not in the center as it would be expected). Now I’m moving and resizing the window during use, but window position and size are not saved! So the next time I open up the app, I will have to move/resize it again
  • Some of the Google apps, for instance Calendar or Contacts, just don’t want to take over my Google account. Calendar is “Checking info…” and asks for a email adress then, When entering my mail adress it says “This account already exists on your device”. Clicking escape while it’s “Checking info…” makes it usable, but still I can’t see my Google account’s appointments. Same for contacts except it’s not “Checking info…”
  • Would be really cool if you could hide selected apps from the Anbox Application Manager / standard application menu

I don’t take charge for anything I explained here! This post describes the way it worked for me, it may not work for you. Still, if you have any questions, suggestions or just success, I would be pleased if you reply to this topic.

I hope this guide will be helpful for someone once.

25 Likes

Hi @Galveston01 there is a category for tutorials #technical-issues-and-assistance:tutorials

Better -Syyu

That command re-install the kernel which is not a needed step

and for install dkms you need the headers for all the kernels install, not only for the one in use; otherwise dkms fail


Also exist BlissOS for Android on general computers

I am somehow unable to move it to this category :thinking:

1 Like

Done :wink:

The developer of anbox just informed me about a more elegant way to boot up anbox from terminal.
Updated 4.1. with this new method.

Thanks for making the Gapps script compatible with the AUR build. I had the same idea, but never actually ended up doing it.

If your PC is 64-bit, you should be downloading the x86_64 build if available.

I didn’t have to add a network connection, it worked out of the (An)box. :laughing: Or it could be because I started the systemd-resolved.service & systemd-networkd.service. This is what I run before using Anbox:

sudo systemctl start systemd-resolved.service
sudo systemctl start systemd-networkd.service
sudo systemctl start anbox-container-manager.service
systemctl --user start anbox-session-manager.service

You could restart the services above. I’m not sure if you actually need to restart all of them, though.

What I meant was a native way to reboot, inside the Anbox Aplication Manager for instance.

Still, your approach works really well, although I find a similar one a bit more convenient. So I added a section 4.2. Shutdown / reboot anbox and removed this point from the list.

If you run those two commands, you don’t have to reboot. If you reboot, you don’t have to run those commands. :wink:

It should be:
sudo rm -rf ~/anbox-work

That’s not necessary. All you need to do is restart the services:

sudo systemctl restart anbox-container-manager.service
systemctl --user restart anbox-session-manager.service

If a file isn’t installed by a package it should be put in a directory under /usr/local/, e.g. /usr/local/bin/anbox-bridge

5 posts were split to a new topic: Errors compiling anbox-bridge

This looks great, but I will wait for a version of anbox that is easier to get running

Hi. I think it is not necessary to create and compile C program in order to start anbox-bridge.sh. The same could be done just with shell.

  • If you want to start it from terminal, save the script to any location, make it executable and run sudo /path/to/script/anbox-bridge.sh start before starting anbox

  • If you want to autostart it from DE following should work (I haven’t tested it yet): save the script to /usr/local/bin as root, make it executable and add anbox-bridge.sh start to DE autostart commands.

The preferred method is by snap as their website suggest, plus the DKMS kernel module

https://docs.anbox.io/userguide/install.html

https://docs.anbox.io/userguide/install_kernel_modules.html

A post was split to a new topic: adb devices shows no devices

I am getting a FATAL error with
sudo modprobe binder_linux
I've found a fix, but I think i will wait until AUR fixes it.

What i would love is a

SPIN with Anbox Pre-installed as a "Chromeosified" Manjaro
I would pray for some enthusiasts to make it, or it to be added as an option in Calamares or Architect

i can do that an iso with anbox pre-installed but if it will work or not no garranty.

Then you already know how to fix it.

...someone else to fix it for you?

Are you sure? It's not a stable project at all as you may already know. Do you really want to be the go-to tech support guy when it doesn't work?

2 Likes

Thanks in advance,

No guaranty ... As any Lignux

If you enjoy trying I gladly will be your beta tester

Anbox plus a daily cron plus at boot.
"yaourt -Suya --noconfirm" or similar to "Chromeosify the OS to make it even more KISS for non technical users : 9x% in my experience.

Well I have an KDE spin
Manjarowish
it does not have anbox preinstalled
But you can install from repo
Anbox is in the chaotic-aur repo. And I have added that repo in the iso.just use it with LTS kernel.

if you want

Download>>>>>>>
https://sourceforge.net/projects/manjarowish/files/manjarowish-kde-light-btrfs/19.07.17/manjarowish-kde-light-btrfs-19.07-testing-x86_64.iso/download