buildscript {
    repositories {
        mavenCentral()
        google()
    }
}

plugins {
    id 'com.github.sherter.google-java-format' version '0.9'
    id 'maven-publish'
    id 'signing'
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 31

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 31
        versionCode VERSION_CODE.toInteger()
        versionName VERSION_NAME

        // Need to set up some project properties to publish to bintray.
        project.group = GROUP_ID
        project.archivesBaseName = ARTIFACT_ID
        project.version = VERSION_NAME
    }

    splits {
        abi {
          enable true
          reset()
          // Specifies a list of ABIs that Gradle should create APKs for.
          include "arm64-v8a", "armeabi-v7a", "armeabi"
          universalApk true
        }
    }

    lintOptions {
        abortOnError false
        checkAllWarnings true
        warningsAsErrors true
        disable 'HardwareIds','MissingApplicationIcon','GoogleAppIndexingWarning','InvalidPackage','OldTargetApi'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'junit:junit:4.13.2'
    implementation 'androidx.test:runner:1.4.0'
}

googleJavaFormat {
    options style: 'AOSP'
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addStringOption('encoding', 'UTF-8')
    options.addStringOption('charSet', 'UTF-8')
    failOnError false
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourcesJar
}


afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId GROUP_ID
                artifactId ARTIFACT_ID
                version VERSION_NAME
                from components.release

                artifact sourcesJar
                artifact javadocJar

                pom {
                    name = ARTIFACT_ID
                    description = 'Android library for triggering device-side ' +
                                  'code from host-side Mobly tests.'
                    url = 'https://github.com/google/mobly-snippet-lib'
                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution = 'repo'
                        }
                    }
                    developers {
                        developer {
                            name = 'The Mobly Team'
                        }
                    }
                    scm {
                        connection = 'https://github.com/google/mobly-snippet-lib.git'
                        url = 'https://github.com/google/mobly-snippet-lib'
                    }
                }
            }
        }

        repositories {
            maven {
                def releasesRepoUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
                def snapshotsRepoUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                credentials {
                    username ossrhUsername
                    password ossrhPassword
                }
            }
        }
    }
    signing {
        sign publishing.publications.release
    }
}

// Open lint's HTML report in your default browser or viewer.
task openLintReport(type: Exec) {
    def lint_report = "build/reports/lint-results-debug.html"
    def cmd = "cat"
    def platform = System.getProperty('os.name').toLowerCase(Locale.ROOT)
    if (platform.contains("linux")) {
        cmd = "xdg-open"
    } else if (platform.contains("mac os x")) {
        cmd = "open"
    } else if (platform.contains("windows")) {
        cmd = "launch"
    }
    commandLine cmd, lint_report
}

task presubmit {
    dependsOn { ['googleJavaFormat', 'lint', 'openLintReport'] }
    doLast {
        println "Fix any lint issues you see. When it looks good, submit the pull request."
    }
}

