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