about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/mobile/androidenv/examples/shell-with-emulator.nix
blob: 4cee79824a6be39944c3328e5d4b1c9a15352181 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{
  # To test your changes in androidEnv run `nix-shell android-sdk-with-emulator-shell.nix`

  # If you copy this example out of nixpkgs, use these lines instead of the next.
  # This example pins nixpkgs: https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html
  /*nixpkgsSource ? (builtins.fetchTarball {
    name = "nixpkgs-20.09";
    url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz";
    sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy";
  }),
  pkgs ? import nixpkgsSource {
    config.allowUnfree = true;
  },
  */

  # If you want to use the in-tree version of nixpkgs:
  pkgs ? import ../../../../.. {
    config.allowUnfree = true;
  },

  config ? pkgs.config
}:

# Copy this file to your Android project.
let
  # Declaration of versions for everything. This is useful since these
  # versions may be used in multiple places in this Nix expression.
  android = {
    platforms = [ "34" ];
    systemImageTypes = [ "google_apis" ];
    abis = [ "arm64-v8a" "x86_64" ];
  };

  # If you copy this example out of nixpkgs, something like this will work:
  /*androidEnvNixpkgs = fetchTarball {
    name = "androidenv";
    url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz";
    sha256 = "<fill me in with nix-prefetch-url --unpack>";
  };

  androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" {
    inherit config pkgs;
    licenseAccepted = true;
  };*/

  # Otherwise, just use the in-tree androidenv:
  androidEnv = pkgs.callPackage ./.. {
    inherit config pkgs;
    # You probably need to uncomment below line to express consent.
    # licenseAccepted = true;
  };

  sdkArgs = {
    platformVersions = android.platforms;
    abiVersions = android.abis;
    systemImageTypes = android.systemImageTypes;

    includeSystemImages = true;
    includeEmulator = true;

    # Accepting more licenses declaratively:
    extraLicenses = [
      # Already accepted for you with the global accept_license = true or
      # licenseAccepted = true on androidenv.
      # "android-sdk-license"

      # These aren't, but are useful for more uncommon setups.
      "android-sdk-preview-license"
      "android-googletv-license"
      "android-sdk-arm-dbt-license"
      "google-gdk-license"
      "intel-android-extra-license"
      "intel-android-sysimage-license"
      "mips-android-sysimage-license"
    ];
  };

  androidComposition = androidEnv.composeAndroidPackages sdkArgs;
  androidEmulator = androidEnv.emulateApp {
    name = "android-sdk-emulator-demo";
    sdkExtraArgs = sdkArgs;
  };
  androidSdk = androidComposition.androidsdk;
  platformTools = androidComposition.platform-tools;
  jdk = pkgs.jdk;
in
pkgs.mkShell rec {
  name = "androidenv-demo";
  packages = [ androidSdk platformTools androidEmulator jdk pkgs.android-studio ];

  LANG = "C.UTF-8";
  LC_ALL = "C.UTF-8";
  JAVA_HOME = jdk.home;

  # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
  ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
  ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle";

  shellHook = ''
    # Write out local.properties for Android Studio.
    cat <<EOF > local.properties
    # This file was automatically generated by nix-shell.
    sdk.dir=$ANDROID_SDK_ROOT
    ndk.dir=$ANDROID_NDK_ROOT
    EOF
  '';

  passthru.tests = {

    shell-with-emulator-sdkmanager-packages-test = pkgs.runCommand "shell-with-emulator-sdkmanager-packages-test" {
      nativeBuildInputs = [ androidSdk jdk ];
    } ''
      output="$(sdkmanager --list)"
      installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')
      echo "installed_packages_section: ''${installed_packages_section}"

      packages=(
        "build-tools;34.0.0" "cmdline-tools;11.0" \
        "emulator" "patcher;v4" "platform-tools" "platforms;android-34" \
        "system-images;android-34;google_apis;arm64-v8a" \
        "system-images;android-34;google_apis;x86_64"
      )

      for package in "''${packages[@]}"; do
        if [[ ! $installed_packages_section =~ "$package" ]]; then
          echo "$package package was not installed."
          exit 1
        fi
      done

      touch "$out"
    '';

    shell-with-emulator-avdmanager-create-avd-test = pkgs.runCommand "shell-with-emulator-avdmanager-create-avd-test" {
      nativeBuildInputs = [ androidSdk androidEmulator jdk ];
    } ''
      avdmanager delete avd -n testAVD || true
      echo "" | avdmanager create avd --force --name testAVD --package 'system-images;android-34;google_apis;x86_64'
      result=$(avdmanager list avd)

      if [[ ! $result =~ "Name: testAVD" ]]; then
        echo "avdmanager couldn't create the avd! The output is :''${result}"
        exit 1
      fi

      avdmanager delete avd -n testAVD || true
      touch "$out"
    '';
  };
}