Merge pull request #66747 from erikarvstedt/androidenv-fixes

Androidenv fixes

authored by Matthew Bauer and committed by GitHub 5be571ee e04a39d5

+61 -66
+4 -5
doc/languages-frameworks/android.section.md
··· 185 185 186 186 androidenv.emulateApp { 187 187 name = "emulate-MyAndroidApp"; 188 - platformVersion = "24"; 189 - abiVersion = "armeabi-v7a"; # mips, x86 or x86_64 190 - systemImageType = "default"; 191 - useGoogleAPIs = false; 188 + platformVersion = "28"; 189 + abiVersion = "x86_64"; # armeabi-v7a, mips, x86 190 + systemImageType = "google_apis_playstore"; 192 191 } 193 192 ``` 194 193 ··· 201 200 androidenv.emulateApp { 202 201 name = "emulate-MyAndroidApp"; 203 202 platformVersion = "24"; 204 - abiVersion = "armeabi-v7a"; # mips, x86 or x86_64 203 + abiVersion = "armeabi-v7a"; # mips, x86, x86_64 205 204 systemImageType = "default"; 206 205 useGoogleAPIs = false; 207 206 app = ./MyApp.apk;
+4 -4
pkgs/development/mobile/androidenv/build-app.nix
··· 1 - { composeAndroidPackages, stdenv, ant, jdk, gnumake, gawk }: 1 + { composeAndroidPackages, stdenv, lib, ant, jdk, gnumake, gawk }: 2 2 3 3 { name 4 4 , release ? false, keyStore ? null, keyAlias ? null, keyStorePassword ? null, keyAliasPassword ? null ··· 16 16 extraArgs = removeAttrs args ([ "name" ] ++ builtins.attrNames androidSdkFormalArgs); 17 17 in 18 18 stdenv.mkDerivation ({ 19 - name = stdenv.lib.replaceChars [" "] [""] name; # Android APKs may contain white spaces in their names, but Nix store paths cannot 19 + name = lib.replaceChars [" "] [""] name; # Android APKs may contain white spaces in their names, but Nix store paths cannot 20 20 ANDROID_HOME = "${androidsdk}/libexec/android-sdk"; 21 21 buildInputs = [ jdk ant ]; 22 22 buildPhase = '' 23 - ${stdenv.lib.optionalString release '' 23 + ${lib.optionalString release '' 24 24 # Provide key singing attributes 25 25 ( echo "key.store=${keyStore}" 26 26 echo "key.alias=${keyAlias}" ··· 31 31 32 32 export ANDROID_SDK_HOME=`pwd` # Key files cannot be stored in the user's home directory. This overrides it. 33 33 34 - ${stdenv.lib.optionalString (args ? includeNDK && args.includeNDK) '' 34 + ${lib.optionalString (args ? includeNDK && args.includeNDK) '' 35 35 export GNUMAKE=${gnumake}/bin/make 36 36 export NDK_HOST_AWK=${gawk}/bin/gawk 37 37 ${androidsdk}/libexec/android-sdk/ndk-bundle/ndk-build
+28 -29
pkgs/development/mobile/androidenv/compose-android-packages.nix
··· 21 21 }: 22 22 23 23 let 24 - inherit (pkgs) stdenv fetchurl makeWrapper unzip; 24 + inherit (pkgs) stdenv lib fetchurl makeWrapper unzip; 25 25 26 26 # Determine the Android os identifier from Nix's system identifier 27 27 os = if stdenv.system == "x86_64-linux" then "linux" ··· 59 59 }; 60 60 61 61 system-images-packages = 62 - stdenv.lib.recursiveUpdate 62 + lib.recursiveUpdate 63 63 system-images-packages-android 64 - (stdenv.lib.recursiveUpdate system-images-packages-android-tv 65 - (stdenv.lib.recursiveUpdate system-images-packages-android-wear 66 - (stdenv.lib.recursiveUpdate system-images-packages-android-wear-cn 67 - (stdenv.lib.recursiveUpdate system-images-packages-google_apis system-images-packages-google_apis_playstore)))); 64 + (lib.recursiveUpdate system-images-packages-android-tv 65 + (lib.recursiveUpdate system-images-packages-android-wear 66 + (lib.recursiveUpdate system-images-packages-android-wear-cn 67 + (lib.recursiveUpdate system-images-packages-google_apis system-images-packages-google_apis_playstore)))); 68 68 69 69 # Generated addons 70 70 addons = import ./generated/addons.nix { ··· 77 77 }; 78 78 79 79 platform-tools = import ./platform-tools.nix { 80 - inherit deployAndroidPackage os autoPatchelfHook pkgs; 81 - inherit (stdenv) lib; 80 + inherit deployAndroidPackage os autoPatchelfHook pkgs lib; 82 81 package = packages.platform-tools."${platformToolsVersion}"; 83 82 }; 84 83 85 84 build-tools = map (version: 86 85 import ./build-tools.nix { 87 - inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs pkgs_i686; 88 - inherit (stdenv) lib; 86 + inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs pkgs_i686 lib; 89 87 package = packages.build-tools."${version}"; 90 88 } 91 89 ) buildToolsVersions; ··· 96 94 }; 97 95 98 96 emulator = import ./emulator.nix { 99 - inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs pkgs_i686; 100 - inherit (stdenv) lib; 97 + inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs pkgs_i686 lib; 101 98 package = packages.emulator."${emulatorVersion}"."${os}"; 102 99 }; 103 100 ··· 115 112 } 116 113 ) platformVersions; 117 114 118 - system-images = stdenv.lib.flatten (map (apiVersion: 115 + system-images = lib.flatten (map (apiVersion: 119 116 map (type: 120 117 map (abiVersion: 121 118 deployAndroidPackage { 122 119 inherit os; 123 120 package = system-images-packages.${apiVersion}.${type}.${abiVersion}; 121 + # Patch 'google_apis' system images so they're recognized by the sdk. 122 + # Without this, `android list targets` shows 'Tag/ABIs : no ABIs' instead 123 + # of 'Tag/ABIs : google_apis*/*' and the emulator fails with an ABI-related error. 124 + patchInstructions = lib.optionalString (lib.hasPrefix "google_apis" type) '' 125 + sed -i '/^Addon.Vendor/d' source.properties 126 + ''; 124 127 } 125 128 ) abiVersions 126 129 ) systemImageTypes ··· 128 131 129 132 lldb = map (version: 130 133 import ./lldb.nix { 131 - inherit deployAndroidPackage os autoPatchelfHook pkgs; 132 - inherit (stdenv) lib; 134 + inherit deployAndroidPackage os autoPatchelfHook pkgs lib; 133 135 package = packages.lldb."${version}"; 134 136 } 135 137 ) lldbVersions; 136 138 137 139 cmake = map (version: 138 140 import ./cmake.nix { 139 - inherit deployAndroidPackage os autoPatchelfHook pkgs; 140 - inherit (stdenv) lib; 141 + inherit deployAndroidPackage os autoPatchelfHook pkgs lib; 141 142 package = packages.cmake."${version}"; 142 143 } 143 144 ) cmakeVersions; 144 145 145 146 ndk-bundle = import ./ndk-bundle { 146 - inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs platform-tools; 147 - inherit (stdenv) lib; 147 + inherit deployAndroidPackage os autoPatchelfHook makeWrapper pkgs lib platform-tools; 148 148 package = packages.ndk-bundle."${ndkVersion}"; 149 149 }; 150 150 ··· 164 164 165 165 # Function that automatically links all plugins for which multiple versions can coexist 166 166 linkPlugins = {name, plugins}: 167 - stdenv.lib.optionalString (plugins != []) '' 167 + lib.optionalString (plugins != []) '' 168 168 mkdir -p ${name} 169 - ${stdenv.lib.concatMapStrings (plugin: '' 169 + ${lib.concatMapStrings (plugin: '' 170 170 ln -s ${plugin}/libexec/android-sdk/${name}/* ${name} 171 171 '') plugins} 172 172 ''; 173 173 174 174 # Function that automatically links a plugin for which only one version exists 175 175 linkPlugin = {name, plugin, check ? true}: 176 - stdenv.lib.optionalString check '' 176 + lib.optionalString check '' 177 177 ln -s ${plugin}/libexec/android-sdk/* ${name} 178 178 ''; 179 179 180 180 # Links all plugins related to a requested platform 181 181 linkPlatformPlugins = {name, plugins, check}: 182 - stdenv.lib.optionalString check '' 182 + lib.optionalString check '' 183 183 mkdir -p ${name} 184 - ${stdenv.lib.concatMapStrings (plugin: '' 184 + ${lib.concatMapStrings (plugin: '' 185 185 ln -s ${plugin}/libexec/android-sdk/${name}/* ${name} 186 186 '') plugins} 187 187 ''; # */ ··· 194 194 https://developer.android.com/studio/terms 195 195 by setting nixpkgs config option 'android_sdk.accept_license = true;' 196 196 '' else import ./tools.nix { 197 - inherit deployAndroidPackage requireFile packages toolsVersion autoPatchelfHook makeWrapper os pkgs pkgs_i686; 198 - inherit (stdenv) lib; 197 + inherit deployAndroidPackage requireFile packages toolsVersion autoPatchelfHook makeWrapper os pkgs pkgs_i686 lib; 199 198 200 199 postInstall = '' 201 200 # Symlink all requested plugins ··· 210 209 ${linkPlugins { name = "cmake"; plugins = cmake; }} 211 210 ${linkPlugin { name = "ndk-bundle"; plugin = ndk-bundle; check = includeNDK; }} 212 211 213 - ${stdenv.lib.optionalString includeSystemImages '' 212 + ${lib.optionalString includeSystemImages '' 214 213 mkdir -p system-images 215 - ${stdenv.lib.concatMapStrings (system-image: '' 214 + ${lib.concatMapStrings (system-image: '' 216 215 apiVersion=$(basename $(echo ${system-image}/libexec/android-sdk/system-images/*)) 217 216 type=$(basename $(echo ${system-image}/libexec/android-sdk/system-images/*/*)) 218 217 mkdir -p system-images/$apiVersion/$type ··· 224 223 ${linkPlatformPlugins { name = "add-ons"; plugins = google-apis; check = useGoogleTVAddOns; }} 225 224 226 225 # Link extras 227 - ${stdenv.lib.concatMapStrings (identifier: 226 + ${lib.concatMapStrings (identifier: 228 227 let 229 228 path = addons.extras."${identifier}".path; 230 229 addon = deployAndroidPackage {
+2 -2
pkgs/development/mobile/androidenv/default.nix
··· 10 10 }; 11 11 12 12 buildApp = import ./build-app.nix { 13 - inherit (pkgs) stdenv jdk ant gnumake gawk; 13 + inherit (pkgs) stdenv lib jdk ant gnumake gawk; 14 14 inherit composeAndroidPackages; 15 15 }; 16 16 17 17 emulateApp = import ./emulate-app.nix { 18 - inherit (pkgs) stdenv; 18 + inherit (pkgs) stdenv lib; 19 19 inherit composeAndroidPackages; 20 20 }; 21 21
+23 -26
pkgs/development/mobile/androidenv/emulate-app.nix
··· 1 - { composeAndroidPackages, stdenv }: 1 + { composeAndroidPackages, stdenv, lib }: 2 2 { name, app ? null 3 - , platformVersion ? "16", abiVersion ? "armeabi-v7a", systemImageType ? "default", useGoogleAPIs ? false 3 + , platformVersion ? "16", abiVersion ? "armeabi-v7a", systemImageType ? "default" 4 4 , enableGPU ? false, extraAVDFiles ? [] 5 5 , package ? null, activity ? null 6 - , avdHomeDir ? null 7 - }@args: 6 + , avdHomeDir ? null, sdkExtraArgs ? {} 7 + }: 8 8 9 9 let 10 - androidSdkArgNames = builtins.attrNames (builtins.functionArgs composeAndroidPackages); 11 - 12 - # Extract the parameters meant for the Android SDK 13 - androidParams = { 10 + sdkArgs = { 14 11 platformVersions = [ platformVersion ]; 15 12 includeEmulator = true; 16 13 includeSystemImages = true; 17 14 systemImageTypes = [ systemImageType ]; 18 15 abiVersions = [ abiVersion ]; 19 - }; 16 + } // sdkExtraArgs; 20 17 21 - androidsdkComposition = (composeAndroidPackages androidParams).androidsdk; 18 + sdk = (composeAndroidPackages sdkArgs).androidsdk; 22 19 in 23 20 stdenv.mkDerivation { 24 21 inherit name; ··· 44 41 ''} 45 42 46 43 # We need to specify the location of the Android SDK root folder 47 - export ANDROID_SDK_ROOT=${androidsdkComposition}/libexec/android-sdk 44 + export ANDROID_SDK_ROOT=${sdk}/libexec/android-sdk 48 45 49 46 # We have to look for a free TCP port 50 47 ··· 52 49 53 50 for i in $(seq 5554 2 5584) 54 51 do 55 - if [ -z "$(${androidsdkComposition}/libexec/android-sdk/platform-tools/adb devices | grep emulator-$i)" ] 52 + if [ -z "$(${sdk}/libexec/android-sdk/platform-tools/adb devices | grep emulator-$i)" ] 56 53 then 57 54 port=$i 58 55 break ··· 70 67 export ANDROID_SERIAL="emulator-$port" 71 68 72 69 # Create a virtual android device for testing if it does not exists 73 - ${androidsdkComposition}/libexec/android-sdk/tools/android list targets 70 + ${sdk}/libexec/android-sdk/tools/android list targets 74 71 75 - if [ "$(${androidsdkComposition}/libexec/android-sdk/tools/android list avd | grep 'Name: device')" = "" ] 72 + if [ "$(${sdk}/libexec/android-sdk/tools/android list avd | grep 'Name: device')" = "" ] 76 73 then 77 74 # Create a virtual android device 78 - yes "" | ${androidsdkComposition}/libexec/android-sdk/tools/android create avd -n device -t 1 --abi ${systemImageType}/${abiVersion} $NIX_ANDROID_AVD_FLAGS 75 + yes "" | ${sdk}/libexec/android-sdk/tools/android create avd -n device -t 1 --abi ${systemImageType}/${abiVersion} $NIX_ANDROID_AVD_FLAGS 79 76 80 - ${stdenv.lib.optionalString enableGPU '' 77 + ${lib.optionalString enableGPU '' 81 78 # Enable GPU acceleration 82 79 echo "hw.gpu.enabled=yes" >> $ANDROID_SDK_HOME/.android/avd/device.avd/config.ini 83 80 ''} 84 81 85 - ${stdenv.lib.concatMapStrings (extraAVDFile: '' 82 + ${lib.concatMapStrings (extraAVDFile: '' 86 83 ln -sf ${extraAVDFile} $ANDROID_SDK_HOME/.android/avd/device.avd 87 84 '') extraAVDFiles} 88 85 fi 89 86 90 87 # Launch the emulator 91 - ${androidsdkComposition}/libexec/android-sdk/emulator/emulator -avd device -no-boot-anim -port $port $NIX_ANDROID_EMULATOR_FLAGS & 88 + ${sdk}/libexec/android-sdk/emulator/emulator -avd device -no-boot-anim -port $port $NIX_ANDROID_EMULATOR_FLAGS & 92 89 93 90 # Wait until the device has completely booted 94 91 echo "Waiting until the emulator has booted the device and the package manager is ready..." >&2 95 92 96 - ${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port wait-for-device 93 + ${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port wait-for-device 97 94 98 95 echo "Device state has been reached" >&2 99 96 100 - while [ -z "$(${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ] 97 + while [ -z "$(${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell getprop dev.bootcomplete | grep 1)" ] 101 98 do 102 99 sleep 5 103 100 done 104 101 105 102 echo "dev.bootcomplete property is 1" >&2 106 103 107 - #while [ -z "$(${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell getprop sys.boot_completed | grep 1)" ] 104 + #while [ -z "$(${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell getprop sys.boot_completed | grep 1)" ] 108 105 #do 109 106 #sleep 5 110 107 #done ··· 113 110 114 111 echo "ready" >&2 115 112 116 - ${stdenv.lib.optionalString (app != null) '' 113 + ${lib.optionalString (app != null) '' 117 114 # Install the App through the debugger, if it has not been installed yet 118 115 119 - if [ -z "${package}" ] || [ "$(${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell pm list packages | grep package:${package})" = "" ] 116 + if [ -z "${package}" ] || [ "$(${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell pm list packages | grep package:${package})" = "" ] 120 117 then 121 118 if [ -d "${app}" ] 122 119 then ··· 125 122 appPath="${app}" 126 123 fi 127 124 128 - ${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port install "$appPath" 125 + ${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port install "$appPath" 129 126 fi 130 127 131 128 # Start the application 132 - ${stdenv.lib.optionalString (package != null && activity != null) '' 133 - ${androidsdkComposition}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/${activity} 129 + ${lib.optionalString (package != null && activity != null) '' 130 + ${sdk}/libexec/android-sdk/platform-tools/adb -s emulator-$port shell am start -a android.intent.action.MAIN -n ${package}/${activity} 134 131 ''} 135 132 ''} 136 133 EOF