apply plugin: 'com.android.application'

sourceCompatibility = "7"
targetCompatibility = "7"

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.elderdrivers.riru.edxp.sandhook"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compileOnly files("${hiddenApiStubJarFilePath}")
    implementation project(':edxp-common')
    implementation project(':xposed-bridge')
    implementation 'com.swift.sandhook:hooklib:4.1.6'
    compileOnly project(':dexmaker')
}


preBuild.doLast {
    def imlFile = file(project.name + ".iml")
    try {
        def parsedXml = (new groovy.util.XmlParser()).parse(imlFile)
        def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
        parsedXml.component[1].remove(jdkNode)
        def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
        new groovy.util.Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
        groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
    } catch (FileNotFoundException e) {
        // nop, iml not found
    }
}

afterEvaluate {

    tasks.withType(JavaCompile) {
        options.compilerArgs.add("-Xbootclasspath/p:${hiddenApiStubJarFilePath}")
    }

    android.applicationVariants.all { variant ->

        def variantNameCapped = variant.name.capitalize()
        def variantNameLowered = variant.name.toLowerCase()

        def myTemplatePath = "${projectDir}/template_override/"

        task("makeAndCopy${variantNameCapped}", type: Jar, dependsOn: "assemble${variantNameCapped}") {
            dependsOn tasks.getByPath(":edxp-common:copyCommonProperties")
            from "${buildDir}/intermediates/transforms/dexMerger/${variantNameLowered}/0/",
                    "${projectDir}/src/main/resources/"
            destinationDir file(myTemplatePath + "system/framework/")
            baseName "edxp"
            doLast {
                copy {
                    from file(myTemplatePath)
                    into file(templateRootPath)
                }
            }
            outputs.upToDateWhen { false }
        }
    }
}