Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 3d94b8f

Browse files
authored
[tool] Install the corresponding APK in flutter run (#113622)
1 parent 1b4b99b commit 3d94b8f

9 files changed

+77
-55
lines changed

dev/devicelab/bin/tasks/run_release_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ void main() {
114114

115115
_findNextMatcherInList(
116116
stdout,
117-
(String line) => line.startsWith('Installing build/app/outputs/flutter-apk/app.apk...'),
118-
'Installing build/app/outputs/flutter-apk/app.apk...',
117+
(String line) => line.startsWith('Installing build/app/outputs/flutter-apk/app-release.apk...'),
118+
'Installing build/app/outputs/flutter-apk/app-release.apk...',
119119
);
120120

121121
_findNextMatcherInList(

packages/flutter_tools/lib/src/android/application_package.dart

+12-3
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,20 @@ class AndroidApk extends ApplicationPackage implements PrebuiltApplicationPackag
103103
required ProcessUtils processUtils,
104104
required Logger logger,
105105
required FileSystem fileSystem,
106+
BuildInfo? buildInfo,
106107
}) async {
107-
File apkFile;
108+
final File apkFile;
109+
final String filename;
110+
if (buildInfo == null) {
111+
filename = 'app.apk';
112+
} else if (buildInfo.flavor == null) {
113+
filename = 'app-${buildInfo.mode.name}.apk';
114+
} else {
115+
filename = 'app-${buildInfo.lowerCasedFlavor}-${buildInfo.mode.name}.apk';
116+
}
108117

109118
if (androidProject.isUsingGradle && androidProject.isSupportedVersion) {
110-
apkFile = getApkDirectory(androidProject.parent).childFile('app.apk');
119+
apkFile = getApkDirectory(androidProject.parent).childFile(filename);
111120
if (apkFile.existsSync()) {
112121
// Grab information from the .apk. The gradle build script might alter
113122
// the application Id, so we need to look at what was actually built.
@@ -124,7 +133,7 @@ class AndroidApk extends ApplicationPackage implements PrebuiltApplicationPackag
124133
// command will grab a new AndroidApk after building, to get the updated
125134
// IDs.
126135
} else {
127-
apkFile = fileSystem.file(fileSystem.path.join(getAndroidBuildDirectory(), 'app.apk'));
136+
apkFile = fileSystem.file(fileSystem.path.join(getAndroidBuildDirectory(), filename));
128137
}
129138

130139
final File manifest = androidProject.appManifestFile;

packages/flutter_tools/lib/src/android/gradle.dart

+25-27
Original file line numberDiff line numberDiff line change
@@ -465,41 +465,39 @@ class AndroidGradleBuilder implements AndroidBuilder {
465465
);
466466
return;
467467
}
468-
// Gradle produced an APK.
468+
// Gradle produced APKs.
469469
final Iterable<String> apkFilesPaths = project.isModule
470470
? findApkFilesModule(project, androidBuildInfo, _logger, _usage)
471471
: listApkPaths(androidBuildInfo);
472472
final Directory apkDirectory = getApkDirectory(project);
473-
final File apkFile = apkDirectory.childFile(apkFilesPaths.first);
474-
if (!apkFile.existsSync()) {
475-
_exitWithExpectedFileNotFound(
476-
project: project,
477-
fileExtension: '.apk',
478-
logger: _logger,
479-
usage: _usage,
480-
);
481-
}
482473

483-
// Copy the first APK to app.apk, so `flutter run` can find it.
484-
// TODO(egarciad): Handle multiple APKs.
485-
apkFile.copySync(apkDirectory
486-
.childFile('app.apk')
487-
.path);
488-
_logger.printTrace('calculateSha: $apkDirectory/app.apk');
474+
// Generate sha1 for every generated APKs.
475+
for (final File apkFile in apkFilesPaths.map(apkDirectory.childFile)) {
476+
if (!apkFile.existsSync()) {
477+
_exitWithExpectedFileNotFound(
478+
project: project,
479+
fileExtension: '.apk',
480+
logger: _logger,
481+
usage: _usage,
482+
);
483+
}
489484

490-
final File apkShaFile = apkDirectory.childFile('app.apk.sha1');
491-
apkShaFile.writeAsStringSync(_calculateSha(apkFile));
485+
final String filename = apkFile.basename;
486+
_logger.printTrace('Calculate SHA1: $apkDirectory/$filename');
487+
final File apkShaFile = apkDirectory.childFile('$filename.sha1');
488+
apkShaFile.writeAsStringSync(_calculateSha(apkFile));
492489

493-
final String appSize = (buildInfo.mode == BuildMode.debug)
494-
? '' // Don't display the size when building a debug variant.
495-
: ' (${getSizeAsMB(apkFile.lengthSync())})';
496-
_logger.printStatus(
497-
'${_logger.terminal.successMark} Built ${_fileSystem.path.relative(apkFile.path)}$appSize.',
498-
color: TerminalColor.green,
499-
);
490+
final String appSize = (buildInfo.mode == BuildMode.debug)
491+
? '' // Don't display the size when building a debug variant.
492+
: ' (${getSizeAsMB(apkFile.lengthSync())})';
493+
_logger.printStatus(
494+
'${_logger.terminal.successMark} Built ${_fileSystem.path.relative(apkFile.path)}$appSize.',
495+
color: TerminalColor.green,
496+
);
500497

501-
if (buildInfo.codeSizeDirectory != null) {
502-
await _performCodeSizeAnalysis('apk', apkFile, androidBuildInfo);
498+
if (buildInfo.codeSizeDirectory != null) {
499+
await _performCodeSizeAnalysis('apk', apkFile, androidBuildInfo);
500+
}
503501
}
504502
}
505503

packages/flutter_tools/lib/src/flutter_application_package.dart

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class FlutterApplicationPackageFactory extends ApplicationPackageFactory {
6666
androidSdk: _androidSdk,
6767
userMessages: _userMessages,
6868
fileSystem: _fileSystem,
69+
buildInfo: buildInfo,
6970
);
7071
}
7172
return AndroidApk.fromApk(

packages/flutter_tools/test/general.shard/android/android_device_start_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void main() {
6565
platform: FakePlatform(),
6666
androidSdk: androidSdk,
6767
);
68-
final File apkFile = fileSystem.file('app.apk')..createSync();
68+
final File apkFile = fileSystem.file('app-debug.apk')..createSync();
6969
final AndroidApk apk = AndroidApk(
7070
id: 'FlutterApp',
7171
applicationPackage: apkFile,
@@ -88,7 +88,7 @@ void main() {
8888
command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', 'FlutterApp'],
8989
));
9090
processManager.addCommand(const FakeCommand(
91-
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', 'app.apk'],
91+
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', 'app-debug.apk'],
9292
));
9393
processManager.addCommand(kShaCommand);
9494
processManager.addCommand(const FakeCommand(
@@ -132,7 +132,7 @@ void main() {
132132
platform: FakePlatform(),
133133
androidSdk: androidSdk,
134134
);
135-
final File apkFile = fileSystem.file('app.apk')..createSync();
135+
final File apkFile = fileSystem.file('app-debug.apk')..createSync();
136136
final AndroidApk apk = AndroidApk(
137137
id: 'FlutterApp',
138138
applicationPackage: apkFile,
@@ -170,7 +170,7 @@ void main() {
170170
platform: FakePlatform(),
171171
androidSdk: androidSdk,
172172
);
173-
final File apkFile = fileSystem.file('app.apk')..createSync();
173+
final File apkFile = fileSystem.file('app-debug.apk')..createSync();
174174
final AndroidApk apk = AndroidApk(
175175
id: 'FlutterApp',
176176
applicationPackage: apkFile,
@@ -200,7 +200,7 @@ void main() {
200200
'-r',
201201
'--user',
202202
'10',
203-
'app.apk',
203+
'app-debug.apk',
204204
],
205205
stdout: '\n\nThe Dart VM service is listening on http://127.0.0.1:456\n\n',
206206
));

packages/flutter_tools/test/general.shard/android/android_install_test.dart

+14-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const FakeCommand kInstallCommand = FakeCommand(
3131
'-r',
3232
'--user',
3333
'10',
34-
'app.apk',
34+
'app-debug.apk',
3535
],
3636
);
3737
const FakeCommand kStoreShaCommand = FakeCommand(
@@ -71,7 +71,7 @@ void main() {
7171
stdout: '[ro.build.version.sdk]: [11]',
7272
),
7373
]);
74-
final File apk = fileSystem.file('app.apk')..createSync();
74+
final File apk = fileSystem.file('app-debug.apk')..createSync();
7575
final AndroidApk androidApk = AndroidApk(
7676
applicationPackage: apk,
7777
id: 'app',
@@ -87,7 +87,7 @@ void main() {
8787
});
8888

8989
testWithoutContext('Cannot install app if APK file is missing', () async {
90-
final File apk = fileSystem.file('app.apk');
90+
final File apk = fileSystem.file('app-debug.apk');
9191
final AndroidApk androidApk = AndroidApk(
9292
applicationPackage: apk,
9393
id: 'app',
@@ -115,7 +115,7 @@ void main() {
115115
kInstallCommand,
116116
kStoreShaCommand,
117117
]);
118-
final File apk = fileSystem.file('app.apk')..createSync();
118+
final File apk = fileSystem.file('app-debug.apk')..createSync();
119119
final AndroidApk androidApk = AndroidApk(
120120
applicationPackage: apk,
121121
id: 'app',
@@ -144,7 +144,7 @@ void main() {
144144
kInstallCommand,
145145
kStoreShaCommand,
146146
]);
147-
final File apk = fileSystem.file('app.apk')..createSync();
147+
final File apk = fileSystem.file('app-debug.apk')..createSync();
148148
final AndroidApk androidApk = AndroidApk(
149149
applicationPackage: apk,
150150
id: 'app',
@@ -182,13 +182,13 @@ void main() {
182182
'-r',
183183
'--user',
184184
'jane',
185-
'app.apk',
185+
'app-debug.apk',
186186
],
187187
exitCode: 1,
188188
stderr: 'Exception occurred while executing: java.lang.IllegalArgumentException: Bad user number: jane',
189189
),
190190
]);
191-
final File apk = fileSystem.file('app.apk')..createSync();
191+
final File apk = fileSystem.file('app-debug.apk')..createSync();
192192
final AndroidApk androidApk = AndroidApk(
193193
applicationPackage: apk,
194194
id: 'app',
@@ -221,8 +221,8 @@ void main() {
221221
stdout: 'example_sha',
222222
),
223223
]);
224-
final File apk = fileSystem.file('app.apk')..createSync();
225-
fileSystem.file('app.apk.sha1').writeAsStringSync('example_sha');
224+
final File apk = fileSystem.file('app-debug.apk')..createSync();
225+
fileSystem.file('app-debug.apk.sha1').writeAsStringSync('example_sha');
226226
final AndroidApk androidApk = AndroidApk(
227227
applicationPackage: apk,
228228
id: 'app',
@@ -254,16 +254,16 @@ void main() {
254254
stdout: 'different_example_sha',
255255
),
256256
const FakeCommand(
257-
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app.apk'],
257+
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app-debug.apk'],
258258
exitCode: 1,
259259
stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
260260
),
261261
const FakeCommand(command: <String>['adb', '-s', '1234', 'uninstall', '--user', '10', 'app']),
262262
kInstallCommand,
263263
const FakeCommand(command: <String>['adb', '-s', '1234', 'shell', 'echo', '-n', 'example_sha', '>', '/data/local/tmp/sky.app.sha1']),
264264
]);
265-
final File apk = fileSystem.file('app.apk')..createSync();
266-
fileSystem.file('app.apk.sha1').writeAsStringSync('example_sha');
265+
final File apk = fileSystem.file('app-debug.apk')..createSync();
266+
fileSystem.file('app-debug.apk.sha1').writeAsStringSync('example_sha');
267267
final AndroidApk androidApk = AndroidApk(
268268
applicationPackage: apk,
269269
id: 'app',
@@ -291,12 +291,12 @@ void main() {
291291
stdout: '\n'
292292
),
293293
const FakeCommand(
294-
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app.apk'],
294+
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app-debug.apk'],
295295
exitCode: 1,
296296
stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
297297
),
298298
]);
299-
final File apk = fileSystem.file('app.apk')..createSync();
299+
final File apk = fileSystem.file('app-debug.apk')..createSync();
300300
final AndroidApk androidApk = AndroidApk(
301301
applicationPackage: apk,
302302
id: 'app',

packages/flutter_tools/test/general.shard/android/gradle_test.dart

+14
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ void main() {
121121
});
122122

123123
group('listApkPaths', () {
124+
testWithoutContext('Finds APK without flavor in debug', () {
125+
final Iterable<String> apks = listApkPaths(
126+
const AndroidBuildInfo(BuildInfo(BuildMode.debug, '', treeShakeIcons: false)),
127+
);
128+
expect(apks, <String>['app-debug.apk']);
129+
});
130+
131+
testWithoutContext('Finds APK with flavor in debug', () {
132+
final Iterable<String> apks = listApkPaths(
133+
const AndroidBuildInfo(BuildInfo(BuildMode.debug, 'flavor1', treeShakeIcons: false)),
134+
);
135+
expect(apks, <String>['app-flavor1-debug.apk']);
136+
});
137+
124138
testWithoutContext('Finds APK without flavor in release', () {
125139
final Iterable<String> apks = listApkPaths(
126140
const AndroidBuildInfo(BuildInfo(BuildMode.release, '', treeShakeIcons: false)),

packages/flutter_tools/test/general.shard/application_package_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void main() {
5757

5858
testUsingContext('Licenses not available, platform and buildtools available, apk exists', () async {
5959
const String aaptPath = 'aaptPath';
60-
final File apkFile = globals.fs.file('app.apk');
60+
final File apkFile = globals.fs.file('app-debug.apk');
6161
final FakeAndroidSdkVersion sdkVersion = FakeAndroidSdkVersion();
6262
sdkVersion.aaptPath = aaptPath;
6363
sdk.latestVersion = sdkVersion;
@@ -81,7 +81,7 @@ void main() {
8181
TargetPlatform.android_arm,
8282
applicationBinary: apkFile,
8383
))!;
84-
expect(applicationPackage.name, 'app.apk');
84+
expect(applicationPackage.name, 'app-debug.apk');
8585
expect(applicationPackage, isA<PrebuiltApplicationPackage>());
8686
expect((applicationPackage as PrebuiltApplicationPackage).applicationPackage.path, apkFile.path);
8787
expect(fakeProcessManager, hasNoRemainingExpectations);
@@ -103,7 +103,7 @@ void main() {
103103

104104
await ApplicationPackageFactory.instance!.getPackageForPlatform(
105105
TargetPlatform.android_arm,
106-
applicationBinary: globals.fs.file('app.apk'),
106+
applicationBinary: globals.fs.file('app-debug.apk'),
107107
);
108108
expect(fakeProcessManager, hasNoRemainingExpectations);
109109
}, overrides: overrides);

packages/flutter_tools/test/general.shard/resident_runner_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void main() {
312312
<FlutterDevice>[
313313
flutterDevice,
314314
],
315-
applicationBinary: globals.fs.file('app.apk'),
315+
applicationBinary: globals.fs.file('app-debug.apk'),
316316
stayResident: false,
317317
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
318318
target: 'main.dart',

0 commit comments

Comments
 (0)