From 41eeaa0204c829e059ae66800aef3090f17e99c1 Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Thu, 10 Nov 2022 16:41:32 -0500
Subject: [PATCH] try harder to tell gradle et al where to find the tools

it doesn't work though
---
 flake.nix | 58 ++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 49 insertions(+), 9 deletions(-)

diff --git a/flake.nix b/flake.nix
index 29cc11b..caaca65 100644
--- a/flake.nix
+++ b/flake.nix
@@ -128,22 +128,62 @@
         ANDROIDSDK = "${androidComposition.androidsdk}/libexec/android-sdk";
         ANDROIDNDK = "${ANDROIDSDK}/ndk-bundle";
 
-        buildInputs = with pkgs; [
-
-          # XXXX
-          # gradle finds an aapk2 from some random tgz it downloads from internet instead of from our ANDROIDSDK!  GREAT!!!!!!!!!!11111
-          #
-          # Fix the one it downloads, if it didn't already root you, with:
-          #
-          #  patchelf --set-interpreter <correct ld.so> ~/.gradle/<...path to aapt2>
+        ANDROID_SDK_ROOT = ANDROIDSDK;
+        ANDROID_NDK_ROOT = ANDROIDNDK;
+
+        # Deprecated but ... :shrug:
+        ANDROID_HOME = ANDROIDSDK;
+
+        # Make sure gradle uses the aapt2 from our Android SDK.  Otherwise it
+        # downloads one from the internet that doesn't work because it
+        # specifies an interpreter (ld.so) that nothing has ensures will
+        # exist.
+        #
+        # Without this you get a stack of build errors that look a bit like:
+        #
+        # Execution failed for task ':app:processDebugResources'.                                                                                                                        #
+        # > Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
+        #    > Failed to transform appcompat-1.3.0.aar (androidx.appcompat:appcompat:1.3.0) to match attributes {artifactType=android-compiled-dependencies-resources, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.
+        #       > Execution failed for AarResourcesCompilerTransform: /home/exarkun/.gradle/caches/transforms-3/f5908003b5fc10a9cf0fd543db10ae32/transformed/appcompat-1.3.0.
+        #          > AAPT2 aapt2-7.2.1-7984345-linux Daemon #1: Daemon startup failed
+        #            This should not happen under normal circumstances, please file an issue if it does.
+        # GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROIDSDK}/build-tools/${buildToolsVersion}/aapt2";
 
+        buildInputs = with pkgs; [
           gradle
           ant
-          # openjdk8
           android-studio
           android-tools
           python39
         ];
+
+        shellHook = ''
+        # Tell the tools a third time where the SDK and NDK are to be found.
+        cat >app/local.properties <<EOF
+        # THIS FILE IS AUTOMATICALLY GENERATED
+        # DO NOT EDIT
+        # YOUR CHANGES WILL BE DESTROYED
+
+        sdk.dir=${ANDROIDSDK}
+
+        # This probably needs to be set somewhere, but perhaps not here?
+        #
+        # > [CXX5106] NDK was located by using ndk.dir property. This method
+        # > is deprecated and will be removed in a future release. Please delete
+        # > ndk.dir from local.properties and set android.ndkVersion to
+        # > [23.0.7123448] in all native modules in the project.
+        # > https://developer.android.com/r/studio-ui/ndk-dir
+        # android.ndkVersion=${ndkVersion}
+        EOF
+        '';
+
+        # cat >app/app/gradle.properties <<EOF
+        # # THIS FILE IS AUTOMATICALLY GENERATED
+        # # DO NOT EDIT
+        # # YOUR CHANGES WILL BE DESTROYED
+
+        # android.aapt2FromMavenOverride=${ANDROIDSDK}/build-tools/30.0.3/aapt2
+        # EOF
       };
 
       devShells.default = self.devShells.${system}.gradle;
-- 
GitLab