Skip to content

Commit 00e2228

Browse files
committed
Fix flutter#3891 by making flutter run generate identical apks to flutter build apk
1 parent 3252701 commit 00e2228

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

packages/flutter_tools/lib/src/commands/build_apk.dart

+30-42
Original file line numberDiff line numberDiff line change
@@ -201,54 +201,19 @@ class BuildApkCommand extends FlutterCommand {
201201

202202
@override
203203
Future<int> runInProject() async {
204-
// Validate that we can find an android sdk.
205-
if (androidSdk == null) {
206-
printError('No Android SDK found. Try setting the ANDROID_HOME environment variable.');
207-
return 1;
208-
}
209-
210-
List<String> validationResult = androidSdk.validateSdkWellFormed();
211-
if (validationResult.isNotEmpty) {
212-
validationResult.forEach(printError);
213-
printError('Try re-installing or updating your Android SDK.');
214-
return 1;
215-
}
216-
217-
BuildMode mode = getBuildMode();
218-
219-
Map<String, File> extraFiles = <String, File>{};
220-
for (String addFile in argResults['add-file']) {
221-
List<String> keyValue = addFile.split('=');
222-
if (keyValue.length != 2) {
223-
printError('add-file option must have the format <path/in/APK>=<local/file/path>');
224-
return 1;
225-
}
226-
extraFiles[keyValue.first] = new File(keyValue.last);
227-
}
228-
229-
if (FileSystemEntity.isDirectorySync(_kDefaultAssetsPath)) {
230-
Directory assetsDir = new Directory(_kDefaultAssetsPath);
231-
for (FileSystemEntity entity in assetsDir.listSync(recursive: true)) {
232-
if (entity is File) {
233-
String targetPath = entity.path.substring(assetsDir.path.length);
234-
extraFiles["assets/$targetPath"] = entity;
235-
}
236-
};
237-
}
238-
239204
// TODO(devoncarew): This command should take an arg for the output type (arm / x64).
240205

241206
return await buildAndroid(
242207
TargetPlatform.android_arm,
243-
mode,
208+
getBuildMode(),
244209
force: true,
245210
manifest: argResults['manifest'],
246211
resources: argResults['resources'],
247212
outputFile: argResults['output-file'],
248213
target: argResults['target'],
249214
flxPath: argResults['flx'],
250215
aotPath: argResults['aot-path'],
251-
extraFiles: extraFiles,
216+
addFiles: argResults['add-file'],
252217
keystore: (argResults['keystore'] ?? '').isEmpty ? null : new ApkKeystoreInfo(
253218
keystore: argResults['keystore'],
254219
password: argResults['keystore-password'],
@@ -427,16 +392,19 @@ int _signApk(
427392
}
428393

429394
// Returns true if the apk is out of date and needs to be rebuilt.
430-
bool _needsRebuild(String apkPath, String manifest) {
395+
bool _needsRebuild(String apkPath, String manifest, Map<String, File> extraFiles) {
431396
FileStat apkStat = FileStat.statSync(apkPath);
432397
// Note: This list of dependencies is imperfect, but will do for now. We
433398
// purposely don't include the .dart files, because we can load those
434399
// over the network without needing to rebuild (at least on Android).
435-
Iterable<FileStat> dependenciesStat = <String>[
400+
List<String> dependencies = <String>[
436401
manifest,
437402
_kFlutterManifestPath,
438403
_kPackagesStatusPath
439-
].map((String path) => FileStat.statSync(path));
404+
];
405+
dependencies.addAll(extraFiles.values.map((File file) => file.path));
406+
Iterable<FileStat> dependenciesStat =
407+
dependencies.map((String path) => FileStat.statSync(path));
440408

441409
if (apkStat.type == FileSystemEntityType.NOT_FOUND)
442410
return true;
@@ -462,7 +430,7 @@ Future<int> buildAndroid(
462430
String target,
463431
String flxPath,
464432
String aotPath,
465-
Map<String, File> extraFiles,
433+
List<String> addFiles,
466434
ApkKeystoreInfo keystore
467435
}) async {
468436
// Validate that we can find an android sdk.
@@ -478,7 +446,27 @@ Future<int> buildAndroid(
478446
return 1;
479447
}
480448

481-
if (!force && !_needsRebuild(outputFile, manifest)) {
449+
Map<String, File> extraFiles = <String, File>{};
450+
for (String addFile in addFiles ?? <String>[]) {
451+
List<String> keyValue = addFile.split('=');
452+
if (keyValue.length != 2) {
453+
printError('add-file option must have the format <path/in/APK>=<local/file/path>');
454+
return 1;
455+
}
456+
extraFiles[keyValue.first] = new File(keyValue.last);
457+
}
458+
459+
if (FileSystemEntity.isDirectorySync(_kDefaultAssetsPath)) {
460+
Directory assetsDir = new Directory(_kDefaultAssetsPath);
461+
for (FileSystemEntity entity in assetsDir.listSync(recursive: true)) {
462+
if (entity is File) {
463+
String targetPath = entity.path.substring(assetsDir.path.length);
464+
extraFiles["assets/$targetPath"] = entity;
465+
}
466+
};
467+
}
468+
469+
if (!force && !_needsRebuild(outputFile, manifest, extraFiles)) {
482470
printTrace('APK up to date; skipping build step.');
483471
return 0;
484472
}

0 commit comments

Comments
 (0)