2012-11-07 20:10:39 +00:00
|
|
|
{stdenv, androidsdk}:
|
2013-08-06 10:16:51 +01:00
|
|
|
{ name, app ? null
|
|
|
|
, platformVersion ? "8", abiVersion ? "armeabi-v7a", useGoogleAPIs ? false
|
|
|
|
, enableGPU ? false, extraAVDFiles ? []
|
2014-02-27 17:46:06 +00:00
|
|
|
, package ? null, activity ? null
|
|
|
|
, avdHomeDir ? null
|
|
|
|
}:
|
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"
|
2014-02-27 18:26:18 +00:00
|
|
|
#! ${stdenv.shell} -e
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
# We need a TMPDIR
|
|
|
|
if [ "$TMPDIR" = "" ]
|
|
|
|
then
|
|
|
|
export TMPDIR=/tmp
|
|
|
|
fi
|
|
|
|
|
2014-02-27 17:46:06 +00:00
|
|
|
${if avdHomeDir == null then ''
|
|
|
|
# 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)
|
|
|
|
'' else ''
|
2014-02-27 18:26:18 +00:00
|
|
|
mkdir -p "${avdHomeDir}"
|
|
|
|
export ANDROID_SDK_HOME="${avdHomeDir}"
|
2014-02-27 17:46:06 +00:00
|
|
|
''}
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
# We have to look for a free TCP port
|
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "Looking for a free TCP port in range 5554-5584" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
for i in $(seq 5554 2 5584)
|
|
|
|
do
|
2016-09-01 14:56:35 +01:00
|
|
|
if [ -z "$(${androidsdkComposition}/libexec/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
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "Unfortunately, the emulator port space is exhausted!" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
exit 1
|
|
|
|
else
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "We have a free TCP port: $port" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
export ANDROID_SERIAL="emulator-$port"
|
|
|
|
|
2014-02-27 18:26:18 +00:00
|
|
|
# Create a virtual android device for testing if it does not exists
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
if [ "$(${androidsdkComposition}/libexec/tools/android list avd | grep 'Name: device')" = "" ]
|
2014-02-27 18:26:18 +00:00
|
|
|
then
|
|
|
|
# Create a virtual android device
|
2016-09-01 14:56:35 +01:00
|
|
|
yes "" | ${androidsdkComposition}/libexec/tools/android create avd -n device -t ${if useGoogleAPIs then "'Google Inc.:Google APIs:"+platformVersion+"'" else "android-"+platformVersion} $NIX_ANDROID_AVD_FLAGS
|
2013-07-31 17:56:27 +01:00
|
|
|
|
2014-02-27 18:26:18 +00:00
|
|
|
${stdenv.lib.optionalString enableGPU ''
|
|
|
|
# Enable GPU acceleration
|
|
|
|
echo "hw.gpu.enabled=yes" >> $ANDROID_SDK_HOME/.android/avd/device.avd/config.ini
|
|
|
|
''}
|
|
|
|
|
|
|
|
${stdenv.lib.concatMapStrings (extraAVDFile: ''
|
|
|
|
ln -sf ${extraAVDFile} $ANDROID_SDK_HOME/.android/avd/device.avd
|
|
|
|
'') extraAVDFiles}
|
|
|
|
fi
|
2013-08-06 10:16:51 +01:00
|
|
|
|
2012-11-07 20:10:39 +00:00
|
|
|
# Launch the emulator
|
2016-09-01 14:56:35 +01:00
|
|
|
${androidsdkComposition}/libexec/tools/emulator -avd device -no-boot-anim -port $port $NIX_ANDROID_EMULATOR_FLAGS &
|
2012-11-07 20:10:39 +00:00
|
|
|
|
|
|
|
# Wait until the device has completely booted
|
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "Waiting until the emulator has booted the device and the package manager is ready..." >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
${androidsdkComposition}/libexec/platform-tools/adb -s emulator-$port wait-for-device
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "Device state has been reached" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
while [ -z "$(${androidsdkComposition}/libexec/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ]
|
2012-11-07 20:10:39 +00:00
|
|
|
do
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "dev.bootcomplete property is 1" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
#while [ -z "$(${androidsdkComposition}/libexec/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
|
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
#echo "sys.boot_completed property is 1" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
echo "ready" >&2
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
${stdenv.lib.optionalString (app != null) ''
|
2014-02-27 18:26:18 +00:00
|
|
|
# Install the App through the debugger, if it has not been installed yet
|
2014-01-28 18:09:08 +00:00
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
if [ -z "${package}" ] || [ "$(${androidsdkComposition}/libexec/platform-tools/adb -s emulator-$port shell pm list packages | grep package:${package})" = "" ]
|
2014-01-28 18:09:08 +00:00
|
|
|
then
|
2014-02-27 18:26:18 +00:00
|
|
|
if [ -d "${app}" ]
|
|
|
|
then
|
|
|
|
appPath="$(echo ${app}/*.apk)"
|
|
|
|
else
|
|
|
|
appPath="${app}"
|
|
|
|
fi
|
|
|
|
|
2016-09-01 14:56:35 +01:00
|
|
|
${androidsdkComposition}/libexec/platform-tools/adb -s emulator-$port install "$appPath"
|
2014-01-28 18:09:08 +00:00
|
|
|
fi
|
2012-11-07 20:10:39 +00:00
|
|
|
|
2013-08-05 18:08:58 +01:00
|
|
|
# Start the application
|
2014-02-27 17:46:06 +00:00
|
|
|
${stdenv.lib.optionalString (package != null && activity != null) ''
|
2016-09-01 14:56:35 +01:00
|
|
|
${androidsdkComposition}/libexec/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/${activity}
|
2014-02-27 17:46:06 +00:00
|
|
|
''}
|
2013-08-05 18:08:58 +01:00
|
|
|
''}
|
2012-11-07 20:10:39 +00:00
|
|
|
EOF
|
|
|
|
chmod +x $out/bin/run-test-emulator
|
|
|
|
'';
|
|
|
|
}
|