Teach FlutterPlugin.groovy about AOT builds (#4389)
The `flutter.buildMode` property now controls whether to build in debug, profiling, or release.
This commit is contained in:
parent
6c96e1a320
commit
8f03ebe56b
@ -17,6 +17,8 @@ There are a number of other parameters you can control with this file:
|
||||
|
||||
* `FLUTTER_APPLICATION_PATH`: The path that contains your `pubspec.yaml` file
|
||||
relative to your `xcodeproj` file.
|
||||
* `FLUTTER_BUILD_MODE`: Whether to build for `debug`, `profile`, or `release`.
|
||||
Defaults to `release`.
|
||||
* `FLUTTER_TARGET`: The path to your `main.dart` relative to your
|
||||
`pubspec.yaml`. Defaults to `lib/main.dart`.
|
||||
* `FLUTTER_FRAMEWORK_DIR`: The absolute path to the directory that contains
|
||||
@ -37,6 +39,14 @@ Create an `android/local.properties` file with these entries:
|
||||
* `sdk.dir=[path to the Android SDK]`
|
||||
* `flutter.sdk=[path to the Flutter SDK]`
|
||||
|
||||
There are a number of other parameters you can control with this file:
|
||||
|
||||
* `flutter.buildMode`: Whether to build for `debug`, `profile`, or `release`.
|
||||
Defaults to `release`.
|
||||
* `flutter.jar`: The path to `flutter.jar`. Defaults to the
|
||||
`android-arm-release` version of `flutter.jar` in the `bin/cache` directory
|
||||
of the Flutter SDK.
|
||||
|
||||
### Build
|
||||
|
||||
To build direction with gradle, use the following commands:
|
||||
|
@ -11,6 +11,7 @@ import org.gradle.api.GradleException
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Task
|
||||
import org.gradle.api.file.CopySpec
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.tasks.Copy
|
||||
import org.gradle.api.tasks.InputDirectory
|
||||
@ -18,7 +19,8 @@ import org.gradle.api.tasks.OutputDirectory
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
|
||||
class FlutterPlugin implements Plugin<Project> {
|
||||
private File sdkDir
|
||||
private File flutterRoot
|
||||
private String buildMode
|
||||
private String localEngine
|
||||
|
||||
@Override
|
||||
@ -26,15 +28,23 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
Properties properties = new Properties()
|
||||
properties.load(project.rootProject.file("local.properties").newDataInputStream())
|
||||
|
||||
String sdkPath = properties.getProperty("flutter.sdk")
|
||||
if (sdkPath == null) {
|
||||
String flutterRootPath = properties.getProperty("flutter.sdk")
|
||||
if (flutterRootPath == null) {
|
||||
throw new GradleException("flutter.sdk must be defined in local.properties")
|
||||
}
|
||||
sdkDir = project.file(sdkPath)
|
||||
if (!sdkDir.isDirectory()) {
|
||||
flutterRoot = project.file(flutterRootPath)
|
||||
if (!flutterRoot.isDirectory()) {
|
||||
throw new GradleException("flutter.sdk must point to the Flutter SDK directory")
|
||||
}
|
||||
|
||||
buildMode = properties.getProperty("flutter.buildMode")
|
||||
if (buildMode == null) {
|
||||
buildMode = "release"
|
||||
}
|
||||
if (!["debug", "profile", "release"].contains(buildMode)) {
|
||||
throw new GradleException("flutter.buildMode must be one of \"debug\", \"profile\", or \"release\" but was \"${buildMode}\"")
|
||||
}
|
||||
|
||||
File flutterJar
|
||||
String flutterJarPath = properties.getProperty("flutter.jar")
|
||||
if (flutterJarPath != null) {
|
||||
@ -43,11 +53,20 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
throw new GradleException("flutter.jar must point to a Flutter engine JAR")
|
||||
}
|
||||
} else {
|
||||
flutterJar = new File(sdkDir, Joiner.on(File.separatorChar).join(
|
||||
"bin", "cache", "artifacts", "engine", "android-arm", "flutter.jar"))
|
||||
// TODO(abarth): Support x64 and x86 in addition to arm.
|
||||
String artifactType = "unknown";
|
||||
if (buildMode == "debug") {
|
||||
artifactType = "android-arm"
|
||||
} else if (buildMode == "profile") {
|
||||
artifactType = "android-arm-profile"
|
||||
} else if (buildMode == "release") {
|
||||
artifactType = "android-arm-release"
|
||||
}
|
||||
flutterJar = new File(flutterRoot, Joiner.on(File.separatorChar).join(
|
||||
"bin", "cache", "artifacts", "engine", artifactType, "flutter.jar"))
|
||||
if (!flutterJar.isFile()) {
|
||||
project.exec {
|
||||
executable "${sdkDir}/bin/flutter"
|
||||
executable "${flutterRoot}/bin/flutter"
|
||||
args "precache"
|
||||
}
|
||||
if (!flutterJar.isFile()) {
|
||||
@ -69,18 +88,19 @@ class FlutterPlugin implements Plugin<Project> {
|
||||
}
|
||||
|
||||
FlutterTask flutterTask = project.tasks.create("flutterBuild", FlutterTask) {
|
||||
sdkDir this.sdkDir
|
||||
flutterRoot this.flutterRoot
|
||||
buildMode this.buildMode
|
||||
localEngine this.localEngine
|
||||
sourceDir project.file(project.flutter.source)
|
||||
intermediateDir project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter")
|
||||
localEngine this.localEngine
|
||||
}
|
||||
|
||||
project.android.applicationVariants.all { variant ->
|
||||
Task copyFlxTask = project.tasks.create(name: "copyFlx${variant.name.capitalize()}", type: Copy) {
|
||||
Task copyFlxTask = project.tasks.create(name: "copyFlutterAssets${variant.name.capitalize()}", type: Copy) {
|
||||
dependsOn flutterTask
|
||||
dependsOn variant.mergeAssets
|
||||
from flutterTask.flxPath
|
||||
into variant.mergeAssets.outputDir
|
||||
with flutterTask.assets
|
||||
}
|
||||
variant.outputs[0].processResources.dependsOn(copyFlxTask)
|
||||
}
|
||||
@ -92,7 +112,9 @@ class FlutterExtension {
|
||||
}
|
||||
|
||||
class FlutterTask extends DefaultTask {
|
||||
File sdkDir
|
||||
File flutterRoot
|
||||
String buildMode
|
||||
String localEngine
|
||||
|
||||
@InputDirectory
|
||||
File sourceDir
|
||||
@ -100,10 +122,16 @@ class FlutterTask extends DefaultTask {
|
||||
@OutputDirectory
|
||||
File intermediateDir
|
||||
|
||||
String localEngine
|
||||
|
||||
String getFlxPath() {
|
||||
return "${intermediateDir}/app.flx"
|
||||
CopySpec getAssets() {
|
||||
return project.copySpec {
|
||||
from "${intermediateDir}/app.flx"
|
||||
if (buildMode != 'debug') {
|
||||
from "${intermediateDir}/snapshot_aot_instr"
|
||||
from "${intermediateDir}/snapshot_aot_isolate"
|
||||
from "${intermediateDir}/snapshot_aot_rodata"
|
||||
from "${intermediateDir}/snapshot_aot_vmisolate"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
@ -113,16 +141,35 @@ class FlutterTask extends DefaultTask {
|
||||
}
|
||||
|
||||
intermediateDir.mkdirs()
|
||||
|
||||
if (buildMode != "debug") {
|
||||
project.exec {
|
||||
executable "${flutterRoot}/bin/flutter"
|
||||
workingDir sourceDir
|
||||
if (localEngine != null) {
|
||||
args "--local-engine", localEngine
|
||||
}
|
||||
args "build", "aot"
|
||||
args "--target-platform", "android-arm"
|
||||
args "--output-dir", "${intermediateDir}"
|
||||
args "--${buildMode}"
|
||||
}
|
||||
}
|
||||
|
||||
project.exec {
|
||||
executable "${sdkDir}/bin/flutter"
|
||||
executable "${flutterRoot}/bin/flutter"
|
||||
workingDir sourceDir
|
||||
if (localEngine != null) {
|
||||
args "--local-engine", localEngine
|
||||
}
|
||||
args "build", "flx"
|
||||
args "-o", flxPath
|
||||
args "--snapshot", "${intermediateDir}/snapshot_blob.bin"
|
||||
args "--depfile", "${intermediateDir}/snapshot_blob.bin.d"
|
||||
args "--output-file", "${intermediateDir}/app.flx"
|
||||
if (buildMode != "debug") {
|
||||
args "--precompiled"
|
||||
} else {
|
||||
args "--snapshot", "${intermediateDir}/snapshot_blob.bin"
|
||||
args "--depfile", "${intermediateDir}/snapshot_blob.bin.d"
|
||||
}
|
||||
args "--working-dir", "${intermediateDir}/flx"
|
||||
}
|
||||
}
|
||||
|
@ -33,17 +33,17 @@ BuildApp() {
|
||||
target_path=${FLUTTER_TARGET}
|
||||
fi
|
||||
|
||||
local flutter_mode="release"
|
||||
if [[ -n "$FLUTTER_MODE" ]]; then
|
||||
flutter_mode=${FLUTTER_MODE}
|
||||
local build_mode="release"
|
||||
if [[ -n "$FLUTTER_BUILD_MODE" ]]; then
|
||||
build_mode=${FLUTTER_BUILD_MODE}
|
||||
fi
|
||||
|
||||
local artifact_variant="unknown"
|
||||
case "$flutter_mode" in
|
||||
case "$build_mode" in
|
||||
release) artifact_variant="ios-release";;
|
||||
profile) artifact_variant="ios-profile";;
|
||||
debug) artifact_variant="ios";;
|
||||
*) echo "Unknown FLUTTER_MODE: $FLUTTER_MODE";;
|
||||
*) echo "Unknown FLUTTER_BUILD_MODE: $FLUTTER_BUILD_MODE";;
|
||||
esac
|
||||
|
||||
local framework_path="${FLUTTER_ROOT}/bin/cache/artifacts/engine/${artifact_variant}"
|
||||
@ -72,10 +72,10 @@ BuildApp() {
|
||||
|
||||
if [[ $CURRENT_ARCH != "x86_64" ]]; then
|
||||
local aot_flags=""
|
||||
if [[ "$flutter_mode" == "debug" ]]; then
|
||||
if [[ "$build_mode" == "debug" ]]; then
|
||||
aot_flags="--interpreter --debug"
|
||||
else
|
||||
aot_flags="--${flutter_mode}"
|
||||
aot_flags="--${build_mode}"
|
||||
fi
|
||||
|
||||
RunCommand ${FLUTTER_ROOT}/bin/flutter --suppress-analytics build aot \
|
||||
@ -95,7 +95,7 @@ BuildApp() {
|
||||
fi
|
||||
|
||||
local precompilation_flag=""
|
||||
if [[ $CURRENT_ARCH != "x86_64" ]] && [[ "$flutter_mode" != "debug" ]]; then
|
||||
if [[ $CURRENT_ARCH != "x86_64" ]] && [[ "$build_mode" != "debug" ]]; then
|
||||
precompilation_flag="--precompiled"
|
||||
fi
|
||||
|
||||
|
@ -70,7 +70,7 @@ void updateXcodeGeneratedProperties(String projectPath, BuildMode mode, String t
|
||||
localsBuffer.writeln('FLUTTER_TARGET=$target');
|
||||
|
||||
// The runtime mode for the current build.
|
||||
localsBuffer.writeln('FLUTTER_MODE=${getModeName(mode)}');
|
||||
localsBuffer.writeln('FLUTTER_BUILD_MODE=${getModeName(mode)}');
|
||||
|
||||
String flutterFrameworkDir = path.normalize(tools.getEngineArtifactsDirectory(TargetPlatform.ios, mode).path);
|
||||
localsBuffer.writeln('FLUTTER_FRAMEWORK_DIR=$flutterFrameworkDir');
|
||||
|
Loading…
x
Reference in New Issue
Block a user