Created
August 14, 2018 19:42
-
-
Save atyachin/6d653b5e57794076aedeeb75ce5c41a0 to your computer and use it in GitHub Desktop.
Android Emulator as a system service (systemd)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------------------------- | |
/etc/systemd/system/android-emulator.service: | |
--------------------------------------------- | |
[Unit] | |
Description=Android Emulator | |
After=network.target | |
[Service] | |
Type=simple | |
Environment=SHELL=/bin/bash | |
WorkingDirectory=/opt/android-sdk/emulator/ | |
ExecStart=/home/ubuntu/start-android-emulator.sh | |
ExecStartPost=/home/ubuntu/myapp-start.sh | |
ExecStop=/home/ubuntu/myapp-stop.sh | |
User=ubuntu | |
TimeoutStartSec=3600 | |
TimeoutStopSec=60 | |
Restart=on-failure | |
LimitNOFILE=65536 | |
[Install] | |
WantedBy=multi-user.target | |
--------------------------------------- | |
/home/ubuntu/start-android-emulator.sh: | |
--------------------------------------- | |
#!/bin/bash | |
echo "Starting Android Emulator on port 5554" | |
/opt/android-sdk/emulator/emulator -ports 5554,5555 -avd APKServer -no-window -no-audio -gpu swiftshader_indirect -show-kernel | |
---------------------------- | |
/home/ubuntu/myapp-start.sh: | |
---------------------------- | |
#!/bin/bash | |
# wait for emulator boot to finish | |
echo "Waiting for emulator to finish loading..." | |
/opt/android-sdk/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]] ; do sleep 1; done' | |
# start your application | |
echo "Starting My App" | |
/opt/android-sdk/platform-tools/adb shell am start -n com.package.name/com.package.name.ActivityName | |
# forward ports (required in case MyApp is a localhost server) | |
port="12345" | |
echo "Forwarding ports" | |
/opt/android-sdk/platform-tools/adb forward tcp:$port tcp:$port | |
echo "My App is ready and listening on port $port" | |
--------------------------- | |
/home/ubuntu/myapp-stop.sh: | |
--------------------------- | |
#!/bin/bash | |
SERVICE="/opt/android-sdk/emulator/emulator" | |
if pgrep -f $SERVICE >/dev/null 2>&1 ; then | |
echo "Android Emulator is running" | |
else | |
echo "My App is already stopped." | |
exit 0 | |
fi | |
echo "Sending kill command to the Android Emulator" | |
/opt/android-sdk/platform-tools/adb -s emulator-5554 emu kill | |
echo "My App is now stopped." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment