NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.83k stars 13.92k forks source link

flutter android cannot build #260278

Open Fructokinase opened 1 year ago

Fructokinase commented 1 year ago

Describe the bug

Android apps create from flutter cannot be built. This issue popped up after the recent update. I think it was working for when flutter was still 3.10.0 on nixpkgs.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Install flutter (3.13.4), jdk17, and install android tools into $ANDROID_SDK_ROOT
  2. Create a flutter app: flutter create myapp
  3. Build the android app: cd myapp/android && ./gradlew assembleDebug

This issue has also been reported on stackoverflow

Expected behavior

gradlew assembleDebug should work

Screenshots

Please see the stackoverflow link

Additional context

Specific issues.

  1. Flutter gradle plugin attempts to be built under $FLUTTER_SDK/flutter_tools/gradle. Since $FLUTTER_SDK is read only in nix, this causes the issue mentioned. As far as I know, Gradle includeBuild for plugins have no other way of specifying the buildDir other than directly in the plugin build.gradle file itself. A workaround I found was by patching build.gradle.kts with a line like buildDir = File(System.getenv("FLUTTER_GRADLE_PLUGIN_BUILDDIR"))

  2. FLUTTER_ROOT points to the wrong root? flutter doctor -v points to the unwrapped root. In the android folder for example, local.properties ends up pointing to the unwrapped flutter (I'm assuming unwrapped flutter is not intended to be used directly). I workaround I found was by patching FLUTTER_ROOT in shared.sh to point to the wrapped-linked sdk. Related, I also needed to symlink .git dir from wrapped-link to unwrapped.

Notify maintainers

@babariviere @ericdallo @FlafyDev @hacker102

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
output here
RossComputerGuy commented 1 year ago

Try with flutter run or flutter build. I'm pretty sure running gradle directly isn't supported by Flutter.

Fructokinase commented 1 year ago

@RossComputerGuy flutter run for android apps also fail because it calls gradle build under the hood

Fructokinase commented 1 year ago

cc @vkryachko

vkryachko commented 1 year ago

cc @hacker1024 @FlafyDev looks like a regression during the 3.10->3.13 upgrade, mind taking a look? thanks

Prn-Ice commented 1 year ago

Ran into this issue today, I tried following the steps to set the GRADLE_OPTS parameter but that didn't work. Rolling back to Flutter 3.10.5 via this commit 03e12d1c86caab8d415f37308dd42d90dc208785 works for me.

I can help with testing & debugging.

darichey commented 12 months ago

I can repro. I also believe the FLUTTER_ROOT issue may be related to https://github.com/Dart-Code/Dart-Code/issues/4800. Downgrading fixed that as well.

DanTup commented 12 months ago

Hi 👋

I work on the Dart VS Code extension and think the reason for @darichey's issue could be that the file bin/snapshots/analysis_server.dart.snapshot is missing from the Dart SDK.

In the change linked above was this code:

https://github.com/NixOS/nixpkgs/blob/03e12d1c86caab8d415f37308dd42d90dc208785/pkgs/development/compilers/flutter/flutter.nix#L73-L77

Deletion of dart-sdk would certainly cause issues, but it's not clear to me if something is being put back in its place - and if so, whether it includes the snapshots/analysis_server.dart.snapshot file?

vkryachko commented 12 months ago

@Fructokinase When do you think you can share your fix PR for review?

Fructokinase commented 12 months ago

Found a working solution for all of the issues above. @DanTup snapshots/analysis_server.dart.snapshot is there but is now under <FLUTTER_ROOT>/bin/cache/dart-sdk/bin/snapshots so ln -sf $out/bin/cache/dart-sdk/bin/snapshots $out/bin/snapshots this should fix it. But perhaps there are other things dart-sdk/bin that I am missing.

I'm not familiar with dart so hoping you can pitch in.

DanTup commented 12 months ago

snapshots/analysis_server.dart.snapshot is there but is now under <FLUTTER_ROOT>/bin/cache/dart-sdk/bin/snapshots

I'm not sure I understand the difference - that last path is where I expected it to be (the Dart SDK is at bin/cache/dart-sdk and the snapshot is at bin/snapshots/... in the Dart SDK). It does not need to be in the bin folder of the Flutter SDK.

PaulGrandperrin commented 11 months ago

I basically know nothing about flutter and android dev but I wanted to start a project. I have the same error doing flutter create.. flutter build apk.

After investigating, I'm pretty sure the issue comes from this PR: https://github.com/flutter/flutter/pull/123511

I don't really know how to move forward with that, but I'm hoping that might save some investigation time.

PaulGrandperrin commented 11 months ago

I'm still unable to find any workaround to use flutter on either 23.05 or unstable. Is there any?

Is going back to https://github.com/NixOS/nixpkgs/commit/03e12d1c86caab8d415f37308dd42d90dc208785 the only solution yet?

heyzec commented 10 months ago

The solution in https://discourse.nixos.org/t/problem-building-flutter-app-for-android/35593/2 proposed by user chito worked for me.

dudozermaks commented 10 months ago

Hi there! I created issue for documentation on this problem, and created a (mostly) working flake for building android projects. Issue: https://github.com/NixOS/nixpkgs/issues/267263