flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.16k stars 27.24k forks source link

[gradle] Flutter generates files under `android/app/build` #93947

Open deakjahn opened 2 years ago

deakjahn commented 2 years ago

Steps to Reproduce

  1. Create a new Flutter project that includes Android support
  2. Check out android/build.gradle

My earlier projects had the following fragment:

rootProject.buildDir = '../build'
subprojects {
  project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
  project.evaluationDependsOn(':app')
}

Currently, a slightly simplified (?) fragment is created:

rootProject.buildDir = '../build'
subprojects {
  project.buildDir = "${rootProject.buildDir}/${project.name}"
  project.evaluationDependsOn(':app')
}

Actual results:

With the current Gradle code, building behaves slightly differently. Part of the generated intermediate files will be placed into android/app/build instead of build/app. The building process works all right but because those files are no longer under build, a flutter clean command will not remove them. Or, Android Studio's Export to Zip File that archives all the important source files necessary to backup a project will also package these intermediate files unnecessarily.

Code sample ```dart ```
Doc ``` [√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1348], locale en-US) • Flutter version 2.5.3 at E:\Android\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 18116933e7 (5 weeks ago), 2021-10-15 10:46:35 -0700 • Engine revision d3ea636dc5 • Dart version 2.14.4 [!] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at E:/Android/Sdk X cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe) ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable. [√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.11.31729.503 • Windows 10 SDK version 10.0.19041.0 [√] Android Studio (version 2020.3) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) [√] Connected device (3 available) • sdk gphone x86 64 arm64 (mobile) • emulator-5554 • android-x64 • Android 11 (API 30) (emulator) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19043.1348] • Edge (web) • edge • web-javascript • Microsoft Edge 95.0.1020.53 ! Doctor found issues in 2 categories. Process finished with exit code 0 ```
maheshj01 commented 2 years ago

@deakjahn I don't see any intermediate files getting generated under android/app/build with a project created on stable 2.5.3.

deakjahn commented 2 years ago

I can't see in a newly created project now, either. But in my real project, I can recreate any time. If I separate it into the earlier two, it works all right, if I unify it, it fails, repeatedly. It's an Android app with two flawors (GMS and HMS ads and in-app purchase), this might have an influence on the issue, different flavors call for different implementations included. Unfortunately, I can't just include the source. I'll try to come up with a minimal sample but I don't know if I can...

deakjahn commented 2 years ago

I could. The flavors make the problem. Don't expect the app to run, I removed the Google ad ID, it throws. Only the build process is important, anyway.

xxx.zip

I don't know if you expect the flavors to be set up any differently. This scheme works for me just fine and is a requirement since you (Google :-) ) introduced that you don't accept apps that have "foreign" billing libraries inside, even if it's not used because the engine is determined at runtime. This is the only viable way I could come up with to have both Google and Huawei support in the same project, to be built with strictly either one or the other. And it works just fine with the two separate subprojects.

maheshj01 commented 2 years ago

Hi @deakjahn, Thanks for filing the issue. Running your sample repo I could see build folder getting generated under android/app/build This seems to be happening because of the local dependency gms_support I tried removing it and it did not generate any such files under android/app. So I believe this is not an issue with the gradle or flavor settings.

Closing as this isn't an issue with flutter, if you disagree feel free to write in the comments and I will reopen it.

Thank you.

deakjahn commented 2 years ago

I beg to differ. The fact that reverting the android/build.gradle file modifies this behavior makes it a Gradle issue per se.

rootProject.buildDir = '../build'
subprojects {
  project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
  project.evaluationDependsOn(':app')
}

I can't really tell why this makes a difference but it does. Can this be an issue of paralell versus sequential evaluation? I don't know the innards of Gradle...

maheshj01 commented 2 years ago

ReOpening the issue based on the above comment.

maheshj01 commented 2 years ago

Keeping this issue open for further insights from the team

milindgoel15 commented 1 year ago

I can reproduce this issue in my project which uses a realm database. I added a plugin "add_2_calendar" or "android_intent_plus" to the pubspec.yaml file and creating release builds will stop the app from working raising a realmException. Although I am not so sure if that specific issue is probably not related to this created issue though changing the code block fixes the issue. And yes, I have tested with several combinations. using flavours, build types etc.

subprojects {
  project.buildDir = "${rootProject.buildDir}/${project.name}"
  project.evaluationDependsOn(':app')
}

back to

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

However, several plugins and even the fresh project don't reproduce this issue. It only happens on a few plugins as mentioned above. The author is likely using some plugin/dependency which causes the gradle to generate files in the android/build folder while still keeping the app usable. In my case, it broke the flutter_toast plugin as well.

I can provide a sample repository to reproduce this behaviour. Lemme know if you need it.

I have a general question regarding this. Was the subprojects not combined intentional when creating a fresh project so as to not make any issues?

hammvdh commented 1 week ago

+1 Did anyone manage to resolve this?