Android NDK and SDK licenses not accepted

I am trying to create a devshell flake for a flutter environment, I wanted to use devshells to take advantage of Nix’s power but I am encountering some issues.

I have the appropriate packages defined and also the environment variables so that the emulator can see the AVD that was created but whenever I try to run the flutter app on the AVD I get the following error:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/zander/Nextcloud/Projects/Applications/bookshelf/android/build.gradle.kts' line: 16

* What went wrong:
A problem occurred configuring project ':app'.
> com.android.builder.sdk.LicenceNotAcceptedException: Failed to install the following Android SDK packages as some licences have not been accepted.
     ndk;26.3.11579264 NDK (Side by side) 26.3.11579264
  To build this project, accept the SDK license agreements and install the missing components using the Android Studio SDK Manager.
  All licenses can be accepted using the sdkmanager command line tool:
  sdkmanager.bat --licenses
  Or, to transfer the license agreements from one workstation to another, see https://developer.android.com/studio/intro/update.html#download-with-gradle

  Using Android SDK: /nix/store/yqk2048cmf27b16al6d2b08j4qqk0pac-android-sdk-platform-tools-35.0.2/libexec/android-sdk

* 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 2s
Running Gradle task 'assembleDebug'...                              4.6s
Error: Gradle task assembleDebug failed with exit code 1

I also have the android_sdk.accept_license = true enabled in my flake. So I did sdkmanager --licenses and accepted all the licenses that I haven’t accepted yet, of which there were 6 of 7. After saying yes to all of them it still says I haven’t accepted them. I did noticed the warnings are a bit weird but I have set the ANDROID_NDK_HOME to be the right location in the nixpkgs.androidsdk path.

Warning: Observed package id 'ndk;28.1.13356709' in inconsistent location '/nix/store/ivh1jgqjy3iz0829nf41hv5p9mmhn3fn-androidsdk/libexec/android-sdk/ndk-bundle' (Expected '/nix/store/ivh1jgqjy3iz0829nf41hv5p9mmhn3fn-androidsdk/libexec/android-sdk/ndk/28.1.13356709')
Warning: Observed package id 'ndk;28.1.13356709' in inconsistent location '/nix/store/ivh1jgqjy3iz0829nf41hv5p9mmhn3fn-androidsdk/libexec/android-sdk/ndk-bundle' (Expected '/nix/store/ivh1jgqjy3iz0829nf41hv5p9mmhn3fn-androidsdk/libexec/android-sdk/ndk/28.1.13356709')
[=======================================] 100% Computing updates...
6 of 7 SDK package licenses not accepted.
Review licenses that have not been accepted (y/N)?

Here is my flake.nix

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  };

  outputs = { self, nixpkgs }@inputs:
  let
        system = "x86_64-linux";
        pkgs = import nixpkgs {
            system = system;
            config = {
                allowUnfree = true;
                android_sdk.accept_license = true;
            };
       };
        android_home = "${pkgs.androidsdk}/libexec/android-sdk";
  in
  {
    androidEnv = pkgs.androidenv.override { licenseAccepted = true; };
    devShells.${system}.default = pkgs.mkShell {
       packages = with pkgs; [
            flutter
            android-tools
            androidenv.androidPkgs.androidsdk
            androidenv.androidPkgs.ndk-bundle
            openjdk
       ];
       ANDROID_HOME = "${android_home}";
       # ANDROID_SDK_ROOT = "${android_home}";
       ANDROID_NDK_HOME = "${android_home}/ndk";
       ANDROID_AVD_HOME = "/home/zander/.config/.android/avd";
       shellHook = ''
            echo $ANDROID_AVD_HOME
            exec ${pkgs.zsh}/bin/zsh
       '';
    };
  };
}

I’m pretty new to nix and this is my first time settings up a devshell so I could be missing something very obvious here. Any help would be greatly appreciated

I had this problem, flutter doctor keeps saying that licenses not accepted, but in fact it can build for android and run on native device/emulator.

Here’s my flake.nix for flutter development:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  };
  outputs = { self, nixpkgs, ... }:
    
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;
        config = {
          android_sdk.accept_license = true; # <-- this is needed
          allowUnfree = true;
        };
      };
      # --- Android SDK Configuration ---
      # We use composeAndroidPackages to select specific components
      # to reduce download size.
      androidComposition = pkgs.androidenv.composeAndroidPackages {
        buildToolsVersions = [ "34.0.0" "35.0.0" ];
        platformVersions = [ "35" "36" ];

        # includeCmdlineTools and includePlatformTools are true by default
        # when using composeAndroidPackages like this.
        # cmdline-tools will be the latest available.
        #
        # You can add other components if needed:
        # includeEmulator = true;
        # includeSystemImages = true;
        # systemImageTypes = [ "google_apis_playstore" ]; 
        # abiVersions = [ "x86_64" ]; # Ensure x86_64 system images are included
        includeNDK = true;
        ndkVersions = ["26.3.11579264"]; # specify the version from grande configs in your project
        cmakeVersions = [ "3.22.1" ]; # the same
      };
      androidSdk = androidComposition.androidsdk;

    in
    {
      devShells.${system}.default =
        with pkgs; mkShell rec {
          ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
          buildInputs = [
            flutter
            androidSdk
            jdk17
            glxinfo 
          ];

          shellHook = ''
            export CHROME_EXECUTABLE="/home/alex/.nix-profile/bin/chromium"
            export JAVA_HOME="${jdk17}/lib/openjdk"
            export PATH="$JAVA_HOME/bin:$PATH"
            export ANDROID_HOME="${androidSdk}/libexec/android-sdk"
            export PATH="${androidSdk}/platform-tools:$PATH"
            export PATH="${androidSdk}/cmdline-tools/latest/bin:$PATH"
            # Add build-tools so we can use the Nix-packaged aapt2 instead of Maven’s
            export PATH="${androidSdk}/build-tools/35.0.0:$PATH"
            export PATH="${androidSdk}/build-tools/34.0.0:$PATH"
            # Force AGP to use aapt2 from SDK/build-tools rather than Maven cache (fixes NixOS stub-ld issue)
            export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$ANDROID_HOME/build-tools/35.0.0/aapt2 $GRADLE_OPTS"
          '';
        }; 
    };
}