Running flutter apps on a physical android device

This is a guide for anyone looking to run flutter apps on a physical device on nix os.

I’ve been struggling for about a month to run flutter apps on an android device, as far as I’m concerned this is a relatively new issue and will get fixed once the stable channel (23.05) updates it’s flutter package.

Firstly, enable adb in your configuration file and add yourself to the adb users

  {
  programs.adb.enable = true;
  users.users.<your-user>.extraGroups = ["adbusers"];
  }

Now you may use the example flake at: Flutter - NixOS Wiki as a reference, but change the nixpkgs input url to point to nixpkgs-unstable, change whatever attributes of the androidenv package you need to change: Nixpkgs 24.05 manual | Nix & NixOS, but keep the 30.0.3 tools version

And add this enviroment variable to your shell

GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/31.0.0/aapt2";   

You should point to whatever build tools version you are using

My flake looks like this, for reference

{
description = "Flutter 3.0.4";
inputs = {
  nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
  flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        inherit system;
        config = {
          android_sdk.accept_license = true;
          allowUnfree = true;
        };
      };
      buildToolsVersion = "30.0.3";
      androidComposition = pkgs.androidenv.composeAndroidPackages {
        buildToolsVersions = [ "31.0.0" buildToolsVersion "28.0.3" ];
        platformVersions = [ "33" "28" ];
        abiVersions = [ "armeabi-v7a" "arm64-v8a" ];
      };
      androidSdk = androidComposition.androidsdk;
    in
    {
      devShell =
        with pkgs; mkShell rec {
   	GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${androidSdk}/libexec/android-sdk/build-tools/31.0.0/aapt2";
          ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
          buildInputs = [
            flutter
            androidSdk
            jdk11
          ];
        };
    });
}

Don’t forget to run

flutter clean
rm ~/.cache/flutter
rm ~/.gradle

In case you’ve tried to build before

3 Likes

I get the following error

FAILURE: Build failed with an exception.

* Where:
Build file '/home/barsch/code/my_app/android/app/build.gradle' line: 4

* What 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/lbi43bsi63zvp7wjgswsp1vjrg4pv9b2-flutter-3.13.4-unwrapped/packages/flutter_tools/gradle/.gradle' when creating directory '/nix/store/lbi43bsi63zvp7wjgswsp1vjrg4pv9b2-flutter-3.13.4-unwrapped/packages/flutter_tools/gradle/.gradle/buildOutputCleanup'

And I cannot find anything how to resolve it

Okay, so it let’s me build with an older version of flutter in nixpkgs `flutter build apk` fails · Issue #231247 · NixOS/nixpkgs · GitHub

@AndroidB, thank you for the flake and instructions. It worked well for me, but led my next issue which was struggling to have the android_sdk.accept_license = true; option work as expected. It seems I have 5 of 6 licenses not accepted and I have tried a few things, but have not had any luck with getting past this issue.

If you have an answer to this one I would be really happy to know.

A couple of things I did learn

  • trying to accept the licenses directly from sdkmanager suggests we need jdk17. I did try with jdk17. I still couldn’t get the licenses accepted though.
[stuart@T14 flutter10]$ sdkmanager --licenses
Error: LinkageError occurred while loading main class com.android.sdklib.tool.sdkmanager.SdkManagerCli
	java.lang.UnsupportedClassVersionError: com/android/sdklib/tool/sdkmanager/SdkManagerCli has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
  • the next is not really worth mentioning, but flutter doctor wanted an environment variable for CHROME. You have a good flake, so I thought I would mention it if you want to update it.
      androidSdk = androidComposition.androidsdk;
      user = builtins.getEnv "USER";  # <-- added
# ...
          ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
          CHROME_EXECUTABLE = "/home/${user}/.nix-profile/bin/google-chrome-stable"; # <-- added

If you do have some known answer for this issue with the acceptance of the licenses, I would love to hear it.

Thanks again for your posts above!

Stu

Licensing has not been an issue for me when running flutter projects.
Though flutter doctor has that output I think It may be related to the flutter tool looking for the licence acceptance on a non-nix store path. If you are picky about this you may try using a FHS environment.
The missing chrome warning is not that much of an issue either, just copy and paste the local server address prompted by flutter into any browser when executing flutter run, you could try and add chrome into the environment and that may fix it

Thanks for the quick reply, I have not done much flutter dev, but from the little I have done recently on an ubuntu-instance to get more familiar with the install and workflow led me to believe that vs code provides a good workflow with the extensions it has. I have no issues creating a project from the cli in the dev shell, but starting the project from the vs code side (with vs code being nix aware for the environment) wasn’t providing much luck.

I will keep with an ubuntu instance over rdp for now as that works fine, as I get more into flutter I will try to shave this yak through building a solid flutter dev environment another day. Who knows, maybe some upstream change ends up helping at some point.

Thanks again for the response and flake.nix file.

Hi, I am replying to myself here, but I thought I would share the outcome of some trial and error at my end. I spent a bit of time trying to get a good flake.nix generated dev environment for flutter and android working nicely with vs code (with vs code already part of the home-manager base-install). I got close, but didn’t quite get there. VS Code extensions were looking for libraries where they didn’t exist and I was going down a bit of a rabbit hole. So, I decided to work towards getting a good Dockerfile built to achieve the same.

Although, my end goal is still to achieve this through a flake.nix set up, for expediency, this set up I built and tested seems to work well. I thought would share if someone else in the community was wanting to get started quickly and was also having issues with achieving this through NixOS/flakes directly.

I also published an image at hub.docker.com

https://hub.docker.com/repository/docker/stuzenz/flutter-android-dev-image/general

A bit late, but you can also try @chito’s solution in this thread. Which allowed me to use the latest flutter version from the unstable branch.