nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 206 lines 6.3 kB view raw
1{ 2 # To test your changes in androidEnv run `nix-shell android-sdk-with-emulator-shell.nix` 3 4 # If you copy this example out of nixpkgs, use these lines instead of the next. 5 # This example pins nixpkgs: https://nix.dev/tutorials/first-steps/towards-reproducibility-pinning-nixpkgs.html 6 /* 7 nixpkgsSource ? (fetchTarball { 8 name = "nixpkgs-20.09"; 9 url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz"; 10 sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy"; 11 }), 12 pkgs ? import nixpkgsSource { 13 config.allowUnfree = true; 14 }, 15 */ 16 17 # If you want to use the in-tree version of nixpkgs: 18 pkgs ? import ../../../../.. { 19 config.allowUnfree = true; 20 }, 21 22 # You probably need to set it to true to express consent. 23 licenseAccepted ? pkgs.callPackage ../license.nix { }, 24}: 25 26# Copy this file to your Android project. 27let 28 # If you copy this example out of nixpkgs, something like this will work: 29 /* 30 androidEnvNixpkgs = fetchTarball { 31 name = "androidenv"; 32 url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz"; 33 sha256 = "<fill me in with nix-prefetch-url --unpack>"; 34 }; 35 36 androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" { 37 inherit pkgs; 38 licenseAccepted = true; 39 }; 40 */ 41 42 inherit (pkgs) lib; 43 44 # Otherwise, just use the in-tree androidenv: 45 androidEnv = pkgs.callPackage ./.. { 46 inherit pkgs licenseAccepted; 47 }; 48 49 emulatorSupported = pkgs.stdenv.hostPlatform.isx86_64 || pkgs.stdenv.hostPlatform.isDarwin; 50 51 sdkArgs = { 52 includeSystemImages = true; 53 includeEmulator = "if-supported"; 54 55 # Accepting more licenses declaratively: 56 extraLicenses = [ 57 # Already accepted for you with the global accept_license = true or 58 # licenseAccepted = true on androidenv. 59 # "android-sdk-license" 60 61 # These aren't, but are useful for more uncommon setups. 62 "android-sdk-preview-license" 63 "android-googletv-license" 64 "android-sdk-arm-dbt-license" 65 "google-gdk-license" 66 "intel-android-extra-license" 67 "intel-android-sysimage-license" 68 "mips-android-sysimage-license" 69 ]; 70 }; 71 72 androidComposition = androidEnv.composeAndroidPackages sdkArgs; 73 androidEmulator = androidEnv.emulateApp { 74 name = "android-sdk-emulator-demo"; 75 configOptions = { 76 "hw.keyboard" = "yes"; 77 }; 78 sdkExtraArgs = sdkArgs; 79 }; 80 androidSdk = androidComposition.androidsdk; 81 platformTools = androidComposition.platform-tools; 82 latestSdkVersion = lib.foldl' ( 83 s: x: if lib.strings.compareVersions (toString x) s > 0 then toString x else s 84 ) "0" androidComposition.platformVersions; 85 jdk = pkgs.jdk; 86in 87pkgs.mkShell rec { 88 name = "androidenv-demo"; 89 packages = [ 90 androidSdk 91 platformTools 92 androidEmulator 93 jdk 94 ]; 95 96 LANG = "C.UTF-8"; 97 LC_ALL = "C.UTF-8"; 98 JAVA_HOME = jdk.home; 99 100 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT. 101 ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk"; 102 ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle"; 103 104 shellHook = '' 105 # Write out local.properties for Android Studio. 106 cat <<EOF > local.properties 107 # This file was automatically generated by nix-shell. 108 sdk.dir=$ANDROID_SDK_ROOT 109 ndk.dir=$ANDROID_NDK_ROOT 110 EOF 111 ''; 112 113 passthru.tests = { 114 115 shell-with-emulator-sdkmanager-packages-test = 116 pkgs.runCommand "shell-with-emulator-sdkmanager-packages-test" 117 { 118 nativeBuildInputs = [ 119 androidSdk 120 jdk 121 ]; 122 } 123 '' 124 output="$(sdkmanager --list)" 125 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}') 126 echo "installed_packages_section: ''${installed_packages_section}" 127 128 packages=( 129 "build-tools" "cmdline-tools" \ 130 "platform-tools" "platforms;android-${toString latestSdkVersion}" \ 131 "system-images;android-${toString latestSdkVersion};google_apis;x86_64" 132 ) 133 ${lib.optionalString emulatorSupported ''packages+=("emulator")''} 134 135 for package in "''${packages[@]}"; do 136 if [[ ! $installed_packages_section =~ "$package" ]]; then 137 echo "$package package was not installed." 138 exit 1 139 fi 140 done 141 142 touch "$out" 143 ''; 144 145 shell-with-emulator-sdkmanager-excluded-packages-test = 146 pkgs.runCommand "shell-with-emulator-sdkmanager-excluded-packages-test" 147 { 148 nativeBuildInputs = [ 149 androidSdk 150 jdk 151 ]; 152 } 153 '' 154 output="$(sdkmanager --list)" 155 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}') 156 157 excluded_packages=(ndk) 158 for x in $(seq 1 ${lib.versions.major (toString latestSdkVersion)}); do 159 excluded_packages+=( 160 "platforms;android-$x" 161 "sources;android-$x" 162 "system-images;android-$x" 163 ) 164 done 165 166 for package in "''${excluded_packages[@]}"; do 167 if [[ $installed_packages_section =~ ^"$package"$ ]]; then 168 echo "$package package was installed, while it was excluded!" 169 exit 1 170 fi 171 done 172 173 touch "$out" 174 ''; 175 176 shell-with-emulator-avdmanager-create-avd-test = 177 pkgs.runCommand "shell-with-emulator-avdmanager-create-avd-test" 178 { 179 nativeBuildInputs = [ 180 androidSdk 181 androidEmulator 182 jdk 183 ]; 184 } 185 ( 186 lib.optionalString emulatorSupported '' 187 export ANDROID_USER_HOME=$PWD/.android 188 mkdir -p $ANDROID_USER_HOME 189 190 avdmanager delete avd -n testAVD || true 191 echo "" | avdmanager create avd --force --name testAVD --package 'system-images;android-${toString latestSdkVersion};google_apis;x86_64' 192 result=$(avdmanager list avd) 193 194 if [[ ! $result =~ "Name: testAVD" ]]; then 195 echo "avdmanager couldn't create the avd! The output is :''${result}" 196 exit 1 197 fi 198 199 avdmanager delete avd -n testAVD || true 200 '' 201 + '' 202 touch $out 203 '' 204 ); 205 }; 206}