How to lock cloud Android virtual devices into kiosk mode

So people can use your app from any browser

Sylvain Galand
Genymobile
Published in
4 min readMar 20, 2017

--

Android application in a browser

Genymobile recently launched Genymotion on-demand 2.0 on AWS Marketplace. This new version allows you to put any Android EC2 instance in “Kiosk” mode (i.e. restricting the virtual device to just one app). This can allow you to let people test or try your app online and I will explain you how to do that.

TL;DR

  • Start a virtual device from Genymotion On Demand image on Amazon
  • Customize this device with your app in a kiosk mode
  • Share this app through the web to anyone with a browser
F-Droid Android application in a browser.

Requirements

  • Python 3 ready environment
  • An Amazon EC2 account (with a valid cert.pem)
  • Boto3 — Python SDK for Amazon Web Services (AWS) supporting EC2
  • Android ready environment with adb installed

I used Python through this tutorial but you can use the language you want or even the CLI provided by Amazon.

Set up an Amazon Machine Image with an application in kiosk mode

Create the security group

We need to allow some traffic in order to have everything working, this script will create the security group and print its ID.

import boto3ec2 = boto3.resource('ec2')# Create a new security group
sg = ec2.create_security_group(
GroupName='genymotion-security-group',
Description='Security group for Genymotion AWS'
)
# Allow SSH
sg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=22, ToPort=22)
# Allow HTTPS
sg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=443, ToPort=443)
# Allow HTTP
sg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=80, ToPort=80)
# Allow WebRTC
sg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=32768, ToPort=61000)
sg.authorize_ingress(IpProtocol="udp", CidrIp="0.0.0.0/0", FromPort=32768, ToPort=61000)
print('Security group created with id: ' + sg.id)

Once you have the security ID, keep it somewhere, you will need it later.

Launch a Genymotion instance

This step will create and launch a Genymotion instance. From this we are going to install the APK and set it up in kiosk mode.

import boto3key_name = '<private-amazon-key-name>'
security_group_id = '<security-group-id>'
genymotion_2_0_ami_id = 'ami-94b7ebf4' # Android 5.1
ec2 = boto3.resource('ec2')# Launch the instance
print("Launching the instance")
instances = ec2.create_instances(
ImageId=genymotion_2_0_ami_id,
MinCount=1,
MaxCount=1,
KeyName=key_name,
SecurityGroupIds=[security_group_id],
InstanceType='t2.medium' # Minimum configuration for Genymotion
)
# Retrieve the instance we just launched.
instance = instances[0]
#Wait for instance to be running
print("Waiting for instance to be fully running...")
instance.wait_until_running()
instance.reload() # In order to get the public DNS name
print("Instance " + instance.id + " running : " + instance.public_dns_name)

Keep the instance ID and the public DNS name.

Setting up the instance

Now that the instance has been launched, it’s time to set it up. The prerequisites are disabling HTTP authentication, removing Genymotion’s side bar, activating adb and — at last — locking your application in kiosk mode. For that, you will need the .pem used to create the instance.

First, let’s connect to the instance via SSH:

$ ssh -i <path-to-key.pem> root@<instance-public-dns>

Configure the instance

# Deactivate HTTP auth
$ setprop persist.webrtcd.authent off
# Remove Genymotion sidebar
$ /sbin/busybox sed -i -e 's/default/simple/g' /data/www/index.html
# Activate adb
$ setprop persist.sys.usb.config adb
# That's it for now so we can exit
$ exit

Now we can tunnel the adb connection via SSH to the local host

# Make adb available locally by tunneling the connection
$ ssh -i <path-to-key.pem> -NL 5555:localhost:5555 root@<instance-public-dns>

We can now install, launch the app and lock it into kiosk mode

# install the app
$ adb install <path-to-your-apk>
# Launch the app
$ adb shell am start -n <your-package-name>/.<activity-name>
# Put the activity on top of the stack in kiosk mode
$ adb shell cmd activity kiosk start

You can now stop the SSH tunnel and this is it!

You now have an Android instance, accessible from the cloud, locked into a single application.

Now if you don’t want to do all of this again, it’s possible to create an image from this instance.

Create an image from an instance already set

import boto3instance_id = '<instance-id>'ec2 = boto3.resource('ec2')image = ec2.Instance(id=instance_id).create_image(
Name='My App in Kiosk mode',
Description='Image of my app in kiosk mode',
NoReboot=True
)
print("Image has been created: " + image.id)

Now you can just use this image ID to launch an instance as seen earlier in this article to popup Genymotion with your application locked into kiosk mode.

What now?

The kiosk mode on an application ensures the users will use your app and only your app, but above all, this is a great solution if you want to share an app with coworkers to have feedback or to allow your users to test your app directly on your website. This feature brought by the new version of Genymotion for AWS has tons of possibilities. Up to you to find an usage that fits with your business — and there sure are many.

Thanks for reading! This was my first Medium story, hope you liked it. Let me know your thoughts in comments or on Twitter. I’ll be happy to hear them 🙂

Liked what you saw? Hit that ❤ and follow me 🤓

Give Genymotion a shout on Twitter, Facebook and Google+

--

--