]> Git — Sourcephile - tmp/julm/android.git/blob - android/default.nix
compatibility(haskell): enable `ghcPackages.shellFor`
[tmp/julm/android.git] / android / default.nix
1 {
2 inputs,
3 system,
4 pkgs,
5 mainLynxBundle,
6 }:
7 let
8 lib = pkgs.lib;
9 config = rec {
10 gradle = pkgs.gradle;
11 androidPlatformVersion = "34";
12 androidBuildToolsVersion = "34.0.0";
13 androidCmakeVersion = "3.22.1";
14 androidSdkArgs = {
15 #cmakeVersions = [ androidCmakeVersion ];
16 platformVersions = [ androidPlatformVersion ];
17 abiVersions = [
18 #"arm64-v8a"
19 #"armeabi-v7a"
20 "x86_64"
21 ];
22 #platformToolsVersion = "35.0.2";
23 #ndkVersions = [ "23.1.7779620" ];
24 buildToolsVersions = [ androidBuildToolsVersion ];
25 includeNDK = true;
26 includeEmulator = false;
27
28 # avdmanager create avd --force -n device -k 'system-images;android-36;google_apis_playstore;x86_64'
29 #emulatorVersion = "35.6.2";
30 includeSystemImages = true;
31 systemImageTypes = [
32 #"google_apis"
33 "google_apis_playstore"
34 ];
35
36 extraLicenses = [
37 "android-sdk-license"
38 "google-gdk-license"
39 #"android-sdk-preview-license"
40 #"android-googletv-license"
41 #"android-sdk-arm-dbt-license"
42 #"intel-android-extra-license"
43 #"intel-android-sysimage-license"
44 #"mips-android-sysimage-license"
45 ];
46 };
47 androidEmulateAppArgs = rec {
48 name = "android-emulate-app";
49 androidUserHome = null;
50 #androidUserHome = "/var/lib/android";
51 deviceName = "device-${androidPlatformVersion}";
52
53 sdkExtraArgs = androidSdkArgs // {
54 includeEmulator = true;
55 #emulatorVersion = "35.6.2";
56 };
57 platformVersion = androidPlatformVersion;
58 systemImageType = "google_apis_playstore";
59
60 # PortabilityNote: workaround `-gpu host` not working
61 # on my Vulkan system.
62 androidEmulatorFlags = "-gpu guest";
63
64 configOptions = {
65 "hw.keyboard" = "yes";
66 #"sdcard.path" = "${androidUserHome}/avd/${deviceName}.sdcard.img";
67 # FixMe: Not working for me:
68 # WARNING | emuglConfig_get_vulkan_hardware_gpu: Failed to create vulkan instance error code: -9
69 # See https://issuetracker.google.com/issues/356896486
70 #"hw.gpu.enabled" = "yes";
71 };
72 };
73 ANDROID_SDK_ROOT = "${androidComposition.androidsdk}/libexec/android-sdk";
74 ANDROID_NDK_ROOT = "${androidComposition.androidsdk}/libexec/android-sdk/ndk-bundle";
75 androidComposition = pkgs.androidenv.composeAndroidPackages config.androidSdkArgs;
76 };
77 in
78 {
79 inherit config;
80 allowUnfreePredicate = pkg:
81 builtins.elem (lib.getName pkg) [
82 "android-sdk-build-tools"
83 "android-sdk-cmdline-tools"
84 "android-sdk-ndk"
85 "android-sdk-platform-tools"
86 "android-sdk-platforms"
87 "emulator"
88 "android-sdk-tools"
89 "android-studio-stable"
90 "android-emulate-app"
91 "build-tools"
92 "cmake"
93 "cmdline-tools"
94 "ndk"
95 "platform-tools"
96 "platforms"
97 "tools"
98 "android-sdk-emulator"
99 "android-sdk-system-image-${config.androidPlatformVersion}-google_apis_playstore-x86_64"
100 "system-image-${config.androidPlatformVersion}-google_apis_playstore-x86_64"
101 "system-image-${config.androidPlatformVersion}-google_apis-x86_64"
102 "android-sdk-system-image-${config.androidPlatformVersion}-google_apis-x86_64"
103 ];
104 devShells = {
105 default = {
106 nativeBuildInputs = [
107 (pkgs.android-studio.withSdk config.androidComposition.androidsdk)
108 config.androidComposition.androidsdk
109 config.androidComposition.platform-tools
110 config.androidComposition.emulator # For mksdcard
111 config.gradle
112 config.gradle.jdk
113 pkgs.just
114 ];
115 shellHook = ''
116 # ExplanationNote: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
117 export ANDROID_SDK_ROOT="${config.ANDROID_SDK_ROOT}"
118 export ANDROID_NDK_ROOT="${config.ANDROID_NDK_ROOT}"
119 export PATH="${
120 lib.concatStringsSep ":" [
121 "$ANDROID_SDK_ROOT/cmake/${config.androidCmakeVersion}/bin"
122 "$ANDROID_SDK_ROOT/build-tools/${config.androidBuildToolsVersion}"
123 "$PATH"
124 ]
125 }"
126
127 # Use the same androidBuildToolsVersion
128 # and a statically linked aapt2 to workaround a dynamic linking failure.
129 export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$ANDROID_SDK_ROOT/build-tools/${config.androidBuildToolsVersion}/aapt2";
130 '';
131 };
132 };
133 packages = rec {
134 default = pkgs.stdenv.mkDerivation (finalAttrs: {
135 pname = "${mainLynxBundle.pname}-apk";
136 version = mainLynxBundle.version;
137 src =
138 with lib.fileset;
139 toSource rec {
140 root = ./.;
141 fileset = unions [
142 (fileFilter (file: lib.any file.hasExt [ "java" "kt" "toml" "xml" ]) ./app)
143 ./build.gradle
144 ./gradle.properties
145 ./settings.gradle
146 app/build.gradle
147 app/proguard-rules.pro
148 app/src/main/res
149 gradle/libs.versions.toml
150 ];
151 };
152 inherit (config)
153 ANDROID_SDK_ROOT
154 ANDROID_NDK_ROOT
155 ;
156
157 nativeBuildInputs = [
158 config.gradle
159 pkgs.makeWrapper
160 ];
161
162 mitmCache = config.gradle.fetchDeps {
163 pkg = finalAttrs.finalPackage;
164 # MaintenanceNote: nix -L run .#default.update-deps
165 data = ./deps.json;
166 };
167
168 passthru = {
169 update-deps = pkgs.writeShellApplication {
170 name = "update-deps.sh";
171 text = ''
172 exec ${finalAttrs.finalPackage.mitmCache.updateScript}
173 '';
174 };
175 };
176
177 # this is required for using mitm-cache on Darwin
178 __darwinAllowLocalNetworking = true;
179
180 gradleFlags = [
181 "-Dfile.encoding=utf-8"
182 # Use the same androidBuildToolsVersion
183 # and a statically linked aapt2 to workaround a dynamic linking failure.
184 "-Dorg.gradle.project.android.aapt2FromMavenOverride=${config.ANDROID_SDK_ROOT}/build-tools/${config.androidBuildToolsVersion}/aapt2"
185 ];
186
187 #gradleBuildTask = "shadowJar";
188 gradleBuildTask = "assemble";
189
190 # MaintenanceToDo: remove this `gradleUpdateScript`
191 # if https://github.com/NixOS/nixpkgs/pull/383115
192 # is ever merged.
193 gradleUpdateScript = ''
194 runHook preBuild
195 gradle help --write-verification-metadata sha256
196 '';
197
198 # will run the gradleCheckTask (defaults to "test")
199 doCheck = false;
200
201 preBuild = ''
202 install -Dm440 \
203 ${mainLynxBundle}/main.lynx.bundle \
204 app/src/main/assets/main.lynx.bundle
205 '';
206 installPhase = ''
207 mkdir -p $out
208 ${
209 if finalAttrs.gradleBuildTask == "bundleRelease" then
210 "cp -RL app/build/outputs/bundle/release/*.aab $out"
211 else
212 "cp -RL app/build/outputs/apk/*/*.apk $out"
213 }
214 '';
215
216 meta.sourceProvenance = with lib.sourceTypes; [
217 fromSource
218 binaryBytecode # mitm cache
219 ];
220 });
221
222 # Run the app into an Android emulator
223 android-emulator-x86_64 = pkgs.androidenv.emulateApp (
224 config.androidEmulateAppArgs
225 // {
226 abiVersion = "x86_64";
227 # SecurityToDo: sign the APK
228 # See: https://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle
229 app = "${default}/app-debug.apk";
230 #app = "${default}/app-release-unsigned.apk";
231 package = "com.lynx.javaemptyproject";
232 activity = ".MainActivity";
233 }
234 );
235 };
236 }