apply plugin: 'com.android.application'
apply plugin: 'checkstyle'

/* gets the version name from the latest Git tag, stripping the leading v off */
def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags', '--always'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

gradle.allprojects {
    ext.getVersionCode = { ->
        def manifestFile = file("app/src/main/AndroidManifest.xml")
        def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
        def manifestText = manifestFile.getText()
        def matcher = pattern.matcher(manifestText)
        matcher.find()
        return Integer.parseInt(matcher.group(1))
    }
}

repositories {
    jcenter()
}

dependencies {
    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.android.support:support-annotations:25.3.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
}

android {
    compileSdkVersion 25
    buildToolsVersion '24.0.0'

    defaultConfig {
        archivesBaseName = "F-DroidPrivilegedExtension"
        versionName getVersionName()

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        compileOptions.encoding = "UTF-8"

        // Use Java 1.7, requires minSdk 8
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    aaptOptions {
        cruncherEnabled = false
    }

    buildTypes {
        all {
            // Keep IPackageInstallObserver and IPackageDeleteObserver
            minifyEnabled false
            useProguard false
            shrinkResources false
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError true

        htmlReport true
        xmlReport false
        textReport false

        lintConfig file("lint.xml")
    }

    File signFile = rootProject.file('signing.properties')
    if (signFile.exists()) {
        Properties properties = new Properties()
        properties.load(new FileInputStream(signFile))
        signingConfigs {
            release {
                storeFile rootProject.file(properties['key.store'])
                storePassword properties['key.store.password']
                keyAlias properties['key.alias']
                keyPassword properties['key.alias.password']
            }
        }
        buildTypes.release.signingConfig signingConfigs.release
    }
}

checkstyle {
    toolVersion = '7.2'
}

task checkstyle(type: Checkstyle) {
    configFile file("${project.rootDir}/config/checkstyle/checkstyle.xml")
    source 'src/main/java'
    include '**/*.java'

    classpath = files()
}
