diff --git a/dev/devicelab/bin/tasks/gradle_plugin_bundle_test.dart b/dev/devicelab/bin/tasks/gradle_plugin_bundle_test.dart index f08e96fcd5..ec026f75e9 100644 --- a/dev/devicelab/bin/tasks/gradle_plugin_bundle_test.dart +++ b/dev/devicelab/bin/tasks/gradle_plugin_bundle_test.dart @@ -17,17 +17,14 @@ Future main() async { await project.runGradleTask('bundleRelease'); final String releaseBundle = path.join( - project.rootPath, - 'build', - 'app', - 'outputs', - 'bundle', - 'release', - 'app.aab', - ); - - final Iterable bundleFiles = await getFilesInAppBundle(releaseBundle); - + project.rootPath, + 'build', + 'app', + 'outputs', + 'bundle', + 'release', + 'app.aab', + ); checkItContains([ 'base/manifest/AndroidManifest.xml', 'base/dex/classes.dex', @@ -35,28 +32,30 @@ Future main() async { 'base/lib/arm64-v8a/libflutter.so', 'base/lib/armeabi-v7a/libapp.so', 'base/lib/armeabi-v7a/libflutter.so', - ], bundleFiles); + ], await getFilesInAppBundle(releaseBundle)); }); await runProjectTest((FlutterProject project) async { section('App bundle content using flavors without explicit target platform'); // Add a few flavors. - await project.addProductFlavors( ['production', 'staging', 'development']); + await project.addProductFlavors( [ + 'production', + 'staging', + 'development', + 'flavor_underscore', // https://github.com/flutter/flutter/issues/36067 + ]); // Build the production flavor in release mode. await project.runGradleTask('bundleProductionRelease'); - final String bundlePath = path.join( - project.rootPath, - 'build', - 'app', - 'outputs', - 'bundle', - 'productionRelease', - 'app.aab', - ); - - final Iterable bundleFiles = await getFilesInAppBundle(bundlePath); - + final String bundleFromGradlePath = path.join( + project.rootPath, + 'build', + 'app', + 'outputs', + 'bundle', + 'productionRelease', + 'app.aab', + ); checkItContains([ 'base/manifest/AndroidManifest.xml', 'base/dex/classes.dex', @@ -64,7 +63,78 @@ Future main() async { 'base/lib/arm64-v8a/libflutter.so', 'base/lib/armeabi-v7a/libapp.so', 'base/lib/armeabi-v7a/libflutter.so', - ], bundleFiles); + ], await getFilesInAppBundle(bundleFromGradlePath)); + + section('Build app bundle using the flutter tool - flavor: flavor_underscore'); + + int exitCode; + await inDirectory(project.rootPath, () async { + exitCode = await flutter( + 'build', + options: [ + 'appbundle', + '--flavor=flavor_underscore', + '--verbose', + ], + ); + }); + + if (exitCode != 0) { + throw TaskResult.failure('flutter build appbundle command exited with code: $exitCode'); + } + + final String flavorUnderscoreBundlePath = path.join( + project.rootPath, + 'build', + 'app', + 'outputs', + 'bundle', + 'flavor_underscoreRelease', + 'app.aab', + ); + checkItContains([ + 'base/manifest/AndroidManifest.xml', + 'base/dex/classes.dex', + 'base/lib/arm64-v8a/libapp.so', + 'base/lib/arm64-v8a/libflutter.so', + 'base/lib/armeabi-v7a/libapp.so', + 'base/lib/armeabi-v7a/libflutter.so', + ], await getFilesInAppBundle(flavorUnderscoreBundlePath)); + + section('Build app bundle using the flutter tool - flavor: production'); + + await inDirectory(project.rootPath, () async { + exitCode = await flutter( + 'build', + options: [ + 'appbundle', + '--flavor=production', + '--verbose', + ], + ); + }); + + if (exitCode != 0) { + throw TaskResult.failure('flutter build appbundle command exited with code: $exitCode'); + } + + final String productionBundlePath = path.join( + project.rootPath, + 'build', + 'app', + 'outputs', + 'bundle', + 'productionRelease', + 'app.aab', + ); + checkItContains([ + 'base/manifest/AndroidManifest.xml', + 'base/dex/classes.dex', + 'base/lib/arm64-v8a/libapp.so', + 'base/lib/arm64-v8a/libflutter.so', + 'base/lib/armeabi-v7a/libapp.so', + 'base/lib/armeabi-v7a/libflutter.so', + ], await getFilesInAppBundle(productionBundlePath)); }); await runProjectTest((FlutterProject project) async { @@ -73,14 +143,14 @@ Future main() async { options: ['-Ptarget-platform=android-arm']); final String releaseBundle = path.join( - project.rootPath, - 'build', - 'app', - 'outputs', - 'bundle', - 'release', - 'app.aab', - ); + project.rootPath, + 'build', + 'app', + 'outputs', + 'bundle', + 'release', + 'app.aab', + ); final Iterable bundleFiles = await getFilesInAppBundle(releaseBundle); diff --git a/packages/flutter_tools/lib/src/android/gradle.dart b/packages/flutter_tools/lib/src/android/gradle.dart index ce041479e1..e2a2adddb3 100644 --- a/packages/flutter_tools/lib/src/android/gradle.dart +++ b/packages/flutter_tools/lib/src/android/gradle.dart @@ -775,7 +775,7 @@ Future _buildGradleProjectV2( color: TerminalColor.green); } } else { - final File bundleFile = _findBundleFile(project, buildInfo); + final File bundleFile = findBundleFile(project, buildInfo); if (bundleFile == null) throwToolExit('Gradle build failed to produce an Android bundle package.'); @@ -820,24 +820,30 @@ Iterable findApkFiles(GradleProject project, AndroidBuildInfo androidBuild }); } -File _findBundleFile(GradleProject project, BuildInfo buildInfo) { +@visibleForTesting +File findBundleFile(GradleProject project, BuildInfo buildInfo) { final String bundleFileName = project.bundleFileFor(buildInfo); - - if (bundleFileName == null) + if (bundleFileName == null) { return null; - final String modeName = camelCase(buildInfo.modeName); - File bundleFile = project.bundleDirectory.childDirectory(modeName).childFile(bundleFileName); - if (bundleFile.existsSync()) + } + File bundleFile = project.bundleDirectory + .childDirectory(camelCase(buildInfo.modeName)) + .childFile(bundleFileName); + if (bundleFile.existsSync()) { + return bundleFile; + } + if (buildInfo.flavor == null) { + return null; + } + // Android Studio Gradle plugin v3 adds the flavor to the path. For the bundle the + // folder name is the flavor plus the mode name. On Windows, filenames aren't case sensitive. + // For example: foo_barRelease where `foo_bar` is the flavor and `Release` the mode name. + final String childDirName = '${buildInfo.flavor}${camelCase('_' + buildInfo.modeName)}'; + bundleFile = project.bundleDirectory + .childDirectory(childDirName) + .childFile(bundleFileName); + if (bundleFile.existsSync()) { return bundleFile; - if (buildInfo.flavor != null) { - - // Android Studio Gradle plugin v3 adds the flavor to the path. For the bundle the folder name is the flavor plus the mode name. - // On linux, filenames are case sensitive. - bundleFile = project.bundleDirectory - .childDirectory(camelCase('${buildInfo.flavor}_$modeName')) - .childFile(bundleFileName); - if (bundleFile.existsSync()) - return bundleFile; } return null; } diff --git a/packages/flutter_tools/test/general.shard/android/gradle_test.dart b/packages/flutter_tools/test/general.shard/android/gradle_test.dart index d6380f0b3b..623c0a8497 100644 --- a/packages/flutter_tools/test/general.shard/android/gradle_test.dart +++ b/packages/flutter_tools/test/general.shard/android/gradle_test.dart @@ -117,6 +117,150 @@ void main() { expect(ndkMessageFilter.hasMatch(m), isTrue); } }); + + testUsingContext('Finds app bundle when flavor contains underscores in release mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('foo_barRelease'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.release, 'foo_bar')); + expect(bundle, isNotNull); + expect(bundle.path, '/foo_barRelease/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when flavor doesn\'t contain underscores in release mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('fooRelease'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.release, 'foo')); + expect(bundle, isNotNull); + expect(bundle.path, '/fooRelease/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when no flavor is used in release mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('release'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.release, null)); + expect(bundle, isNotNull); + expect(bundle.path, '/release/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when flavor contains underscores in debug mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('foo_barDebug'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.debug, 'foo_bar')); + expect(bundle, isNotNull); + expect(bundle.path, '/foo_barDebug/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when flavor doesn\'t contain underscores in debug mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('fooDebug'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.debug, 'foo')); + expect(bundle, isNotNull); + expect(bundle.path, '/fooDebug/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when no flavor is used in debug mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('debug'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.debug, null)); + expect(bundle, isNotNull); + expect(bundle.path, '/debug/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when flavor contains underscores in profile mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('foo_barProfile'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.profile, 'foo_bar')); + expect(bundle, isNotNull); + expect(bundle.path, '/foo_barProfile/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when flavor doesn\'t contain underscores in profile mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('fooProfile'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.profile, 'foo')); + expect(bundle, isNotNull); + expect(bundle.path, '/fooProfile/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); + + testUsingContext('Finds app bundle when no flavor is used in profile mode', () { + final GradleProject gradleProject = MockGradleProject(); + when(gradleProject.bundleDirectory).thenReturn(fs.currentDirectory); + when(gradleProject.bundleFileFor(any)).thenReturn('app.aab'); + + final Directory aabDirectory = gradleProject.bundleDirectory.childDirectory('profile'); + fs.directory(aabDirectory).createSync(recursive: true); + fs.file(fs.path.join(aabDirectory.path, 'app.aab')).writeAsStringSync('irrelevant'); + + final File bundle = findBundleFile(gradleProject, const BuildInfo(BuildMode.profile, null)); + expect(bundle, isNotNull); + expect(bundle.path, '/profile/app.aab'); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + }); }); group('gradle project', () {