Run Android Apps In Linux Using Anbox Android Emulator


Categories:

Last Updated: September/24/2022


Getting Started

Windows users have a lot of Android emulators to choose from; BlueStacks, Noxplayer, MEmu, etc. If you are on Linux, then you are not left out. Though the number of emulators for Linux are far lower and are behind in performance compared to those of Windows, you should be able to run your Android apps in Linux using this guide.

The emulator we will be using here is called Anbox (short for Android in a box). It is available as a snap, so can be used in any distro. Our focus in this guide is on Debian-based distros but you can follow the same steps if you are in another distro as far as you have snap installed or you know how to install snap on the distro.

Anbox uses Linux namespaces (user, pid, uts, net, mount, ipc) to run a full Android system in a container and provide Android applications on any GNU/Linux-based platform. The Android inside the container has no direct access to any hardware. All hardware access is going through the anbox daemon on the host. The Android system inside the container uses different pipes to communicate with the host system and sends all hardware access commands through these.

The Android runtime environment ships with a minimal customized Android system image based on the Android Open Source Project. The used image is currently based on Android 7.1.1 at this time of writing.

Apps run really smooth and very first as if they were Linux apps, giving Anbox a plus in this case over VMs. However, some apps might not be compatible or might not work well, coupled with the fact that Anbox is based on Nougat.

You might also be interested in:

Installation

Here is how to install Anbox and run Android apps;

First, Anbox is available as a snap. While some distros like recent versions of Ubuntu have snap enabled by default, others don’t. To install snap, use the commands below;

– [In recent Linux Mint versions], you need to remove the file nosnap.pref from /etc/apt/preferences.d before you can install snap. The below command moves nosnap.pref to a renamed nosnap.backup file in your Documents directory:

sudo mv /etc/apt/preferences.d/nosnap.pref ~/Documents/nosnap.backup

– Now update apt:

sudo apt update

– Install snapd:

sudo apt install snapd

– Install snap core:

sudo snap install core

– Install Anbox:

sudo snap install --devmode --beta anbox

Currently, only the edge channel is used to install the Anbox snap. Once proper confinement for the Anbox snap exists, the candidate and stable channels will be used as well.

Note that on lower Debian (below Buster) or Ubuntu (below 18.04), you might need to get the necessary kernel support first before installing Anbox. This can be found in the beginning sections of this guide.

If you are on another distro that is not Debian-based, use its corresponding package manager and commands to install snap, and then use snap to install Anbox the same way as explained above. You can see more details here on how to install snap in different distros.

As a side effect of using --devmode the snap (Anbox) will not automatically update. In order to update to a newer version you can run:

snap refresh --edge --devmode anbox

To check Anbox version and other details:

snap info anbox

To uninstall Anbox:

snap remove anbox

Once the snap is removed you may have to remove the legacy DKMS modules (if you installed them):

sudo apt install ppa-purge
sudo ppa-purge ppa:morphis/anbox-support

– You can launch Anbox from the apps menu or through the terminal by using the command:

anbox.appmgr

Transferring Files To Anbox

Now that you have Anbox installed, the first thing you might be thinking is how to transfer files between the Android system and your Linux distro. There are many ways to archive this:

a) Through adb

This can be used to transfer apks to Anbox which you can then install through Android package installer. You can also transfer any file format: audio, video, docs, etc.

You can set up adb on your Linux system using this guide. After setting up adb, run the command adb devices and make sure an emulator is on the list (which is Anbox). If it doesn’t appear, try launching Anbox (to make sure it is running) and then run the command again.

– To transfer any file from your system to Anbox through adb, use:

adb push <path-to-file-in-Linux>/<filename.extension> <path-to-store-file-in-Anbox>

e.g adb push /home/sako/.bashrc /sdcard/Download/

If the file is already in the current working directory, then you can also use:

adb push <filename.extension> <path-to-store-file-in-Anbox>

e.g adb push .bashrc /sdcard/Download/

If the file doesn’t have an extension, use:

adb push <path-to-file-in-Linux>/<filename> <path-to-store-file-in-Anbox>

If the file has space in its name, then enclose it (together with its extension in quotation marks) otherwise you will get an error e.g adb push /home/sako/Downloads/'Mstudio happymod.apk'

NB: To transfer all files of a particular format (e.g .mp4) found in a directory to a directory (e.g Movies) in Anbox, use:

adb push *.mp4 /sdcard/Movies

– Transferring files from Anbox to your Linux system is just the reverse of the commands above and using pull instead of push.

adb pull <file-path-in-Anbox>/<filename.extension> <path-to-store-file-in-Linux>

If <path-to-store-file-in-Linux> is not stated, then the file is moved to the current working directory.

Note that files transferred to Anbox might not be visible in the default file manager. But going to Settings > Storage > Explore or Settings > Storage > Others, and looking at the specific folder, you will see the files. To bypass this hassle, install another file manager e.g MiXplorer Silver is a good choice. MiXplorer Silver also being a root file manager, you will be able to view the root directory and system files of Anbox.

b) Accessing Anbox Folder In Linux File Manager And Easily Transfer Stuff Between The Two

Meth 1

Run the below command:

sudo setfacl -m 'u:<username>:rwx' /var/snap/anbox/common/data/media

e.g sudo setfacl -m ‘u:sako:rwx’ /var/snap/anbox/common/data/media

Now go to /var/snap/anbox/common/data/media/0 in your Linux file manager to access the Android folders. You can navigate to this directory or open it as root and easily copy, paste, move, delete stuff.

Meth 2

This method is similar to the one above but creates a symlink (symbolic link) for easy access of the Android storage. Run the below command to take ownership of the media folder so that it can be read, otherwise all files inside it don't exist.

sudo chown -R <username> /var/snap/anbox/common/data/media

e.g sudo chown -R sako /var/snap/anbox/common/data/media

Now create a symlink in any directory of your choice (here I will use my home directory):

sudo ln -s /var/snap/anbox/common/data/media/0 /home/<username>

e.g sudo ln -s /var/snap/anbox/common/data/media/0 /home/sako

Meth 3

Other suggestions on how to transfer files are found here though I haven’t tested all e.g someone was talking about using kdeconnect.

Installing Apps In Anbox

Anbox does not support Play Store (which relies on the proprietary Google Play Services, that are not installed) by default, neither does it come with any application store. All you need to do is download apks and install. The apk files you will sometimes find on the internet tend to only have ARM support, and will therefore not work on x86_64. [If you want to use Play Store in Anbox or enable support for ARM applications, then visit this other guide]. Installing apps can be done in the following ways:

a) Through adb

If the apk files are in the file manager of your distro, then you can use adb to install and manage them. To install apps, use:

adb install <path-to-file>/<filename>.apk

e.g adb install /home/sako/Downloads/'Mstudio happymod.apk'

If the file is already in the working directory, you can state just its <filename>.apk:

adb install <filename>.apk

e.g adb install 'Mstudio happymod.apk'

Note that the signs ‘’ have being used in ‘Mstudio happymod.apk' because the name is not joint (i.e contains space) which will give an error if not placed in quotation marks. If it were joint e.g Mstudiohappymod.apk, then the above commands should be:

adb install /home/sako/Downloads/Mstudiohappymod.apk

adb install Mstudiohappymod.apk

If there is another device (e.g your phone) connected, you need to unplug it before running the commands else you will get an error. Or if you don’t want to unplug it, include the -e flag to tell adb the command you are running is for the emulator (Anbox).

adb -e install <path-to-file>/<filename>.apk
adb -e install <filename>.apk

NB: There are numerous adb commands that you can use to get details or perform operations in Anbox. For that, see our lists of adb commands and adb shell commands.

b) Through An App Store

My app store of choice is Aptoide (there are many others) as it downloads and automatically install applications just like the Play Store (you might need to grant storage permission and allow installing from unknown sources for the first time). You can download the apk on your Linux distro from here. Then use adb to install it, or first use adb to transfer it to Anbox and then install it (through Android package installler).

You can also get apps from F-Droid or if you are a fan of modded apps from HappyMod.

Handling Media Playing

Apparently, there is a problem with the latest prebuilt image file of Anbox, which for some reason lacks config files for media codecs, including audio codecs. Because of this, audios and videos might not work when you play them in Anbox (YouTube, WhatsApp, Browsers, Media Players, etc). Below are some suggestions gotten here to help you add the config files and bypass the issues. If a path/directory doesn’t exist, create it manually as you apply the below fixes.

a) GUI

– Download the file codecs.zip at the bottom of this page or click on this link to automatically download it. The files in the zip are also present here.

— Extract the files in the zip and copy them to /var/snap/anbox/common/rootfs-overlay/system/etc. If the path doesn’t exist, create it manually.

— Lastly, Run the below commands one after another:

sudo snap set anbox rootfs-overlay.enable=true
sudo snap stop anbox
sudo snap start anbox

Now test to see that the issue is fixed.

b) Terminal

If you prefer the terminal, then you can achieve the steps above using the below commands.

— The rootfs-overlay, system and etc directories might not be present, therefore create them first by running the commands:

sudo mkdir /var/snap/anbox/common/rootfs-overlay
sudo mkdir /var/snap/anbox/common/rootfs-overlay/system
sudo mkdir /var/snap/anbox/common/rootfs-overlay/system/etc

— Download, install the media codecs and set everything up by running the below commands:

cd /var/snap/anbox/common/rootfs-overlay/system/etc
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_codecs.xml
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_codecs_google_audio.xml
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_codecs_google_telephony.xml
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_codecs_google_tv.xml
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_codecs_google_video.xml
sudo wget https://raw.githubusercontent.com/anbox/anbox/master/android/media/media_profiles.xml
sudo snap set anbox rootfs-overlay.enable=true
sudo snap stop anbox
sudo snap start anbox

Now test to see that the issue is fixed.

NB: If the above steps didn’t work for you (which is not obvious), see all suggested solutions here.

Anbox Apps, Resizing Windows

Apps launched start with a wide, yet well-oriented window. To make them fullscreen, click on the fullscreen icon at the top-right. Clicking on it again reduces the window to the previous size. To manually resize a window, go to any of its edges, click and hold down the mouse then move inward/outward to your satisfaction.

To close a window, click on the X button.

For navigating backward (plus even closing windows), use the Esc key on your keyboard.

Anbox Apps In System Apps Menu

It is possible to add Anbox apps to the system apps menu for easy accessibility. Go to ~/snap/anbox/common/app-data/applications/anbox and you will see the .desktop files of Anbox applications together with their images. To integrate apps to the apps menu,

– Double-click on its .desktop file and choose Mark as Trusted.

– Now copy it (not move) to ~/.local/share/applications.

– It should now appear in the apps menu. You can even configure the .desktop file as you like (see this guide).

Anbox Apps And Docks

The Cinnamon panel handles Anbox apps poorly compared to Plank dock. It doesn’t display the icons of the apps which is possible with Plank. If you pin an Android app in the Cinnamon panel, it is started in a new icon instead of the pinned one, while Plank tries to bypass this. Because of all these, I always use Plank with Anbox for a better dock experience.

If you don’t have Plank installed, you can install it by searching in the Software Store of your distro or for Debian-based distros, run:

sudo apt install plank

To edit Plank dock, hold down the Ctrl key and right-click on the dock then choose Preferences.

Locating Some Files From Linux File Manager

– You can find installed Android applications in /var/snap/anbox/common/data/app.

– You can find the default.prop file in /var/snap/anbox/common/state.

– Logs are in /var/snap/anbox/common/logs.

– Apps data are in /var/snap/anbox/common/data/data, /var/snap/anbox/common/data/user_de/0 or /var/snap/anbox/common/data/media/0/Android/data.

– List of installed packages: /var/snap/anbox/common/data/system/packages.list.

Start, Stop & Restart Anbox

– If Anbox hangs or is not performing well, you can stop it using:

sudo snap stop anbox

Or restart it using:

sudo snap restart anbox

– To start it back (and after a reboot):

sudo snap start anbox

You can also stop, start, restart the service:

sudo snap stop anbox.container-manager
sudo snap start anbox.container-manager
sudo snap restart anbox.container-manager

NB: To see all running snap services:

sudo snap services

You can see more snap commands here.

Taking Anbox Logs

– Make sure you have debug logs enabled:

sudo snap set anbox debug.enable=true

– Now reproduce the error while debug logs is enabled.

– Run the Anbox logs collection utility:

sudo /snap/bin/anbox.collect-bug-info

Along the line, you will be asked if you want to include the kernel log, the choice is yours.

– The log is saved in the current working directory.

You can summit logs here but only for the latest Anbox version. You can update Anbox to the latest version using:

snap refresh --devmode --edge anbox

Solving Network Issues In Anbox

By default, Anbox should have internet connection if your machine is connected. If you’re encountering some connectivity issues on Anbox, make sure the anbox-bride.sh is running. You can start it using the first command or restart it using the second:

sudo /snap/anbox/current/bin/anbox-bridge.sh start
sudo /snap/anbox/current/bin/anbox-bridge.sh restart

You may also need to install the dnsmasq package if the connectivity issue with Anbox still prevails, according to this user.

Are There Other Ways Of Running Android Apps In Linux?

Yes there are other emulators like Android Studio, Genymotion, ARChon, etc. See this guide and this one.

Genymotion Emulator

Standalone Android OS For PC

If you want a fully functional (like the one on your phone) and standalone (just like Windows, Linux, etc) Android OS, then you can use Android-x86, Bliss OS, etc in a virtual machine, live USB/CD or in dualboot/multiboot. You can even use them as your main OS just like how you use Windows, Linux, etc. They run well and have just some minor bugs. We will leave this as our next topic which you can read in full here.

Bliss OS

Please if you like the work we are doing here at SakoPC, scroll down to the Conclusions section to support us with whatever you have.


AUTHUR


Leave a comment

Create a website or blog at WordPress.com

Discover more from SakoPC

Subscribe now to keep reading and get access to new posts once they are published.

Continue Reading

Design a site like this with WordPress.com
Get started