I have been trying to do android development in NixOS using the following flake.
{
description = "A basic project in flutter";
inputs = {
flake-utils = {
url = "github:numtide/flake-utils";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
android-nixpkgs = {
url = "github:tadfisher/android-nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, flake-compat, android-nixpkgs }:
flake-utils.lib.eachDefaultSystem ( system:
let
pkgs = import nixpkgs {
inherit system;
config = {
allowUnfree = true;
android_sdk = {
accept_license = true;
};
};
};
androidCustomPackage = android-nixpkgs.sdk.${system} (
sdkPkgs: with sdkPkgs; [
cmdline-tools-latest
build-tools-30-0-3
build-tools-33-0-2
build-tools-34-0-0
platform-tools
emulator
#patcher-v4
platforms-android-28
platforms-android-29
platforms-android-30
platforms-android-31
platforms-android-32
platforms-android-33
platforms-android-34
]
);
pinnedJDK = pkgs.jdk17; # jdk11, jdk13
in {
devShells = {
default = pkgs.mkShell {
name = "My-flutter-dev-shell";
buildInputs = with pkgs; [
flutter
android-studio
] ++ [
pinnedJDK
#androidEnvCustomPackage.androidsdk
androidCustomPackage
];
#shellHook = ''
# GRADLE_USER_HOME=$HOME/gradle-user-home
# GRADLE_HOME=$HOME/gradle-home
#'';
JAVA_HOME = pinnedJDK;
ANDROID_HOME = "${androidCustomPackage}/share/android-sdk";
ANDROID_SDK_HOME = "${androidCustomPackage}/share/android-sdk";
GRADLE_USER_HOME = "/home/admin0101/.gradle";
GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidCustomPackage}/share/android-sdk/build-tools/34.0.0/aapt2";
};
};
}
);
}
It works fine for flutter build linux but it fails with a Gradle error if i try to do flutter build apk. It seems like it is trying to write in /nix/store to configure the flutter plugin for Gradle.
FAILURE: Build failed with an exception.
Where:
Build file ‘/home/admin1010/MyProjects/fivexyz/android/app/build.gradle’ line: 4What went wrong:
Error resolving plugin [id: ‘dev.flutter.flutter-gradle-plugin’, version: ‘1.0.0’]A problem occurred configuring project ‘:gradle’.
Could not create service of type OutputFilesRepository using ExecutionGradleServices.createOutputFilesRepository().
Failed to create parent directory ‘/nix/store/ap5y9j4v0d66bcw36fqj8nfqvwbn7bmq-flutter-wrapped-3.16.7-sdk-links/packages/flutter_tools/gradle/.gradle’ when creating directory ‘/nix/store/ap5y9j4v0d66bcw36fqj8nfqvwbn7bmq-flutter-wrapped-3.16.7-sdk-links/packages/flutter_tools/gradle/.gradle/buildOutputCleanup’
- Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
- Get more help at https://help.gradle.org
BUILD FAILED in 8s
Running Gradle task ‘assembleRelease’… 9.6s
Gradle task assembleRelease failed with exit code 1
I have tried setting the GRADLE_USER_HOME to my home directory but it did not fix the problem. It seems Gradle uses the new location for temperory files and cache but as soon as it gets to flutter plugins, It fails with the same error.