at master 6.4 kB view raw
1{ 2 # If you copy this example out of nixpkgs, use these lines instead of the next. 3 # This example pins nixpkgs: https://nix.dev/tutorials/first-steps/towards-reproducibility-pinning-nixpkgs.html 4 /* 5 nixpkgsSource ? (builtins.fetchTarball { 6 name = "nixpkgs-20.09"; 7 url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz"; 8 sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy"; 9 }), 10 pkgs ? import nixpkgsSource { 11 config.allowUnfree = true; 12 }, 13 */ 14 15 # If you want to use the in-tree version of nixpkgs: 16 pkgs ? import ../../../../.. { 17 config.allowUnfree = true; 18 }, 19 20 # You probably need to set it to true to express consent. 21 licenseAccepted ? pkgs.callPackage ../license.nix { }, 22}: 23 24# Copy this file to your Android project. 25let 26 # If you copy this example out of nixpkgs, something like this will work: 27 /* 28 androidEnvNixpkgs = fetchTarball { 29 name = "androidenv"; 30 url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz"; 31 sha256 = "<fill me in with nix-prefetch-url --unpack>"; 32 }; 33 34 androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" { 35 inherit pkgs; 36 licenseAccepted = true; 37 }; 38 */ 39 40 # Otherwise, just use the in-tree androidenv: 41 androidEnv = pkgs.callPackage ./.. { 42 inherit pkgs licenseAccepted; 43 }; 44 45 # The head unit only works on these platforms 46 includeAuto = pkgs.stdenv.hostPlatform.isx86_64 || pkgs.stdenv.hostPlatform.isDarwin; 47 48 ndkVersions = [ 49 "23.1.7779620" 50 "25.1.8937393" 51 "26.1.10909125" 52 "latest" 53 ]; 54 55 androidComposition = androidEnv.composeAndroidPackages { 56 includeSources = true; 57 includeSystemImages = false; 58 includeEmulator = "if-supported"; 59 includeNDK = "if-supported"; 60 inherit ndkVersions; 61 useGoogleAPIs = true; 62 useGoogleTVAddOns = true; 63 64 # Make sure everything from the last decade works since we are not using system images. 65 numLatestPlatformVersions = 10; 66 67 # If you want to use a custom repo JSON: 68 # repoJson = ../repo.json; 69 70 # If you want to use custom repo XMLs: 71 /* 72 repoXmls = { 73 packages = [ ../xml/repository2-1.xml ]; 74 images = [ 75 ../xml/android-sys-img2-1.xml 76 ../xml/android-tv-sys-img2-1.xml 77 ../xml/android-wear-sys-img2-1.xml 78 ../xml/android-wear-cn-sys-img2-1.xml 79 ../xml/google_apis-sys-img2-1.xml 80 ../xml/google_apis_playstore-sys-img2-1.xml 81 ]; 82 addons = [ ../xml/addon2-1.xml ]; 83 }; 84 */ 85 86 includeExtras = [ 87 "extras;google;gcm" 88 ] 89 ++ pkgs.lib.optionals includeAuto [ 90 "extras;google;auto" 91 ]; 92 93 # Accepting more licenses declaratively: 94 extraLicenses = [ 95 # Already accepted for you with the global accept_license = true or 96 # licenseAccepted = true on androidenv. 97 # "android-sdk-license" 98 99 # These aren't, but are useful for more uncommon setups. 100 "android-sdk-preview-license" 101 "android-googletv-license" 102 "android-sdk-arm-dbt-license" 103 "google-gdk-license" 104 "intel-android-extra-license" 105 "intel-android-sysimage-license" 106 "mips-android-sysimage-license" 107 ]; 108 }; 109 110 androidSdk = androidComposition.androidsdk; 111 platformTools = androidComposition.platform-tools; 112 firstSdk = pkgs.lib.foldl' pkgs.lib.min 100 androidComposition.platformVersions; 113 latestSdk = pkgs.lib.foldl' pkgs.lib.max 0 androidComposition.platformVersions; 114 jdk = pkgs.jdk; 115in 116pkgs.mkShell rec { 117 name = "androidenv-demo"; 118 packages = [ 119 androidSdk 120 platformTools 121 jdk 122 ]; 123 124 LANG = "C.UTF-8"; 125 LC_ALL = "C.UTF-8"; 126 JAVA_HOME = jdk.home; 127 128 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT. 129 ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk"; 130 ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle"; 131 132 shellHook = '' 133 # Ensures that we don't have to use a FHS env by using the nix store's aapt2. 134 export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$(echo "$ANDROID_SDK_ROOT/build-tools/"*"/aapt2")" 135 136 # Add cmake to the path. 137 cmake_root="$(echo "$ANDROID_SDK_ROOT/cmake/"*/)" 138 export PATH="$cmake_root/bin:$PATH" 139 140 # Write out local.properties for Android Studio. 141 cat <<EOF > local.properties 142 # This file was automatically generated by nix-shell. 143 sdk.dir=$ANDROID_SDK_ROOT 144 ndk.dir=$ANDROID_NDK_ROOT 145 cmake.dir=$cmake_root 146 EOF 147 ''; 148 149 passthru.tests = { 150 151 shell-sdkmanager-licenses-test = 152 pkgs.runCommand "shell-sdkmanager-licenses-test" 153 { 154 nativeBuildInputs = [ 155 androidSdk 156 jdk 157 ]; 158 } 159 '' 160 if [[ ! "$(sdkmanager --licenses)" =~ "All SDK package licenses accepted." ]]; then 161 echo "At least one of SDK package licenses are not accepted." 162 exit 1 163 fi 164 touch $out 165 ''; 166 167 shell-sdkmanager-packages-test = 168 pkgs.runCommand "shell-sdkmanager-packages-test" 169 { 170 nativeBuildInputs = [ 171 androidSdk 172 jdk 173 ]; 174 } 175 '' 176 output="$(sdkmanager --list)" 177 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}') 178 179 packages=( 180 "build-tools" "platform-tools" \ 181 "extras;google;gcm" 182 ) 183 184 for x in $(seq ${toString firstSdk} ${toString latestSdk}); do 185 if [ $x -ne 34 ]; then 186 # FIXME couldn't find platforms;android-34, even though it's in the correct directory!! sdkmanager's bug?! 187 packages+=("platforms;android-$x") 188 fi 189 packages+=("sources;android-$x") 190 done 191 192 ${pkgs.lib.optionalString includeAuto ''packages+=("extras;google;auto")''} 193 194 for package in "''${packages[@]}"; do 195 if [[ ! $installed_packages_section =~ "$package" ]]; then 196 echo "$package package was not installed." 197 exit 1 198 fi 199 done 200 201 num_ndk_packages="$(echo "$installed_packages_section" | grep '^ndk;' | wc -l)" 202 if [ $num_ndk_packages -ne ${toString (pkgs.lib.length ndkVersions)} ]; then 203 echo "Invalid NDK package count: $num_ndk_packages" 204 exit 1 205 fi 206 207 touch "$out" 208 ''; 209 }; 210}