Put Gradle output in build/ (#8759)
Changed the default build output directory in the new project template to build/, instead of android/build/ and android/app/build/. Updated tools to ask the Gradle scripts what the build directory is, since this is configurable in the build scripts, and we need to know where the build output actually is. Silenced output from 'flutter build aot' when invoked from Gradle, since the output was confusing in this case. Fixes #8723 Fixes #8656 Fixes #8138
This commit is contained in:
@@ -265,6 +265,7 @@ class FlutterTask extends DefaultTask {
|
||||
args "--local-engine-src-path", localEngineSrcPath
|
||||
}
|
||||
args "build", "aot"
|
||||
args "--quiet"
|
||||
args "--target", targetPath
|
||||
args "--target-platform", "android-arm"
|
||||
args "--output-dir", "${intermediateDir}"
|
||||
|
||||
@@ -20,8 +20,7 @@ import 'android_studio.dart';
|
||||
|
||||
const String gradleManifestPath = 'android/app/src/main/AndroidManifest.xml';
|
||||
const String gradleAppOutV1 = 'android/app/build/outputs/apk/app-debug.apk';
|
||||
const String gradleAppOutV2 = 'android/app/build/outputs/apk/app.apk';
|
||||
const String gradleAppOutDir = 'android/app/build/outputs/apk';
|
||||
const String gradleAppOutDirV1 = 'android/app/build/outputs/apk';
|
||||
|
||||
enum FlutterPluginVersion {
|
||||
none,
|
||||
@@ -63,11 +62,37 @@ String get gradleAppOut {
|
||||
case FlutterPluginVersion.managed:
|
||||
// Fall through. The managed plugin matches plugin v2 for now.
|
||||
case FlutterPluginVersion.v2:
|
||||
return gradleAppOutV2;
|
||||
return '$gradleAppOutDirV2/app.apk';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String get gradleAppOutDirV2 {
|
||||
final String gradle = ensureGradle();
|
||||
try {
|
||||
final String properties = runCheckedSync(
|
||||
<String>[gradle, 'app:properties'],
|
||||
workingDirectory: 'android',
|
||||
hideStdout: true,
|
||||
);
|
||||
String buildDir = properties
|
||||
.split('\n')
|
||||
.firstWhere((String s) => s.startsWith('buildDir: '))
|
||||
.substring('buildDir: '.length)
|
||||
.trim();
|
||||
final String currentDirectory = fs.currentDirectory.path;
|
||||
if (buildDir.startsWith(currentDirectory)) {
|
||||
// Relativize path, snip current directory + separating '/'.
|
||||
buildDir = buildDir.substring(currentDirectory.length + 1);
|
||||
}
|
||||
return '$buildDir/outputs/apk';
|
||||
} catch (e) {
|
||||
printError('Error running gradle: $e');
|
||||
}
|
||||
// Fall back to the default
|
||||
return gradleAppOutDirV1;
|
||||
}
|
||||
|
||||
String locateSystemGradle({ bool ensureExecutable: true }) {
|
||||
final String gradle = gradleExecutable;
|
||||
if (ensureExecutable && gradle != null) {
|
||||
@@ -82,15 +107,16 @@ String locateProjectGradlew({ bool ensureExecutable: true }) {
|
||||
final String path = 'android/gradlew';
|
||||
|
||||
if (fs.isFileSync(path)) {
|
||||
final File gradle = fs.file(path);
|
||||
if (ensureExecutable)
|
||||
os.makeExecutable(fs.file(path));
|
||||
return path;
|
||||
os.makeExecutable(gradle);
|
||||
return gradle.absolute.path;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> ensureGradle() async {
|
||||
String ensureGradle() {
|
||||
String gradle = locateProjectGradlew();
|
||||
if (gradle == null) {
|
||||
gradle = locateSystemGradle();
|
||||
@@ -120,7 +146,7 @@ Future<Null> buildGradleProject(BuildMode buildMode, String target) async {
|
||||
settings.values['flutter.buildMode'] = buildModeName;
|
||||
settings.writeContents(localProperties);
|
||||
|
||||
final String gradle = await ensureGradle();
|
||||
final String gradle = ensureGradle();
|
||||
|
||||
switch (flutterPluginVersion) {
|
||||
case FlutterPluginVersion.none:
|
||||
@@ -182,14 +208,15 @@ Future<Null> buildGradleProjectV2(String gradle, String buildModeName, String ta
|
||||
if (exitcode != 0)
|
||||
throwToolExit('Gradle build failed: $exitcode', exitCode: exitcode);
|
||||
|
||||
final String buildDirectory = gradleAppOutDirV2;
|
||||
final String apkFilename = 'app-$buildModeName.apk';
|
||||
final File apkFile = fs.file('$gradleAppOutDir/$apkFilename');
|
||||
final File apkFile = fs.file('$buildDirectory/$apkFilename');
|
||||
// Copy the APK to app.apk, so `flutter run`, `flutter install`, etc. can find it.
|
||||
apkFile.copySync('$gradleAppOutDir/app.apk');
|
||||
apkFile.copySync('$buildDirectory/app.apk');
|
||||
|
||||
printTrace('calculateSha: $gradleAppOutDir/app.apk');
|
||||
final File apkShaFile = fs.file('$gradleAppOutDir/app.apk.sha1');
|
||||
printTrace('calculateSha: $buildDirectory/app.apk');
|
||||
final File apkShaFile = fs.file('$buildDirectory/app.apk.sha1');
|
||||
apkShaFile.writeAsStringSync(calculateSha(apkFile));
|
||||
|
||||
printStatus('Built $apkFilename (${getSizeAsMB(apkFile.lengthSync())}).');
|
||||
printStatus('Built ${apkFile.path} (${getSizeAsMB(apkFile.lengthSync())}).');
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ class BuildAotCommand extends BuildSubCommand {
|
||||
defaultsTo: 'android-arm',
|
||||
allowed: <String>['android-arm', 'ios']
|
||||
)
|
||||
..addFlag('interpreter');
|
||||
..addFlag('interpreter')
|
||||
..addFlag('quiet', defaultsTo: false);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -51,8 +52,11 @@ class BuildAotCommand extends BuildSubCommand {
|
||||
throwToolExit('Unknown platform: $targetPlatform');
|
||||
|
||||
final String typeName = artifacts.getEngineType(platform, getBuildMode());
|
||||
final Status status = logger.startProgress('Building AOT snapshot in ${getModeName(getBuildMode())} mode ($typeName)...',
|
||||
expectSlowOperation: true);
|
||||
Status status;
|
||||
if (!argResults['quiet']) {
|
||||
status = logger.startProgress('Building AOT snapshot in ${getModeName(getBuildMode())} mode ($typeName)...',
|
||||
expectSlowOperation: true);
|
||||
}
|
||||
final String outputPath = await buildAotSnapshot(
|
||||
findMainDartFile(targetFile),
|
||||
platform,
|
||||
@@ -60,12 +64,17 @@ class BuildAotCommand extends BuildSubCommand {
|
||||
outputPath: argResults['output-dir'],
|
||||
interpreter: argResults['interpreter']
|
||||
);
|
||||
status.stop();
|
||||
status?.stop();
|
||||
|
||||
if (outputPath == null)
|
||||
throwToolExit(null);
|
||||
|
||||
printStatus('Built to $outputPath${fs.path.separator}.');
|
||||
final String builtMessage = 'Built to $outputPath${fs.path.separator}.';
|
||||
if (argResults['quiet']) {
|
||||
printTrace(builtMessage);
|
||||
} else {
|
||||
printStatus(builtMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,11 @@ allprojects {
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.buildDir = '../build'
|
||||
subprojects {
|
||||
project.buildDir = "${rootProject.buildDir}/${project.name}"
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user