2012-11-07 20:10:39 +00:00
|
|
|
{stdenv, androidsdk}:
|
2013-07-31 17:56:27 +01:00
|
|
|
{name, app, platformVersion ? "8", abiVersion ? "armeabi-v7a", useGoogleAPIs ? false, enableGPU ? false, package, activity}:
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
let
|
2013-07-31 17:56:27 +01:00
|
|
|
androidsdkComposition = androidsdk {
|
|
|
|
inherit useGoogleAPIs;
|
|
|
|
platformVersions = [ platformVersion ];
|
|
|
|
abiVersions = [ abiVersion ];
|
|
|
|
};
|
2012-11-07 20:10:39 +00:00
|
|
|
in
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
inherit name;
|
|
|
|
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
|
|
|
cat > $out/bin/run-test-emulator << "EOF"
|
|
|
|
#!/bin/sh -e
|
|
|
|
|
|
|
|
# We need a TMPDIR
|
|
|
|
if [ "$TMPDIR" = "" ]
|
|
|
|
then
|
|
|
|
export TMPDIR=/tmp
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Store the virtual devices somewhere else, instead of polluting a user's HOME directory
|
|
|
|
export ANDROID_SDK_HOME=$(mktemp -d $TMPDIR/nix-android-vm-XXXX)
|
|
|
|
|
|
|
|
# We have to look for a free TCP port
|
|
|
|
|
|
|
|
echo "Looking for a free TCP port in range 5554-5584"
|
|
|
|
|
|
|
|
for i in $(seq 5554 2 5584)
|
|
|
|
do
|
2012-11-08 14:01:06 +00:00
|
|
|
if [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb devices | grep emulator-$i)" ]
|
2012-11-07 20:10:39 +00:00
|
|
|
then
|
|
|
|
port=$i
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$port" ]
|
|
|
|
then
|
|
|
|
echo "Unfortunately, the emulator port space is exhausted!"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "We have a free TCP port: $port"
|
|
|
|
fi
|
|
|
|
|
|
|
|
export ANDROID_SERIAL="emulator-$port"
|
|
|
|
|
|
|
|
# Create a virtual android device
|
2013-08-01 10:32:57 +01:00
|
|
|
yes "" | ${androidsdkComposition}/libexec/android-sdk-*/tools/android create avd -n device -t ${if useGoogleAPIs then "'Google Inc.:Google APIs:"+platformVersion+"'" else "android-"+platformVersion}
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2013-07-31 17:56:27 +01:00
|
|
|
# Enable GPU acceleration
|
|
|
|
${stdenv.lib.optionalString enableGPU ''
|
|
|
|
echo "hw.gpu.enabled=yes" >> $ANDROID_SDK_HOME/.android/avd/device.avd/config.ini
|
|
|
|
''}
|
|
|
|
|
2012-11-07 20:10:39 +00:00
|
|
|
# Launch the emulator
|
2012-11-08 14:01:06 +00:00
|
|
|
${androidsdkComposition}/libexec/android-sdk-*/tools/emulator -avd device -no-boot-anim -port $port &
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
# Wait until the device has completely booted
|
|
|
|
|
|
|
|
echo "Waiting until the emulator has booted the device and the package manager is ready..."
|
|
|
|
|
2012-11-08 14:01:06 +00:00
|
|
|
${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port wait-for-device
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
echo "Device state has been reached"
|
|
|
|
|
2012-11-08 14:01:06 +00:00
|
|
|
while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ]
|
2012-11-07 20:10:39 +00:00
|
|
|
do
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "dev.bootcomplete property is 1"
|
|
|
|
|
2012-11-08 14:01:06 +00:00
|
|
|
#while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop sys.boot_completed | grep 1)" ]
|
2012-11-07 20:10:39 +00:00
|
|
|
#do
|
|
|
|
#sleep 5
|
|
|
|
#done
|
|
|
|
|
|
|
|
#echo "sys.boot_completed property is 1"
|
|
|
|
|
|
|
|
echo "ready"
|
|
|
|
|
|
|
|
# Install the App through the debugger
|
2012-11-08 15:30:36 +00:00
|
|
|
${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port install ${app}/*.apk
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
# Start the application
|
2012-11-08 15:30:36 +00:00
|
|
|
${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/.${activity}
|
2012-11-07 20:10:39 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
chmod +x $out/bin/run-test-emulator
|
|
|
|
'';
|
|
|
|
}
|