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 {
repositories {
jcenter ( )
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
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
private String resolveProperty ( Project project , String name , String defaultValue ) {
if ( localProperties = = null ) {
localProperties = new Properties ( )
def localPropertiesFile = project . rootProject . file ( "local.properties" )
if ( localPropertiesFile . exists ( ) ) {
localProperties . load ( localPropertiesFile . newDataInputStream ( ) )
}
}
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 ) {
2017-01-31 22:48:48 +00:00
// Add a 'profile' build type
project . android . buildTypes {
profile {
initWith debug
}
}
2016-03-12 00:30:56 +00:00
2017-01-31 22:48:48 +00:00
String flutterRootPath = resolveProperty ( project , "flutter.sdk" , System . env . FLUTTER_HOME )
2016-06-06 19:56:04 +00:00
if ( flutterRootPath = = null ) {
2017-01-31 22:48:48 +00:00
throw new GradleException ( "Flutter SDK not found. Define location with flutter.sdk in the local.properties file or with a FLUTTER_HOME 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' )
}
File 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 {
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" )
File debugFlutterJar = baseEnginePath . resolve ( "android-arm" ) . resolve ( "flutter.jar" ) . toFile ( )
File profileFlutterJar = baseEnginePath . resolve ( "android-arm-profile" ) . resolve ( "flutter.jar" ) . toFile ( )
File releaseFlutterJar = baseEnginePath . resolve ( "android-arm-release" ) . resolve ( "flutter.jar" ) . toFile ( )
if ( ! debugFlutterJar . isFile ( ) ) {
2016-04-12 21:26:22 +00:00
project . exec {
2017-02-16 22:17:09 +00:00
executable flutterExecutable . absolutePath
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.
File flutterX86Jar = project . file ( "${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/flutter-x86.jar" )
project . tasks . create ( "flutterBuildX86Jar" , Jar ) {
destinationDir flutterX86Jar . parentFile
archiveName flutterX86Jar . name
from ( "${flutterRoot}/bin/cache/artifacts/engine/android-x86/libsky_shell.so" ) {
into "lib/x86"
}
from ( "${flutterRoot}/bin/cache/artifacts/engine/android-x64/libsky_shell.so" ) {
into "lib/x86_64"
}
}
project . dependencies {
debugCompile project . files ( flutterX86Jar , debugFlutterJar )
profileCompile project . files ( profileFlutterJar )
releaseCompile project . files ( releaseFlutterJar )
}
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
}
private void addFlutterTask ( Project project ) {
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 13:56:19 +00:00
if ( project . tasks . findByName ( 'flutterBuildX86Jar' ) ) {
project . compileDebugJavaWithJavac . dependsOn project . flutterBuildX86Jar
}
2016-03-12 00:30:56 +00:00
project . android . applicationVariants . all { variant - >
2017-01-31 22:48:48 +00:00
if ( ! [ "debug" , "profile" , "release" ] . contains ( variant . name ) ) {
throw new GradleException ( "Build variant must be one of \"debug\", \"profile\", or \"release\" but was \"${variant.name}\"" )
}
FlutterTask flutterTask = project . tasks . create ( "flutterBuild${variant.name.capitalize()}" , FlutterTask ) {
flutterRoot this . flutterRoot
2017-02-16 22:17:09 +00:00
flutterExecutable this . flutterExecutable
2017-01-31 22:48:48 +00:00
buildMode variant . name
localEngine this . localEngine
2017-02-23 13:56:19 +00:00
localEngineSrcPath this . localEngineSrcPath
2017-01-31 22:48:48 +00:00
targetPath target
sourceDir project . file ( project . flutter . source )
intermediateDir project . file ( "${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/${variant.name}" )
}
2016-06-06 19:56:04 +00:00
Task copyFlxTask = project . tasks . create ( name: "copyFlutterAssets${variant.name.capitalize()}" , type: Copy ) {
2016-03-12 00:30:56 +00:00
dependsOn flutterTask
dependsOn variant . mergeAssets
into variant . mergeAssets . outputDir
2016-06-06 19:56:04 +00:00
with flutterTask . assets
2016-03-12 00:30:56 +00:00
}
variant . outputs [ 0 ] . processResources . dependsOn ( copyFlxTask )
}
}
}
class FlutterExtension {
String source
2016-06-07 20:05:42 +00:00
String target
2016-03-12 00:30:56 +00:00
}
class FlutterTask 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
2016-06-07 20:05:42 +00:00
String targetPath
2016-03-12 00:30:56 +00:00
File sourceDir
@OutputDirectory
File intermediateDir
2016-06-06 19:56:04 +00:00
CopySpec getAssets ( ) {
return project . copySpec {
from "${intermediateDir}/app.flx"
if ( buildMode ! = 'debug' ) {
2017-01-23 21:28:35 +00:00
from "${intermediateDir}/vm_snapshot_data"
from "${intermediateDir}/vm_snapshot_instr"
from "${intermediateDir}/isolate_snapshot_data"
from "${intermediateDir}/isolate_snapshot_instr"
2016-06-06 19:56:04 +00:00
}
}
2016-03-12 00:30:56 +00:00
}
2017-01-31 22:48:48 +00:00
@InputFiles
FileCollection getSourceFiles ( ) {
return project . fileTree ( dir: sourceDir , exclude: [ 'android' , 'ios' ] , include: [ '**/*.dart' , 'pubspec.yaml' , 'flutter.yaml' ] )
}
2016-03-12 00:30:56 +00:00
@TaskAction
void build ( ) {
if ( ! sourceDir . isDirectory ( ) ) {
throw new GradleException ( "Invalid Flutter source directory: ${sourceDir}" )
}
intermediateDir . mkdirs ( )
2016-06-06 19:56:04 +00:00
if ( buildMode ! = "debug" ) {
2017-01-31 22:48:48 +00:00
project . exec {
2017-02-16 22:17:09 +00:00
executable flutterExecutable . absolutePath
2017-01-31 22:48:48 +00:00
workingDir sourceDir
if ( localEngine ! = null ) {
args "--local-engine" , localEngine
2017-02-23 13:56:19 +00:00
args "--local-engine-src-path" , localEngineSrcPath
2017-01-31 22:48:48 +00:00
}
args "build" , "aot"
args "--target" , targetPath
args "--target-platform" , "android-arm"
args "--output-dir" , "${intermediateDir}"
args "--${buildMode}"
2016-06-06 19:56:04 +00:00
}
}
2016-03-12 00:30:56 +00:00
project . exec {
2017-02-16 22:17:09 +00:00
executable flutterExecutable . absolutePath
2016-03-12 00:30:56 +00:00
workingDir sourceDir
2016-05-28 00:12:52 +00:00
if ( localEngine ! = null ) {
2017-01-31 22:48:48 +00:00
args "--local-engine" , localEngine
2017-02-23 13:56:19 +00:00
args "--local-engine-src-path" , localEngineSrcPath
2016-03-23 20:33:11 +00:00
}
2016-03-19 04:44:48 +00:00
args "build" , "flx"
2016-06-07 20:05:42 +00:00
args "--target" , targetPath
2016-06-06 19:56:04 +00:00
args "--output-file" , "${intermediateDir}/app.flx"
2017-02-15 09:27:05 +00:00
args "--no-include-roboto-fonts"
2016-06-06 19:56:04 +00:00
if ( buildMode ! = "debug" ) {
2017-01-31 22:48:48 +00:00
args "--precompiled"
2016-06-06 19:56:04 +00:00
} else {
2017-01-31 22:48:48 +00:00
args "--snapshot" , "${intermediateDir}/snapshot_blob.bin"
args "--depfile" , "${intermediateDir}/snapshot_blob.bin.d"
2016-06-06 19:56:04 +00:00
}
2016-03-12 00:30:56 +00:00
args "--working-dir" , "${intermediateDir}/flx"
}
}
}