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
163.62k stars 26.91k forks source link

Building an AppBundle using `archivesBaseName` fails. #126971

Open kkkkan opened 1 year ago

kkkkan commented 1 year ago

Is there an existing issue for this?

Steps to reproduce

  1. Create a new project, for example using the flutter create bug command.
  2. Open projectRoot/android/app/gradle.
  3. Look for the following section:
    
    android {

........

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.example.bug"
    // You can update the following values to match your application needs.
    // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
    minSdkVersion flutter.minSdkVersion
    targetSdkVersion flutter.targetSdkVersion
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
}

......

}

4. Add `archivesBaseName="abc"` inside the `defaultConfig` section.
5. Build the AppBundle using the `flutter build appbundle` command.

### Expected results

- The `abc-release.aab` file is created in `projectRoot/build/app/outputs/bundle/release`.
- The log shows that the build was successful.

### Actual results

- The abc-release.aab file is created in `projectRoot/build/app/outputs/bundle/release`.
- But the terminal log shows `Gradle build failed to produce an .aab file.`.

### Code sample

<details><summary>Code sample</summary>

```android/app/build.gradle
.....
android {
    namespace "com.example.bug"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.bug"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName

        archivesBaseName = "abc"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

......

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs It was too long, so I uploaded it as a txt file. Here is the link. https://pastebin.com/0E3wYXVb

Flutter Doctor output

Doctor output ```console [√] Flutter (Channel stable, 3.10.0, on Microsoft Windows [Version 10.0.22621.1555], locale ja-JP) • Flutter version 3.10.0 on channel stable at C:\Users\xxxx\AppData\Local\flutter_windows_3.10.0-stable\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 84a1e904f4 (7 days ago), 2023-05-09 07:41:44 -0700 • Engine revision d44b5a94c9 • Dart version 3.0.0 • DevTools version 2.23.1 [√] Windows Version (Installed version of Windows is version 10 or higher) [!] Android toolchain - develop for Android devices (Android SDK version 33.0.1) • Android SDK at C:\Users\xxxx\AppData\Local\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. [√] Chrome - develop for the web • Chrome at C:\Users\xxxx\AppData\Local\Google\Chrome\Application\chrome.exe [!] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.11.9) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools • Visual Studio Build Tools 2019 version 16.11.32106.194 X The current Visual Studio installation is incomplete. Please reinstall Visual Studio. [√] Android Studio (version 2022.1) • 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.15+0-b2043.56-9505619) [√] Android Studio (version 2022.2) • Android Studio at C:\Users\xxxx\Downloads\android-studio-2022.2.1.17-windows\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 17.0.6+0-b2043.56-9586694) [√] Android Studio (version 2022.1) • Android Studio at C:\Users\xxxx\Desktop\android-studio-2022.1.1.10-windows\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.15+0-b2043.56-8887301) [√] VS Code (version 1.74.1) • VS Code at C:\Users\xxxx\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.1555] • Chrome (web) • chrome • web-javascript • Google Chrome 113.0.5672.93 • Edge (web) • edge • web-javascript • Microsoft Edge 112.0.1722.58 [√] Network resources • All expected network resources are available. ! Doctor found issues in 2 categories. ```
kkkkan commented 1 year ago

This is because archivesBaseName renaming is not considered in File findBundleFile(FlutterProject project, BuildInfo buildInfo, Logger logger, Usage usage) in flutter/packages/flutter_tools/lib/src/android/gradle.dart, which checks whether the app bundle is generated after the generation is completed.

.childFile('app-${buildInfo.modeName}.aab'),

Here, use regular expressions to clear any aab (sorry ,I don`t know how write in dart. but I means like below).

.childFile('/*/-${buildInfo.modeName}.aab'),

Or、like on apk, create new directory like flutter-bundle, and copy appbundle flie in here by fixed file name. Like this :

in flutter/packages/flutter_tools/gradle/flutter.gradle

 variant.outputs.all { output ->
                    assembleTask.doLast {
                        // `packageApplication` became `packageApplicationProvider` in AGP 3.3.0.
                        def outputDirectory = variant.hasProperty("packageApplicationProvider")
                            ? variant.packageApplicationProvider.get().outputDirectory
                            : variant.packageApplication.outputDirectory
                        //  `outputDirectory` is a `DirectoryProperty` in AGP 4.1.
                        String outputDirectoryStr = outputDirectory.metaClass.respondsTo(outputDirectory, "get")
                            ? outputDirectory.get()
                            : outputDirectory
                        String filename = "app"
                        String abi = output.getFilter(OutputFile.ABI)
                        if (abi != null && !abi.isEmpty()) {
                            filename += "-${abi}"
                        }
                        if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
                            filename += "-${variant.flavorName.toLowerCase()}"
                        }
                        filename += "-${buildModeFor(variant.buildType)}"
                        project.copy {
                            from new File("$outputDirectoryStr/${output.outputFileName}")
                            into new File("${project.buildDir}/outputs/flutter-apk");
                            rename {
                                return "${filename}.apk"
                            }
                        }
                    }
                }
            }

add like this :

variant.outputs.all { output ->
                    bundleTask.doLast {

and

           project.copy {
                            from new File("$outputDirectoryStr/${output.outputFileName}")
                            into new File("${project.buildDir}/outputs/flutter-bundle");
                            rename {
                                return "${filename}.aab"
                            }
                        }

and in File findBundleFile(FlutterProject project, BuildInfo buildInfo, Logger logger, Usage usage) function , change checking path so.

darshankawar commented 1 year ago

Thanks for the report @kkkkan Can you check this related issue and see if it resembles your case ?

kkkkan commented 1 year ago

@darshankawar

Thank you for checking.

The issue #73919 you pointed out seems to be about building apks. This issue is similar, but slightly different.

Currently, even if you add archivesBaseName, the apk build is successful, but app-<buildtype>.apk is created in the ${baseBuildDir}/flutter-apkdirectory. (${baseBuildDir}/apkdirectory contains ${archivesBaseName}-<buildtype>.apk.) The issue you pointed out seems to be about creating ${archivesBaseName}-<buildtype>.apk in ${baseBuildDir}/flutter-apk directory as well.

This issue I raised is that the build fails when archivesBaseName is added to the AppBundle build, so it is slightly different. (The method that #73919 said should be fixed in the issue you pointed out seems to be a method that can only be used for apk builds, if I understand correctly.)

I hope this is helpful! Let me know if you have any other questions.

kkkkan commented 1 year ago

I apologize for the error in the previous part. The code I provided earlier probably won't work.

add like this :

variant.outputs.all { output ->
                    bundleTask.doLast {

and

           project.copy {
                            from new File("$outputDirectoryStr/${output.outputFileName}")
                            into new File("${project.buildDir}/outputs/flutter-bundle");
                            rename {
                                return "${filename}.aab"
                            }
                        }

There is no function such as bundleTask , and I think we need to do the following in order to achieve the same goal. I think it would be better to do the following instead of the above.

Change

 variant.outputs.all { output ->
                    assembleTask.doLast {
                        // `packageApplication` became `packageApplicationProvider` in AGP 3.3.0.
                        def outputDirectory = variant.hasProperty("packageApplicationProvider")
                            ? variant.packageApplicationProvider.get().outputDirectory
                            : variant.packageApplication.outputDirectory
                        //  `outputDirectory` is a `DirectoryProperty` in AGP 4.1.
                        String outputDirectoryStr = outputDirectory.metaClass.respondsTo(outputDirectory, "get")
                            ? outputDirectory.get()
                            : outputDirectory
                        String filename = "app"
                        String abi = output.getFilter(OutputFile.ABI)
                        if (abi != null && !abi.isEmpty()) {
                            filename += "-${abi}"
                        }
                        if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
                            filename += "-${variant.flavorName.toLowerCase()}"
                        }
                        filename += "-${buildModeFor(variant.buildType)}"
                        project.copy {
                            from new File("$outputDirectoryStr/${output.outputFileName}")
                            into new File("${project.buildDir}/outputs/flutter-apk");
                            rename {
                                return "${filename}.apk"
                            }
                        }
                    }
                }
            }

to like this :

            variant.outputs.all { output ->
                    assembleTask.doLast {
                        // `packageApplication` became `packageApplicationProvider` in AGP 3.3.0.
                        def outputDirectory = variant.hasProperty("packageApplicationProvider")
                            ? variant.packageApplicationProvider.get().outputDirectory
                            : variant.packageApplication.outputDirectory
                        //  `outputDirectory` is a `DirectoryProperty` in AGP 4.1.
                        String outputDirectoryStr = outputDirectory.metaClass.respondsTo(outputDirectory, "get")
                            ? outputDirectory.get()
                            : outputDirectory
                        String filename = "app"
                        String abi = output.getFilter(OutputFile.ABI)
                        if (abi != null && !abi.isEmpty()) {
                            filename += "-${abi}"
                        }
                        if (variant.flavorName != null && !variant.flavorName.isEmpty()) {
                            filename += "-${variant.flavorName.toLowerCase()}"
                        }
                        filename += "-${buildModeFor(variant.buildType)}"
                        project.copy {
                            from new File("$outputDirectoryStr/${output.outputFileName}")
                            into new File("${project.buildDir}/outputs/flutter-apk");
                            rename {
                                return "${filename}.apk"
                            }
                        }

                        **
                        // replace "apk" to "aab" because AGP make bundle same prefix between apk and aab
                        // (I don`t know how to write replace substring , so belows is just image. actual working code is maybe not like this.) 
                        def bundleName = output.outputFileName.replace(".apk",".aab") 

                        // replace "apk" to "bundle" because AGP make appbundle in `bundle` directory. (apk in `apk` directory.)
                        // (I don`t know how to write replace substring , so belows is just image. actual working code is maybe not like this.) 
                        // I think the directory name might be in output.outputFileName, so it might be better to replace it there. 
                        // However, I think it is in outputDirectoryStr.
                        def bundleOuputDir = outputDirectoryStr.replace("apk","bundle")
                        project.copy {
                            from new File("$bundleOuputDir/${bundleName}")
                            into new File("${project.buildDir}/outputs/flutter-bundle");
                            rename {
                                return "${filename}.aab"
                            }
                        }
                        **
                    }
                }
            }
darshankawar commented 1 year ago

Thanks for the update. Seeing the same behavior as reported.

stable, master flutter doctor -v ``` [!] Flutter (Channel stable, 3.10.0, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.10.0 on channel stable at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 84a1e904f4 (2 days ago), 2023-05-09 07:41:44 -0700 • Engine revision d44b5a94c9 • Dart version 3.0.0 • DevTools version 2.23.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. [!] Flutter (Channel master, 3.11.0-6.0.pre.134, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.11.0-6.0.pre.134 on channel master at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision f31dae2a80 (5 hours ago), 2023-05-17 17:18:51 -0700 • Engine revision d970370779 • Dart version 3.1.0 (build 3.1.0-118.0.dev) • DevTools version 2.23.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/dhs/Library/Android/sdk ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 13C100 • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2) • IntelliJ at /Applications/IntelliJ IDEA.app • Flutter plugin version 65.1.4 • Dart plugin version 213.7228 [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.29.0 [✓] Connected device (3 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 15.3.1 19D52 • macOS (desktop) • macos • darwin-x64 • macOS 12.2.1 21D62 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
tranleo148 commented 3 months ago

+1 I have faced the same issue too. The build failed when build appbundle with archivesBaseName modified in build.gradle

NickNevzorov commented 1 month ago

+1 I have faced the same issue too. The build failed when build appbundle with archivesBaseName modified in build.gradle