From 5dabe102a2581a134dbf10ffc7fc8458d9bd1c05 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Tue, 24 Jan 2023 16:19:09 -0500 Subject: [PATCH] Fix path name to discover debug apk on add2app builds (#117999) --- .../lib/src/android/application_package.dart | 8 +- .../application_package_test.dart | 83 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/android/application_package.dart b/packages/flutter_tools/lib/src/android/application_package.dart index b5ab7c49fc..021ea10e73 100644 --- a/packages/flutter_tools/lib/src/android/application_package.dart +++ b/packages/flutter_tools/lib/src/android/application_package.dart @@ -115,7 +115,13 @@ class AndroidApk extends ApplicationPackage implements PrebuiltApplicationPackag } if (androidProject.isUsingGradle && androidProject.isSupportedVersion) { - apkFile = getApkDirectory(androidProject.parent).childFile(filename); + Directory apkDirectory = getApkDirectory(androidProject.parent); + if (androidProject.parent.isModule) { + // Module builds output the apk in a subdirectory that corresponds + // to the buildmode of the apk. + apkDirectory = apkDirectory.childDirectory(buildInfo!.mode.name); + } + apkFile = apkDirectory.childFile(filename); if (apkFile.existsSync()) { // Grab information from the .apk. The gradle build script might alter // the application Id, so we need to look at what was actually built. diff --git a/packages/flutter_tools/test/general.shard/application_package_test.dart b/packages/flutter_tools/test/general.shard/application_package_test.dart index ce2d400b7f..e7e04cdfc6 100644 --- a/packages/flutter_tools/test/general.shard/application_package_test.dart +++ b/packages/flutter_tools/test/general.shard/application_package_test.dart @@ -55,6 +55,73 @@ void main() { ).path).createSync(recursive: true); }); + testUsingContext('correct debug filename in module projects', () async { + const String aaptPath = 'aaptPath'; + final File apkFile = globals.fs.file('app-debug.apk'); + final FakeAndroidSdkVersion sdkVersion = FakeAndroidSdkVersion(); + sdkVersion.aaptPath = aaptPath; + sdk.latestVersion = sdkVersion; + sdk.platformToolsAvailable = true; + sdk.licensesAvailable = false; + + fakeProcessManager.addCommand( + FakeCommand( + command: [ + aaptPath, + 'dump', + 'xmltree', + apkFile.path, + 'AndroidManifest.xml', + ], + stdout: _aaptDataWithDefaultEnabledAndMainLauncherActivity + ) + ); + + fakeProcessManager.addCommand( + FakeCommand( + command: [ + aaptPath, + 'dump', + 'xmltree', + fs.path.join('module_project', 'build', 'host', 'outputs', 'apk', 'debug', 'app-debug.apk'), + 'AndroidManifest.xml', + ], + stdout: _aaptDataWithDefaultEnabledAndMainLauncherActivity + ) + ); + + await ApplicationPackageFactory.instance!.getPackageForPlatform( + TargetPlatform.android_arm, + applicationBinary: apkFile, + ); + final BufferLogger logger = BufferLogger.test(); + final FlutterProject project = await aModuleProject(); + project.android.hostAppGradleRoot.childFile('build.gradle').createSync(recursive: true); + final File appGradle = project.android.hostAppGradleRoot.childFile( + fs.path.join('app', 'build.gradle')); + appGradle.createSync(recursive: true); + appGradle.writeAsStringSync("def flutterPluginVersion = 'managed'"); + final File apkDebugFile = project.directory + .childDirectory('build') + .childDirectory('host') + .childDirectory('outputs') + .childDirectory('apk') + .childDirectory('debug') + .childFile('app-debug.apk'); + apkDebugFile.createSync(recursive: true); + final AndroidApk? androidApk = await AndroidApk.fromAndroidProject( + project.android, + androidSdk: sdk, + processManager: fakeProcessManager, + userMessages: UserMessages(), + processUtils: ProcessUtils(processManager: fakeProcessManager, logger: logger), + logger: logger, + fileSystem: fs, + buildInfo: const BuildInfo(BuildMode.debug, null, treeShakeIcons: false), + ); + expect(androidApk, isNotNull); + }, overrides: overrides); + testUsingContext('Licenses not available, platform and buildtools available, apk exists', () async { const String aaptPath = 'aaptPath'; final File apkFile = globals.fs.file('app-debug.apk'); @@ -895,3 +962,19 @@ class FakeAndroidSdkVersion extends Fake implements AndroidSdkVersion { @override late String aaptPath; } + +Future aModuleProject() async { + final Directory directory = globals.fs.directory('module_project'); + directory + .childDirectory('.dart_tool') + .childFile('package_config.json') + ..createSync(recursive: true) + ..writeAsStringSync('{"configVersion":2,"packages":[]}'); + directory.childFile('pubspec.yaml').writeAsStringSync(''' +name: my_module +flutter: + module: + androidPackage: com.example +'''); + return FlutterProject.fromDirectory(directory); +}