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