Home | History | Annotate | Download | only in apps
      1 // Top-level build file where you can add configuration options common to all sub-projects/modules.
      2 
      3 import java.io.File
      4 import java.nio.file.Paths
      5 import org.apache.tools.ant.taskdefs.condition.Os
      6 
      7 buildscript {
      8     repositories {
      9         google()
     10         jcenter()
     11     }
     12     dependencies {
     13         classpath 'com.android.tools.build:gradle:3.2.0'
     14 
     15         // NOTE: Do not place your application dependencies here; they belong
     16         // in the individual module build.gradle files
     17     }
     18 }
     19 
     20 allprojects {
     21     repositories {
     22         google()
     23         jcenter()
     24     }
     25 }
     26 
     27 def setupSkiaLibraryBuild(project, appVariants, appName) {
     28     appVariants.all{ variant ->
     29         def buildNativeLib = project.task("${variant.name}_BuildSkiaLib", type:Exec) {
     30             workingDir '../../../..' // top-level skia directory
     31             final String cmd = constructBuildCommand(project, variant, appName)
     32             if (Os.isFamily(Os.FAMILY_WINDOWS)) {
     33                 commandLine "cmd", "/c", cmd
     34             } else {
     35                 commandLine cmd.split()
     36             }
     37         }
     38         buildNativeLib.onlyIf { !project.hasProperty("suppressNativeBuild") }
     39 
     40         def copyNativeLib = project.task("${variant.name}_CopySkiaLib", type:Copy) {
     41             def fromDir = getVariantOutDir(project, variant).skiaOut
     42             def intoDir = getVariantOutDir(project, variant).androidOut
     43             from fromDir
     44             into intoDir
     45             include "${appName}.so"
     46         }
     47 
     48         TaskCollection<Task> compileTask = project.tasks.matching {
     49             //  println(it.name)
     50             it.name.toLowerCase().contains("compile" + variant.name.toLowerCase()) &&
     51                     it.name.toLowerCase().endsWith("ndk")
     52         }
     53         compileTask.findAll()*.dependsOn copyNativeLib
     54         copyNativeLib.dependsOn buildNativeLib
     55     }
     56 }
     57 
     58 def getLocalProperties() {
     59     Properties properties = new Properties()
     60     File propFile = project.rootProject.file('local.properties')
     61     if (propFile.canRead()) {
     62         properties.load(propFile.newDataInputStream())
     63     }
     64     propFile = project.rootProject.file('gradle.properties')
     65     if (propFile.canRead()) {
     66         properties.load(propFile.newDataInputStream())
     67     }
     68     return properties
     69 }
     70 
     71 def getVariantOutDir(project, variant) {
     72     String variantPrefix = null
     73     String androidLibDir = null
     74     if (variant.name.startsWith("arm64")) {
     75         variantPrefix = "arm64"
     76         androidLibDir = "arm64-v8a"
     77     } else if (variant.name.startsWith("arm")) {
     78         variantPrefix = "arm"
     79         androidLibDir = "armeabi-v7a"
     80     } else if (variant.name.startsWith("x64")) {
     81         variantPrefix = "x64"
     82         androidLibDir = "x86_64"
     83     } else if (variant.name.startsWith("x86")) {
     84         variantPrefix = "x86"
     85         androidLibDir = "x86"
     86     }
     87 
     88     String skiaOutDir = null
     89     String propName = "${variantPrefix}.out.dir"
     90     if (project.hasProperty(propName)) {
     91         skiaOutDir = project.getProperties().getAt(propName)
     92     } else {
     93         skiaOutDir = getLocalProperties().getProperty(propName, "missing_variant_out")
     94     }
     95 
     96     return [skiaOut: skiaOutDir,
     97             androidOut: "src/main/libs/${androidLibDir}"]
     98 }
     99 
    100 def constructBuildCommand(project, variant, appName) {
    101     String depotToolsDir = null
    102     for (String entry : System.getenv("PATH").split(File.pathSeparator)) {
    103         if (Paths.get(entry).endsWith("depot_tools")) {
    104             depotToolsDir = entry;
    105             break;
    106         }
    107     }
    108     if (depotToolsDir == null) {
    109         depotToolsDir = getLocalProperties().getProperty('depot_tools.dir', null)
    110     }
    111 
    112     if (depotToolsDir == null) {
    113         throw GradleScriptException("Depot Tools not found! Please update your path to include" +
    114                 " depot_tools or define depot_tools.dir in local.properties")
    115     }
    116 
    117     String ninja = Paths.get(depotToolsDir, "ninja")
    118     String out_dir = getVariantOutDir(project, variant).skiaOut
    119     return "$ninja -C $out_dir $appName"
    120 }
    121