add comments
[tmp/julm/android.git] / flake.nix
1 {
2 # UsageNote: to run the emulator
3 # nix -L run .#emulate-android-x86_64
4 #
5 # UsageNote: to run load gradle into $PATH
6 # nix -L develop
7 description = "Nix flake to develop for Android";
8
9 inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
10 inputs.flake-utils.url = "github:numtide/flake-utils";
11
12 # ExplanationNote: an alternative way to install Android tools,
13 # not sure if it's better than nixpkgs' machinery at anything.
14 # Just here for reference.
15 inputs.android-nixpkgs = {
16 url = "github:tadfisher/android-nixpkgs";
17
18 # ExplanationNote:
19 # The main branch follows the "canary" channel of the Android SDK
20 # repository. Use another android-nixpkgs branch to explicitly
21 # track an SDK release channel.
22 #
23 # url = "github:tadfisher/android-nixpkgs/stable";
24 # url = "github:tadfisher/android-nixpkgs/beta";
25 # url = "github:tadfisher/android-nixpkgs/preview";
26 # url = "github:tadfisher/android-nixpkgs/canary";
27
28 # ExplanationNote: replace the "nixpkgs" input for the "android-nixpkgs" flake.
29 inputs.nixpkgs.follows = "nixpkgs";
30 };
31
32 outputs =
33 inputs:
34 let
35 systems = [
36 "x86_64-linux"
37 "x86_64-darwin"
38 "aarch64-linux"
39 "aarch64-darwin"
40 ];
41 in
42 inputs.flake-utils.lib.eachSystem systems (
43 system:
44 let
45 pkgs = import inputs.nixpkgs {
46 inherit system;
47 config = {
48 android_sdk.accept_license = true;
49 allowUnfreePredicate =
50 pkg:
51 builtins.elem (pkgs.lib.getName pkg) [
52 # ExplanationNote: android-emulate-app (see below) is unfree by transitivity
53 "android-emulate-app"
54 "android-sdk-build-tools"
55 "android-sdk-cmdline-tools"
56 "android-sdk-emulator"
57 "android-sdk-ndk"
58 "android-sdk-platform-tools"
59 "android-sdk-platforms"
60 "android-sdk-tools"
61 "android-studio-stable"
62 "android-sdk-system-image-${androidPlatformVersion}-google_apis-x86_64"
63 "android-sdk-system-image-${androidPlatformVersion}-google_apis-arm64-v8a"
64 "android-sdk-system-image-${androidPlatformVersion}-google_apis-armeabi-v7a"
65 ];
66 };
67 };
68 androidPlatformVersion = "34";
69 androidBuildToolsVersion = "34.0.0";
70 androidCmakeVersion = "3.22.1";
71 androidSdkArgs = {
72 cmakeVersions = [ androidCmakeVersion ];
73 platformVersions = [ androidPlatformVersion ];
74 abiVersions = [
75 "arm64-v8a"
76 "armeabi-v7a"
77 "x86_64"
78 ];
79 systemImageTypes = [
80 "google_apis"
81 #"google_apis_playstore"
82 ];
83 platformToolsVersion = "34.0.1";
84 ndkVersions = [ "23.1.7779620" ];
85 buildToolsVersions = [ androidBuildToolsVersion ];
86
87 includeNDK = true;
88 #includeEmulator = true;
89 #includeSystemImages = true;
90 extraLicenses = [
91 "android-sdk-license"
92 #"android-sdk-preview-license"
93 #"android-googletv-license"
94 #"android-sdk-arm-dbt-license"
95 #"google-gdk-license"
96 #"intel-android-extra-license"
97 #"intel-android-sysimage-license"
98 #"mips-android-sysimage-license"
99 ];
100 };
101 androidComposition = pkgs.androidenv.composeAndroidPackages androidSdkArgs;
102 androidEmulateApp =
103 args:
104 pkgs.androidenv.emulateApp (
105 {
106 name = "android-emulate-app";
107 app = null;
108 deviceName = "device";
109 package = "";
110
111 # ToDo: set this when the emulator will be working
112 # for emulate-android-armeabi-v7a
113 # or emulate-android-arm64-v8a
114 #app = ".";
115 #package = "chat.simplex.app";
116 #activity = "chat.simplex.app.MainActivity";
117
118 sdkExtraArgs = androidSdkArgs // {
119 emulatorVersion = "35.6.2";
120 };
121 platformVersion = androidPlatformVersion;
122 systemImageType = "google_apis";
123
124 # ExplanationNote: workaround `-gpu host` not working
125 # on my Vulkan system.
126 androidEmulatorFlags = "-gpu guest";
127
128 configOptions = {
129 "hw.keyboard" = "yes";
130 # FixMe: Not working for me:
131 # WARNING | emuglConfig_get_vulkan_hardware_gpu: Failed to create vulkan instance error code: -9
132 # See https://issuetracker.google.com/issues/356896486
133 #"hw.gpu.enabled" = "yes";
134 };
135 }
136 // args
137 );
138 in
139 {
140 packages = rec {
141 default = emulate-android-x86_64;
142
143 emulate-android-x86_64 = androidEmulateApp { abiVersion = "x86_64"; };
144
145 # FailureNote: PANIC: Avd's CPU Architecture 'arm64' is not supported by the QEMU2 emulator on x86_64 host.
146 # ExplanationNote: https://developer.android.com/studio/releases/emulator#support_for_arm_binaries_on_android_9_and_11_system_images
147 # If you were previously unable to use the Android Emulator because
148 # your app depended on ARM binaries, you can now use the Android 9
149 # x86 system image or any Android 11 system image to run your app –
150 # it is no longer necessary to download a specific system image to
151 # run ARM binaries. These Android 9 and Android 11 system images
152 # support ARM by default and provide dramatically improved
153 # performance when compared to those with full ARM emulation.
154 emulate-android-arm64-v8a = androidEmulateApp { abiVersion = "arm64-v8a"; };
155
156 # FailureNote: PANIC: CPU Architecture 'arm' is not supported by the QEMU2 emulator, (the classic engine is deprecated!)
157 # Note: only androidPlatformVersion <= 25 still provide images for this ABI
158 # See nixpkgs/pkgs/development/mobile/androidenv/repo.json
159 emulate-android-armeabi-v7a = androidEmulateApp { abiVersion = "armeabi-v7a"; };
160
161 android-sdk = inputs.android-nixpkgs.sdk (
162 sdkPkgs: with sdkPkgs; [
163 cmdline-tools-latest
164 build-tools-34-0-0
165 platform-tools
166 platforms-android-34
167 emulator
168 ]
169 );
170 };
171
172 devShell =
173 let
174 zipAlignPath = "${androidComposition.androidsdk}/libexec/android-sdk/build-tools/${androidBuildToolsVersion}/zipalign";
175 in
176 pkgs.mkShell {
177 buildInputs = [
178 (pkgs.android-studio.withSdk androidComposition.androidsdk)
179 (pkgs.writeShellScriptBin "zipalign" ''exec ${zipAlignPath} "$@"'')
180 androidComposition.androidsdk
181 androidComposition.platform-tools
182 pkgs.gradle
183 pkgs.gradle.jdk
184 ];
185 shellHook = ''
186 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
187 export ANDROID_SDK_ROOT="${androidComposition.androidsdk}/libexec/android-sdk"
188 export ANDROID_NDK_ROOT="${androidComposition.androidsdk}/libexec/android-sdk/ndk-bundle"
189 export PATH="$(echo "$ANDROID_SDK_ROOT/cmake/${androidCmakeVersion}".*/bin):$PATH"
190
191 # Use the same androidBuildToolsVersion
192 # and a statically linked aapt2 to workaround a dynamic linking failure.
193 export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$ANDROID_SDK_ROOT/build-tools/${androidBuildToolsVersion}/aapt2";
194 '';
195 };
196 }
197 );
198 }