Flutter: Difference between revisions

imported>Jmarmstrong1207
No edit summary
m Replaced `ANDROID_SDK_ROOT` with its non-deprecated counterpart
 
(19 intermediate revisions by 10 users not shown)
Line 5: Line 5:
== Development ==
== Development ==


The easiest way is to install Android Studio by adding <code>pkgs.androidstudio</code> to your list of packages in configuration.nix.
=== Linux desktop/Web ===
 
To build Flutter apps to Linux desktop or Web you only need the <code>flutter</code> package from Nixpkgs.
Then run <code>flutter build linux</code> or <code>flutter build web</code>. Ensure that <code>pkg-config</code> is installed on your system, either through a development shell or directly on the base system. Not having <code>pkg-config</code> available may result in compilation errors.
 
=== Android ===
 
The easiest way is to install Android Studio by adding <code>pkgs.android-studio</code> to your list of packages in configuration.nix.


If you prefer [[Vscode|VSCode]], you can create a [[Development environment with nix-shell|dev-shell]] with the packages "jdk", "flutter", and a preferred android sdk such as the preconfigured one "androidenv.androidPkgs_9_0.androidsdk" (mentioned in the Android wiki page). Add other packages if missing any. Or you can install Android Studio to get all the Android packages, and install Flutter.
If you prefer [[Vscode|VSCode]], you can create a [[Development environment with nix-shell|dev-shell]] with the packages "jdk", "flutter", and a preferred android sdk such as the preconfigured one "androidenv.androidPkgs_9_0.androidsdk" (mentioned in the Android wiki page). Add other packages if missing any. Or you can install Android Studio to get all the Android packages, and install Flutter.


Below is an example [[Flakes|flake.nix]] for creating a dev shell. Create following <code>flake.nix</code> in a new project directory
Below is an example [[Flakes|flake.nix]] for creating a dev shell. Create following <code>flake.nix</code> in a new project directory
Line 16: Line 22:
description = "Flutter 3.13.x";
description = "Flutter 3.13.x";
inputs = {
inputs = {
   nixpkgs.url = "github:NixOS/nixpkgs/23.11";
   nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
   flake-utils.url = "github:numtide/flake-utils";
   flake-utils.url = "github:numtide/flake-utils";
};
};
Line 40: Line 46:
       devShell =
       devShell =
         with pkgs; mkShell rec {
         with pkgs; mkShell rec {
           ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
           ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
           buildInputs = [
           buildInputs = [
             flutter
             flutter
Line 70: Line 76:
         };
         };
       };
       };
      androidSdk = pkgs.androidenv.androidPkgs_9_0.androidsdk;
     in
     in
     {
     {
       devShell =
       devShell =
         with pkgs; mkShell rec {
         with pkgs; mkShell rec {
           ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
           ANDROID_SDK = "${androidSdk}/libexec/android-sdk";
           buildInputs = [
           buildInputs = [
             flutter
             flutter
             androidenv.androidPkgs_9_0.androidsdk
             androidSdk
             jdk17
             jdk17
           ];
           ];
Line 94: Line 101:
# flutter run
# flutter run
</syntaxhighlight>
</syntaxhighlight>
== Emulators ==
[[Android|View the Android wiki page for more info]], but you can set up emulators in Android Studio, run them from there, then target the emulator in VSCode when running your flutter code. Otherwise, you can Nixify or even manually add your emulators as stated in the Android wiki page


== Packaging ==
== Packaging ==


Use [https://github.com/NixOS/nixpkgs/blob/cfe96dbfce8bd62dcd4a8ad62cb79dec140b1a62/pkgs/development/compilers/flutter/flutter.nix#L168 buildFlutterApplication from nixpkgs].
Use [https://github.com/NixOS/nixpkgs/blob/cfe96dbfce8bd62dcd4a8ad62cb79dec140b1a62/pkgs/development/compilers/flutter/flutter.nix#L168 buildFlutterApplication from nixpkgs].
== Troubleshooting ==
* The default Gradle template for android captures the Android and Flutter SDK paths in <code>android/local.properties</code> preventing future updates to the SDK from participating in the build. Example:<syntaxhighlight lang="shell-session">$ cat android/local.properties
sdk.dir=/nix/store/m5ygjwkz8brwkw9anx9kbwssymwvlaxl-androidsdk/libexec/android-sdk
flutter.sdk=/nix/store/rga4z7r7x705lns431b89gwxx47zs1zp-flutter-wrapped-3.29.3-sdk-links
flutter.buildMode=debug
flutter.versionName=6.3.2</syntaxhighlight>This manifests itself as Gradle failing to install SDK components registered in the shell. To fix, simply delete the properties file and run the build again. NOTE: this assumes that the shell correctly reflects the requirements of the build. If it doesn't correct that first, otherwise deleting <code>android/local.properties</code> achieves nothing.<syntaxhighlight lang="shell-session">FAILURE: Build failed with an exception.
* Where:
Build file /xxx/android/build.gradle.kts' line: 16
* What went wrong:
A problem occurred configuring project ':app'.
> com.android.builder.sdk.InstallFailedException: Failed to install the following SDK components:
      ndk;26.3.11579264 NDK (Side by side) 26.3.11579264
  The SDK directory is not writable (/nix/store/1xw5npxd7isrl50pl7y82anhdapnfs6p-androidsdk/libexec/android-sdk)</syntaxhighlight>
* There might be cases where setting the <code>ANDROID_HOME</code> environment variable will have no effect on Flutter's ability to assess the validity of Android SDK's installation. This behavior may be caused by an existing configuration file (<code>$XDG_CONFIG_HOME/settings</code> or <code>$XDG_CONFIG_HOME/flutter/settings</code>) containing an incorrectly set <code>"android-sdk"</code> key-value pair. Once removed, the environment variable should no longer be overridden, and should function correctly.


== See also ==
== See also ==


* The [https://github.com/orgs/NixOS/teams/flutter team working on flutter in nixpkgs] maintains several pieces of infrastructure related to the cause. The documentation is lacking as of now, but there are plans to improve it.
* The [https://github.com/orgs/NixOS/teams/flutter team working on flutter in nixpkgs] maintains several pieces of infrastructure related to the cause. The documentation is lacking as of now, but there are plans to improve it.
[[Category: Development]]