buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

apply plugin: 'com.android.library'

dependencies {
}

android {
    // mandatory fields
    compileSdkVersion FindProperty("compileSdkVersion")

    defaultConfig {
        // Required to avoid WRITE_EXTERNAL_STORAGE and READ_PHONE_STATE permissions from being added
        targetSdkVersion FindProperty("targetSdkVersion", ParentKeys.defaultConfig)
    }
    
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            res.srcDirs = ['res']
        }
    }
    
    // setup more details here    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    lintOptions {
        abortOnError false
    }
}

enum ParentKeys {
    android, defaultConfig
}

//
// Searches for an ANDROID property defined in Unity
// - name       : name of a property to search
// - parentName : name of the parent property to search in
//
def FindProperty(String name, ParentKeys parentName = ParentKeys.android) {
    println "Searching for property: " + name + " in parent " + parentName

    // search the root project first
    def value = FindProperty(project.rootProject, name, parentName)
    if (value) {
        return value
    }

    // search the launcher project second
    value = FindProperty(project.rootProject.subprojects.find { p -> p.name == "launcher" }, name, parentName)
    if (value) {
        return value
    }

    // search any other project last
    for (subProject in project.rootProject.subprojects) {
        value = FindProperty(subProject, name, parentName)
        if (value) {
            return value
        }
    }

    println "ERROR: Cannot find '$parentName.$name' property in any gradle file"
}

//
// Searches for an ANDROID property defined in a project
// - project    : gradle project to search at
// - name       : name of a property to search
// - parentName : name of the parent property to search in
//
def FindProperty(project, name, parentName) {
    println "Searching in the project: ${project.name}"
    def returnValue = null;
    def pAndroid = project.findProperty("android")
    if (pAndroid) {
        switch (parentName)
        {
            case ParentKeys.android:
                returnValue = pAndroid.properties[name]
                break;

            case ParentKeys.defaultConfig:
                def defaultConfig = pAndroid.getProperty("defaultConfig")
                if (defaultConfig) {
                    def property = defaultConfig.getProperty(name)
                    if (property) {
                        returnValue = (name == "minSdkVersion" || name == "targetSdkVersion") ? property.getApiString() : property
                    }
                }
                break;
        }
    }

    if (returnValue) {
        println "OK: Found in the project '${project.name}': $returnValue"
    }
    return returnValue;
}