2017-01-31 22:48:48 +00:00
import java.nio.file.Path
import java.nio.file.Paths
2016-03-12 00:30:56 +00:00
import com.android.builder.model.AndroidProject
2017-02-16 22:17:09 +00:00
import org.apache.tools.ant.taskdefs.condition.Os
2016-03-12 00:30:56 +00:00
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.Task
2016-06-06 19:56:04 +00:00
import org.gradle.api.file.CopySpec
2016-03-12 00:30:56 +00:00
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.Copy
2017-01-31 22:48:48 +00:00
import org.gradle.api.tasks.InputFiles
2016-03-12 00:30:56 +00:00
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
2017-01-31 22:48:48 +00:00
import org.gradle.api.tasks.bundling.Jar
2016-03-12 00:30:56 +00:00
2017-02-10 08:37:38 +00:00
buildscript {
2017-07-31 11:57:24 +00:00
repositories {
2017-12-14 14:38:39 +00:00
jcenter ( )
2017-12-13 11:16:22 +00:00
maven {
2018-02-28 12:34:45 +00:00
url 'https://dl.google.com/dl/android/maven2'
2017-12-13 11:16:22 +00:00
}
2017-07-31 11:57:24 +00:00
}
dependencies {
2018-06-01 07:57:40 +00:00
classpath 'com.android.tools.build:gradle:3.1.2'
2017-07-31 11:57:24 +00:00
}
2017-02-10 08:37:38 +00:00
}
apply plugin: FlutterPlugin
2016-03-12 00:30:56 +00:00
class FlutterPlugin implements Plugin < Project > {
2016-06-06 19:56:04 +00:00
private File flutterRoot
2017-02-16 22:17:09 +00:00
private File flutterExecutable
2016-05-28 00:12:52 +00:00
private String localEngine
2017-02-23 13:56:19 +00:00
private String localEngineSrcPath
2017-01-31 22:48:48 +00:00
private Properties localProperties
2017-03-23 13:59:12 +00:00
private File flutterJar
2017-07-31 11:57:24 +00:00
private File flutterX86Jar
2017-03-23 13:59:12 +00:00
private File debugFlutterJar
private File profileFlutterJar
private File releaseFlutterJar
2018-08-16 15:43:41 +00:00
private File dynamicProfileFlutterJar
private File dynamicReleaseFlutterJar
2017-03-23 13:59:12 +00:00
private Properties readPropertiesIfExist ( File propertiesFile ) {
Properties result = new Properties ( )
if ( propertiesFile . exists ( ) ) {
2018-01-08 21:48:08 +00:00
propertiesFile . withReader ( 'UTF-8' ) { reader - > result . load ( reader ) }
2017-03-23 13:59:12 +00:00
}
return result
}
2017-01-31 22:48:48 +00:00
private String resolveProperty ( Project project , String name , String defaultValue ) {
if ( localProperties = = null ) {
2018-06-20 14:07:05 +00:00
localProperties = readPropertiesIfExist ( new File ( project . projectDir . parentFile , "local.properties" ) )
2017-01-31 22:48:48 +00:00
}
2017-02-23 13:56:19 +00:00
String result
2017-01-31 22:48:48 +00:00
if ( project . hasProperty ( name ) ) {
result = project . property ( name )
}
if ( result = = null ) {
result = localProperties . getProperty ( name )
}
if ( result = = null ) {
result = defaultValue
}
return result
}
2016-03-12 00:30:56 +00:00
@Override
void apply ( Project project ) {
2018-08-16 15:43:41 +00:00
// Add custom build types
2017-01-31 22:48:48 +00:00
project . android . buildTypes {
profile {
initWith debug
2017-12-14 10:38:20 +00:00
if ( it . hasProperty ( 'matchingFallbacks' ) ) {
matchingFallbacks = [ 'debug' , 'release' ]
}
2017-01-31 22:48:48 +00:00
}
2018-08-16 15:43:41 +00:00
dynamicProfile {
initWith debug
if ( it . hasProperty ( 'matchingFallbacks' ) ) {
matchingFallbacks = [ 'debug' , 'release' ]
}
}
dynamicRelease {
initWith debug
if ( it . hasProperty ( 'matchingFallbacks' ) ) {
matchingFallbacks = [ 'debug' , 'release' ]
}
}
2017-01-31 22:48:48 +00:00
}
2016-03-12 00:30:56 +00:00
2017-05-22 14:50:31 +00:00
String flutterRootPath = resolveProperty ( project , "flutter.sdk" , System . env . FLUTTER_ROOT )
2016-06-06 19:56:04 +00:00
if ( flutterRootPath = = null ) {
2017-05-22 14:50:31 +00:00
throw new GradleException ( "Flutter SDK not found. Define location with flutter.sdk in the local.properties file or with a FLUTTER_ROOT environment variable." )
2016-03-12 00:30:56 +00:00
}
2016-06-06 19:56:04 +00:00
flutterRoot = project . file ( flutterRootPath )
if ( ! flutterRoot . isDirectory ( ) ) {
2016-03-12 00:30:56 +00:00
throw new GradleException ( "flutter.sdk must point to the Flutter SDK directory" )
}
2017-02-16 22:17:09 +00:00
String flutterExecutableName = Os . isFamily ( Os . FAMILY_WINDOWS ) ? "flutter.bat" : "flutter"
flutterExecutable = Paths . get ( flutterRoot . absolutePath , "bin" , flutterExecutableName ) . toFile ( ) ;
2017-02-23 13:56:19 +00:00
if ( project . hasProperty ( 'localEngineOut' ) ) {
String engineOutPath = project . property ( 'localEngineOut' )
File engineOut = project . file ( engineOutPath )
if ( ! engineOut . isDirectory ( ) ) {
throw new GradleException ( 'localEngineOut must point to a local engine build' )
}
2017-03-23 13:59:12 +00:00
flutterJar = Paths . get ( engineOut . absolutePath , "flutter.jar" ) . toFile ( )
2016-04-12 21:26:22 +00:00
if ( ! flutterJar . isFile ( ) ) {
2017-02-23 13:56:19 +00:00
throw new GradleException ( 'Local engine build does not contain flutter.jar' )
2016-04-12 21:26:22 +00:00
}
2017-02-23 13:56:19 +00:00
localEngine = engineOut . name
localEngineSrcPath = engineOut . parentFile . parent
2017-01-31 22:48:48 +00:00
project . dependencies {
2018-06-20 14:07:05 +00:00
if ( project . getConfigurations ( ) . findByName ( "api" ) ) {
api project . files ( flutterJar )
2017-12-13 11:16:22 +00:00
} else {
compile project . files ( flutterJar )
}
2016-06-06 19:56:04 +00:00
}
2017-01-31 22:48:48 +00:00
} else {
Path baseEnginePath = Paths . get ( flutterRoot . absolutePath , "bin" , "cache" , "artifacts" , "engine" )
2018-02-01 23:14:48 +00:00
String targetArch = 'arm'
if ( project . hasProperty ( 'target-platform' ) & &
project . property ( 'target-platform' ) = = 'android-arm64' ) {
targetArch = 'arm64'
}
debugFlutterJar = baseEnginePath . resolve ( "android-${targetArch}" ) . resolve ( "flutter.jar" ) . toFile ( )
profileFlutterJar = baseEnginePath . resolve ( "android-${targetArch}-profile" ) . resolve ( "flutter.jar" ) . toFile ( )
releaseFlutterJar = baseEnginePath . resolve ( "android-${targetArch}-release" ) . resolve ( "flutter.jar" ) . toFile ( )
2018-08-16 15:43:41 +00:00
dynamicProfileFlutterJar = baseEnginePath . resolve ( "android-${targetArch}-dynamic-profile" ) . resolve ( "flutter.jar" ) . toFile ( )
dynamicReleaseFlutterJar = baseEnginePath . resolve ( "android-${targetArch}-dynamic-release" ) . resolve ( "flutter.jar" ) . toFile ( )
2017-01-31 22:48:48 +00:00
if ( ! debugFlutterJar . isFile ( ) ) {
2016-04-12 21:26:22 +00:00
project . exec {
2017-02-16 22:17:09 +00:00
executable flutterExecutable . absolutePath
2017-06-16 21:33:59 +00:00
args "--suppress-analytics"
2016-04-12 21:26:22 +00:00
args "precache"
}
2017-01-31 22:48:48 +00:00
if ( ! debugFlutterJar . isFile ( ) ) {
throw new GradleException ( "Unable to find flutter.jar in SDK: ${debugFlutterJar}" )
2016-04-12 21:26:22 +00:00
}
}
2017-01-31 22:48:48 +00:00
// Add x86/x86_64 native library. Debug mode only, for now.
2017-07-31 11:57:24 +00:00
flutterX86Jar = project . file ( "${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/flutter-x86.jar" )
2017-01-31 22:48:48 +00:00
project . tasks . create ( "flutterBuildX86Jar" , Jar ) {
destinationDir flutterX86Jar . parentFile
archiveName flutterX86Jar . name
2017-03-29 22:31:36 +00:00
from ( "${flutterRoot}/bin/cache/artifacts/engine/android-x86/libflutter.so" ) {
2017-01-31 22:48:48 +00:00
into "lib/x86"
}
2017-03-29 22:31:36 +00:00
from ( "${flutterRoot}/bin/cache/artifacts/engine/android-x64/libflutter.so" ) {
2017-01-31 22:48:48 +00:00
into "lib/x86_64"
}
}
2018-06-20 14:07:05 +00:00
// Add flutter.jar dependencies to all <buildType>Api configurations, including custom ones
2017-07-31 11:57:24 +00:00
// added after applying the Flutter plugin.
2018-06-20 14:07:05 +00:00
project . android . buildTypes . each { addFlutterJarApiDependency ( project , it ) }
project . android . buildTypes . whenObjectAdded { addFlutterJarApiDependency ( project , it ) }
2016-04-12 21:26:22 +00:00
}
2016-03-12 00:30:56 +00:00
project . extensions . create ( "flutter" , FlutterExtension )
project . afterEvaluate this . & addFlutterTask
2017-03-23 13:59:12 +00:00
2018-06-20 14:07:05 +00:00
File pluginsFile = new File ( project . projectDir . parentFile . parentFile , '.flutter-plugins' )
2017-03-23 13:59:12 +00:00
Properties plugins = readPropertiesIfExist ( pluginsFile )
plugins . each { name , _ - >
def pluginProject = project . rootProject . findProject ( ":$name" )
if ( pluginProject ! = null ) {
project . dependencies {
2017-12-13 11:16:22 +00:00
if ( project . getConfigurations ( ) . findByName ( "implementation" ) ) {
implementation pluginProject
} else {
compile pluginProject
}
2017-03-23 13:59:12 +00:00
}
2017-12-13 11:16:22 +00:00
pluginProject . afterEvaluate this . & addFlutterJarCompileOnlyDependency
2017-03-23 13:59:12 +00:00
} else {
project . logger . error ( "Plugin project :$name not found. Please update settings.gradle." )
}
}
}
2017-12-13 11:16:22 +00:00
private void addFlutterJarCompileOnlyDependency ( Project project ) {
2017-08-24 08:44:32 +00:00
if ( project . state . failure ) {
return
}
2017-03-23 13:59:12 +00:00
project . dependencies {
if ( flutterJar ! = null ) {
2017-12-13 11:16:22 +00:00
if ( project . getConfigurations ( ) . findByName ( "compileOnly" ) ) {
compileOnly project . files ( flutterJar )
} else {
provided project . files ( flutterJar )
}
2017-03-23 13:59:12 +00:00
} else {
2017-12-13 11:16:22 +00:00
if ( project . getConfigurations ( ) . findByName ( "debugCompileOnly" ) ) {
debugCompileOnly project . files ( debugFlutterJar )
releaseCompileOnly project . files ( releaseFlutterJar )
} else {
debugProvided project . files ( debugFlutterJar )
releaseProvided project . files ( releaseFlutterJar )
}
2017-03-23 13:59:12 +00:00
}
}
2016-03-12 00:30:56 +00:00
}
2017-07-31 11:57:24 +00:00
/ * *
2018-06-20 14:07:05 +00:00
* Adds suitable flutter . jar api dependencies to the specified buildType .
2017-07-31 11:57:24 +00:00
*
* Note: The BuildType DSL type is not public , and is therefore omitted from the signature .
* /
2018-06-20 14:07:05 +00:00
private void addFlutterJarApiDependency ( Project project , buildType ) {
2017-07-31 11:57:24 +00:00
project . dependencies {
2017-12-13 11:16:22 +00:00
String configuration ;
2018-06-20 14:07:05 +00:00
if ( project . getConfigurations ( ) . findByName ( "api" ) ) {
configuration = buildType . name + "Api" ;
2017-12-13 11:16:22 +00:00
} else {
configuration = buildType . name + "Compile" ;
}
add ( configuration , project . files {
2017-07-31 11:57:24 +00:00
String buildMode = buildModeFor ( buildType )
if ( buildMode = = "debug" ) {
[ flutterX86Jar , debugFlutterJar ]
} else if ( buildMode = = "profile" ) {
profileFlutterJar
2018-08-16 15:43:41 +00:00
} else if ( buildMode = = "dynamicProfile" ) {
dynamicProfileFlutterJar
} else if ( buildMode = = "dynamicRelease" ) {
dynamicReleaseFlutterJar
2017-07-31 11:57:24 +00:00
} else {
releaseFlutterJar
}
} )
}
}
/ * *
* Returns a Flutter build mode suitable for the specified Android buildType .
*
* Note: The BuildType DSL type is not public , and is therefore omitted from the signature .
*
2018-08-16 15:43:41 +00:00
* @return "debug" , "profile" , "dynamicProfile" , "dynamicRelease" , or "release" ( fall - back ) .
2017-07-31 11:57:24 +00:00
* /
private static String buildModeFor ( buildType ) {
if ( buildType . name = = "profile" ) {
return "profile"
2018-08-16 15:43:41 +00:00
} else if ( buildType . name = = "dynamicProfile" ) {
return "dynamicProfile"
} else if ( buildType . name = = "dynamicRelease" ) {
return "dynamicRelease"
2017-07-31 11:57:24 +00:00
} else if ( buildType . debuggable ) {
return "debug"
}
return "release"
}
2016-03-12 00:30:56 +00:00
private void addFlutterTask ( Project project ) {
2017-08-24 08:44:32 +00:00
if ( project . state . failure ) {
return
}
2016-03-12 00:30:56 +00:00
if ( project . flutter . source = = null ) {
throw new GradleException ( "Must provide Flutter source directory" )
}
2017-01-31 22:48:48 +00:00
String target = project . flutter . target
2016-06-07 20:05:42 +00:00
if ( target = = null ) {
target = 'lib/main.dart'
}
2017-02-23 14:09:05 +00:00
if ( project . hasProperty ( 'target' ) ) {
target = project . property ( 'target' )
}
2016-06-07 20:05:42 +00:00
2018-06-06 20:43:32 +00:00
Boolean verboseValue = null
if ( project . hasProperty ( 'verbose' ) ) {
verboseValue = project . property ( 'verbose' ) . toBoolean ( )
}
2018-06-13 19:46:39 +00:00
Boolean previewDart2Value = true
if ( project . hasProperty ( 'preview-dart-2' ) ) {
previewDart2Value = project . property ( 'preview-dart-2' ) . toBoolean ( )
}
2018-03-17 19:47:40 +00:00
String [ ] fileSystemRootsValue = null
if ( project . hasProperty ( 'filesystem-roots' ) ) {
fileSystemRootsValue = project . property ( 'filesystem-roots' ) . split ( '\\|' )
}
String fileSystemSchemeValue = null
if ( project . hasProperty ( 'filesystem-scheme' ) ) {
fileSystemSchemeValue = project . property ( 'filesystem-scheme' )
}
2018-02-12 18:44:31 +00:00
Boolean trackWidgetCreationValue = false
if ( project . hasProperty ( 'track-widget-creation' ) ) {
2018-04-24 16:51:25 +00:00
trackWidgetCreationValue = project . property ( 'track-widget-creation' ) . toBoolean ( )
2018-02-12 18:44:31 +00:00
}
2018-06-15 18:53:30 +00:00
Boolean buildSnapshotValue = false
if ( project . hasProperty ( 'build-snapshot' ) ) {
buildSnapshotValue = project . property ( 'build-snapshot' ) . toBoolean ( )
}
2017-10-03 19:55:53 +00:00
String extraFrontEndOptionsValue = null
if ( project . hasProperty ( 'extra-front-end-options' ) ) {
extraFrontEndOptionsValue = project . property ( 'extra-front-end-options' )
}
String extraGenSnapshotOptionsValue = null
if ( project . hasProperty ( 'extra-gen-snapshot-options' ) ) {
extraGenSnapshotOptionsValue = project . property ( 'extra-gen-snapshot-options' )
}
2018-06-04 15:44:37 +00:00
Boolean buildSharedLibraryValue = false
if ( project . hasProperty ( 'build-shared-library' ) ) {
buildSharedLibraryValue = project . property ( 'build-shared-library' ) . toBoolean ( )
2017-11-21 14:44:03 +00:00
}
2018-02-01 23:14:48 +00:00
String targetPlatformValue = null
if ( project . hasProperty ( 'target-platform' ) ) {
targetPlatformValue = project . property ( 'target-platform' )
}
2017-10-03 19:55:53 +00:00
2018-04-06 11:11:47 +00:00
def addFlutterDeps = { variant - >
2017-08-04 12:57:54 +00:00
String flutterBuildMode = buildModeFor ( variant . buildType )
if ( flutterBuildMode = = 'debug' & & project . tasks . findByName ( 'flutterBuildX86Jar' ) ) {
2017-07-31 11:57:24 +00:00
Task task = project . tasks . findByName ( "compile${variant.name.capitalize()}JavaWithJavac" )
if ( task ) {
task . dependsOn project . flutterBuildX86Jar
}
2017-12-13 11:16:22 +00:00
task = project . tasks . findByName ( "compile${variant.name.capitalize()}Kotlin" )
if ( task ) {
task . dependsOn project . flutterBuildX86Jar
}
2017-01-31 22:48:48 +00:00
}
2017-07-31 11:57:24 +00:00
FlutterTask flutterTask = project . tasks . create ( name: "flutterBuild${variant.name.capitalize()}" , type: FlutterTask ) {
2017-01-31 22:48:48 +00:00
flutterRoot this . flutterRoot
2017-02-16 22:17:09 +00:00
flutterExecutable this . flutterExecutable
2017-07-31 11:57:24 +00:00
buildMode flutterBuildMode
2017-01-31 22:48:48 +00:00
localEngine this . localEngine
2017-02-23 13:56:19 +00:00
localEngineSrcPath this . localEngineSrcPath
2017-01-31 22:48:48 +00:00
targetPath target
2018-06-06 20:43:32 +00:00
verbose verboseValue
2018-06-13 19:46:39 +00:00
previewDart2 previewDart2Value
2018-03-17 19:47:40 +00:00
fileSystemRoots fileSystemRootsValue
fileSystemScheme fileSystemSchemeValue
2018-02-12 18:44:31 +00:00
trackWidgetCreation trackWidgetCreationValue
2018-06-15 18:53:30 +00:00
buildSnapshot buildSnapshotValue
2018-06-04 15:44:37 +00:00
buildSharedLibrary buildSharedLibraryValue
2018-02-01 23:14:48 +00:00
targetPlatform targetPlatformValue
2017-01-31 22:48:48 +00:00
sourceDir project . file ( project . flutter . source )
intermediateDir project . file ( "${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/${variant.name}" )
2017-10-03 19:55:53 +00:00
extraFrontEndOptions extraFrontEndOptionsValue
extraGenSnapshotOptions extraGenSnapshotOptionsValue
2017-01-31 22:48:48 +00:00
}
2018-04-12 08:12:26 +00:00
Task copyFlutterAssetsTask = project . tasks . create ( name: "copyFlutterAssets${variant.name.capitalize()}" , type: Copy ) {
2016-03-12 00:30:56 +00:00
dependsOn flutterTask
dependsOn variant . mergeAssets
2018-01-15 02:24:07 +00:00
dependsOn "clean${variant.mergeAssets.name.capitalize()}"
2016-03-12 00:30:56 +00:00
into variant . mergeAssets . outputDir
2016-06-06 19:56:04 +00:00
with flutterTask . assets
2016-03-12 00:30:56 +00:00
}
2018-04-12 08:12:26 +00:00
variant . outputs [ 0 ] . processResources . dependsOn ( copyFlutterAssetsTask )
2016-03-12 00:30:56 +00:00
}
2018-04-06 11:11:47 +00:00
if ( project . android . hasProperty ( "applicationVariants" ) ) {
project . android . applicationVariants . all addFlutterDeps
} else {
project . android . libraryVariants . all addFlutterDeps
}
2016-03-12 00:30:56 +00:00
}
}
class FlutterExtension {
String source
2016-06-07 20:05:42 +00:00
String target
2016-03-12 00:30:56 +00:00
}
2017-05-05 12:53:51 +00:00
abstract class BaseFlutterTask extends DefaultTask {
2016-06-06 19:56:04 +00:00
File flutterRoot
2017-02-16 22:17:09 +00:00
File flutterExecutable
2016-06-06 19:56:04 +00:00
String buildMode
String localEngine
2017-02-23 13:56:19 +00:00
String localEngineSrcPath
2017-02-23 14:09:05 +00:00
@Input
2016-06-07 20:05:42 +00:00
String targetPath
2017-10-03 19:55:53 +00:00
@Optional @Input
2018-06-06 20:43:32 +00:00
Boolean verbose
@Optional @Input
2018-06-13 19:46:39 +00:00
Boolean previewDart2
@Optional @Input
2018-03-17 19:47:40 +00:00
String [ ] fileSystemRoots
@Optional @Input
String fileSystemScheme
@Optional @Input
2018-02-12 18:44:31 +00:00
Boolean trackWidgetCreation
@Optional @Input
2018-06-15 18:53:30 +00:00
Boolean buildSnapshot
@Optional @Input
2018-06-04 15:44:37 +00:00
Boolean buildSharedLibrary
2018-02-01 23:14:48 +00:00
@Optional @Input
String targetPlatform
2016-03-12 00:30:56 +00:00
File sourceDir
2017-05-05 12:53:51 +00:00
File intermediateDir
2017-10-03 19:55:53 +00:00
@Optional @Input
String extraFrontEndOptions
@Optional @Input
String extraGenSnapshotOptions
2017-05-05 12:53:51 +00:00
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
@OutputFiles
FileCollection getDependenciesFiles ( ) {
2018-06-15 18:53:30 +00:00
// For AOT and Core JIT builds, include the gen_snapshot depfile.
FileCollection depfiles = project . files ( "${intermediateDir}/snapshot.d" )
if ( previewDart2 ) {
// For Dart 2, also include the kernel compiler depfile, since
// kernel compile is the first stage of AOT build in this mode,
// and it includes all the Dart sources.
depfiles + = project . files ( "${intermediateDir}/kernel_compile.d" )
}
// Include Core JIT kernel compiler depfile, since kernel compile is
// the first stage of JIT builds in this mode, and it includes all the
// Dart sources.
depfiles + = project . files ( "${intermediateDir}/snapshot_blob.bin.d" )
return depfiles
2017-05-05 12:53:51 +00:00
}
2018-04-12 08:12:26 +00:00
void buildBundle ( ) {
2017-05-05 12:53:51 +00:00
if ( ! sourceDir . isDirectory ( ) ) {
throw new GradleException ( "Invalid Flutter source directory: ${sourceDir}" )
}
intermediateDir . mkdirs ( )
2018-08-16 15:43:41 +00:00
if ( buildMode = = "profile" | | buildMode = = "release" ) {
2017-05-05 12:53:51 +00:00
project . exec {
executable flutterExecutable . absolutePath
workingDir sourceDir
if ( localEngine ! = null ) {
args "--local-engine" , localEngine
args "--local-engine-src-path" , localEngineSrcPath
}
args "build" , "aot"
2017-06-16 21:33:59 +00:00
args "--suppress-analytics"
2017-05-05 12:53:51 +00:00
args "--quiet"
args "--target" , targetPath
args "--target-platform" , "android-arm"
args "--output-dir" , "${intermediateDir}"
2018-06-13 19:46:39 +00:00
if ( previewDart2 ) {
args "--preview-dart-2"
} else {
args "--no-preview-dart-2"
}
2018-02-12 18:44:31 +00:00
if ( trackWidgetCreation ) {
args "--track-widget-creation"
}
2017-10-03 19:55:53 +00:00
if ( extraFrontEndOptions ! = null ) {
2017-11-21 14:44:03 +00:00
args "--extra-front-end-options" , "${extraFrontEndOptions}"
2017-10-03 19:55:53 +00:00
}
if ( extraGenSnapshotOptions ! = null ) {
2017-11-21 14:44:03 +00:00
args "--extra-gen-snapshot-options" , "${extraGenSnapshotOptions}"
}
2018-06-04 15:44:37 +00:00
if ( buildSharedLibrary ) {
args "--build-shared-library"
2017-10-03 19:55:53 +00:00
}
2018-02-01 23:14:48 +00:00
if ( targetPlatform ! = null ) {
args "--target-platform" , "${targetPlatform}"
}
2017-05-05 12:53:51 +00:00
args "--${buildMode}"
}
}
2016-03-12 00:30:56 +00:00
2017-05-05 12:53:51 +00:00
project . exec {
executable flutterExecutable . absolutePath
workingDir sourceDir
if ( localEngine ! = null ) {
args "--local-engine" , localEngine
args "--local-engine-src-path" , localEngineSrcPath
}
2018-04-12 08:12:26 +00:00
args "build" , "bundle"
2017-06-16 21:33:59 +00:00
args "--suppress-analytics"
2017-05-05 12:53:51 +00:00
args "--target" , targetPath
2018-06-06 20:43:32 +00:00
if ( verbose ) {
args "--verbose"
}
2018-06-13 19:46:39 +00:00
if ( previewDart2 ) {
args "--preview-dart-2"
} else {
args "--no-preview-dart-2"
}
2018-03-17 19:47:40 +00:00
if ( fileSystemRoots ! = null ) {
for ( root in fileSystemRoots ) {
args "--filesystem-root" , root
}
}
if ( fileSystemScheme ! = null ) {
args "--filesystem-scheme" , fileSystemScheme
}
2018-02-12 18:44:31 +00:00
if ( trackWidgetCreation ) {
args "--track-widget-creation"
}
2018-06-15 18:53:30 +00:00
if ( buildSnapshot ) {
args "--build-snapshot"
}
2018-05-14 16:41:58 +00:00
if ( extraFrontEndOptions ! = null ) {
args "--extra-front-end-options" , "${extraFrontEndOptions}"
}
2018-06-15 18:53:30 +00:00
if ( extraGenSnapshotOptions ! = null ) {
args "--extra-gen-snapshot-options" , "${extraGenSnapshotOptions}"
}
if ( targetPlatform ! = null ) {
args "--target-platform" , "${targetPlatform}"
}
2018-08-16 15:43:41 +00:00
if ( buildMode = = "release" | | buildMode = = "profile" ) {
2017-05-05 12:53:51 +00:00
args "--precompiled"
} else {
2018-03-02 19:33:13 +00:00
args "--depfile" , "${intermediateDir}/snapshot_blob.bin.d"
2018-06-13 19:46:39 +00:00
if ( ! previewDart2 ) {
args "--snapshot" , "${intermediateDir}/snapshot_blob.bin"
}
2017-05-05 12:53:51 +00:00
}
2018-04-12 08:12:26 +00:00
args "--asset-dir" , "${intermediateDir}/flutter_assets"
2018-08-16 15:43:41 +00:00
if ( buildMode = = "debug" ) {
args "--debug"
}
if ( buildMode = = "profile" | | buildMode = = "dynamicProfile" ) {
args "--profile"
}
if ( buildMode = = "release" | | buildMode = = "dynamicRelease" ) {
args "--release"
}
if ( buildMode = = "dynamicProfile" | | buildMode = = "dynamicRelease" ) {
args "--dynamic"
}
2017-05-05 12:53:51 +00:00
}
}
}
class FlutterTask extends BaseFlutterTask {
2016-03-12 00:30:56 +00:00
@OutputDirectory
2017-05-05 12:53:51 +00:00
File getOutputDirectory ( ) {
return intermediateDir
}
2016-03-12 00:30:56 +00:00
2016-06-06 19:56:04 +00:00
CopySpec getAssets ( ) {
return project . copySpec {
2017-12-14 16:27:25 +00:00
from "${intermediateDir}"
2018-01-15 02:24:07 +00:00
2017-12-14 16:27:25 +00:00
include "flutter_assets/**" // the working dir and its files
2018-01-15 02:24:07 +00:00
2018-06-15 18:53:30 +00:00
if ( buildMode ! = 'debug' | | buildSnapshot ) {
2018-06-04 15:44:37 +00:00
if ( buildSharedLibrary ) {
2018-01-27 13:21:58 +00:00
include "app.so"
2018-01-15 02:24:07 +00:00
} else {
include "vm_snapshot_data"
include "vm_snapshot_instr"
include "isolate_snapshot_data"
include "isolate_snapshot_instr"
}
2016-06-06 19:56:04 +00:00
}
2018-01-15 02:24:07 +00:00
}
2016-03-12 00:30:56 +00:00
}
2017-03-14 11:49:30 +00:00
FileCollection readDependencies ( File dependenciesFile ) {
if ( dependenciesFile . exists ( ) ) {
try {
// Dependencies file has Makefile syntax:
// <target> <files>: <source> <files> <separated> <by> <space>
String depText = dependenciesFile . text
return project . files ( depText . split ( ': ' ) [ 1 ] . split ( ) )
} catch ( Exception e ) {
logger . error ( "Error reading dependency file ${dependenciesFile}: ${e}" )
}
}
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
return project . files ( )
2017-03-14 11:49:30 +00:00
}
2017-01-31 22:48:48 +00:00
@InputFiles
FileCollection getSourceFiles ( ) {
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
FileCollection sources = project . files ( )
for ( File depfile in getDependenciesFiles ( ) ) {
sources + = readDependencies ( depfile )
}
if ( ! sources . isEmpty ( ) ) {
2017-03-14 11:49:30 +00:00
// We have a dependencies file. Add a dependency on gen_snapshot as well, since the
// snapshots have to be rebuilt if it changes.
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
sources + = readDependencies ( project . file ( "${intermediateDir}/gen_snapshot.d" ) )
2018-06-13 19:46:39 +00:00
if ( previewDart2 ) {
sources + = readDependencies ( project . file ( "${intermediateDir}/frontend_server.d" ) )
}
2017-09-12 06:35:02 +00:00
if ( localEngineSrcPath ! = null ) {
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
sources + = project . files ( "$localEngineSrcPath/$localEngine" )
2017-09-12 06:35:02 +00:00
}
2017-03-14 11:49:30 +00:00
// Finally, add a dependency on pubspec.yaml as well.
Include kernel_compile.d in Gradle depfiles (#17175)
This updates the Android build to declare the kernel compile depfile as
an output and its contents as inputs when running with --preview-dart-2
(the default mode).
The 'flutter build aot' command behaves differently depending on whether
it's running in Dart 1 or Dart 2 mode:
* Dart 1: the entrypoint Dart file (typically main.dart) is passed
directly to gen_snapshot, which then emits snapshot.d, whose contents
list the transitive closure of Dart dependencies (input files) for the
snapshot. snapshot.d is a declared output, its contents (plus
gen_snapshot itself) constitute the set of input files to the Gradle
build action.
* Dart 2: then entrypoint Dart file (typically main.dart) is first
compiled with the Dart kernel frontend. This emits kernel_compile.d,
whose contents list the transitive closure of Dart dependencies (input
files) for the kernel 'dill' output file. This 'dill' file is then
passed to gen_snapshot, which emits snapshot.d, whose contents are
empty. As of this change, both snapshot.d and kernel_compile.d are
declared outputs, and their contents (plus gen_snapshot and the
frontend compiler themselves) constitute the set of input files to the
Gradle build action.
This fixes a bug wherein profile/release AOT outputs were not
invalidated due to snapshot.d being empty, and kernel_compile.d being
ignored. This was introduced during recent refactoring of the AOT build
code, wherein the kernel compile and gen_snapshot actions were changed
to emit independent depfiles (previously one stomped -- or failed to --
on the other's output).
2018-05-02 01:11:57 +00:00
return sources + project . files ( 'pubspec.yaml' )
2017-03-14 11:49:30 +00:00
}
// No dependencies file (or problems parsing it). Fall back to source files.
return project . fileTree (
dir: sourceDir ,
exclude: [ 'android' , 'ios' ] ,
include: [ '**/*.dart' , 'pubspec.yaml' ]
)
2017-01-31 22:48:48 +00:00
}
2016-03-12 00:30:56 +00:00
@TaskAction
void build ( ) {
2018-04-12 08:12:26 +00:00
buildBundle ( )
2016-03-12 00:30:56 +00:00
}
}