summary refs log tree commit diff
path: root/pkgs/development/androidenv/emulate-app.nix
blob: 3cbe572380664002756f7608ee1bd3e5491f4be4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{stdenv, androidsdk}:
{name, app, platformVersion ? "8", useGoogleAPIs ? false, package, activity}:

let
  androidsdkComposition = androidsdk { inherit useGoogleAPIs; platformVersions = [ platformVersion ]; };
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
        if [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb devices | grep emulator-$i)" ]
        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
    ${androidsdkComposition}/libexec/android-sdk-*/tools/android create avd -n device -t ${if useGoogleAPIs then "'Google Inc.:Google APIs:"+platformVersion+"'" else "android-"+platformVersion}
    
    # Launch the emulator
    ${androidsdkComposition}/libexec/android-sdk-*/tools/emulator -avd device -no-boot-anim -port $port &

    # Wait until the device has completely booted
    
    echo "Waiting until the emulator has booted the device and the package manager is ready..."
    
    ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port wait-for-device
    
    echo "Device state has been reached"
    
    while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ]
    do
        sleep 5
    done
    
    echo "dev.bootcomplete property is 1"
    
    #while [ -z "$(${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell getprop sys.boot_completed | grep 1)" ]
    #do
        #sleep 5
    #done
    
    #echo "sys.boot_completed property is 1"
    
    echo "ready"
    
    # Install the App through the debugger
    ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port install ${app}/*.apk
    
    # Start the application
    ${androidsdkComposition}/libexec/android-sdk-*/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/.${activity}
    EOF
    
    chmod +x $out/bin/run-test-emulator
  '';
}