apply plugin: 'com.android.library'
apply plugin: 'signing'
apply plugin: 'checkstyle'

group 'com.anjlab.android.iab.v3'
version '1.1.0'

android {
    compileSdkVersion 31

    defaultConfig {
        minSdkVersion 22
        //noinspection ExpiredTargetSdkVersion
        targetSdkVersion 31
        consumerProguardFiles 'progress-proguard.txt'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    testOptions.unitTests {
        // Don't throw runtime exceptions for android calls that are not mocked
        returnDefaultValues = true

        all {
            testLogging {
                events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
            }
        }
    }
}

dependencies {
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    implementation 'com.android.support:support-annotations:28.0.0'
    implementation 'com.android.billingclient:billing:5.0.0'
}

configurations.archives.extendsFrom configurations.default

signing {
    required { gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

task androidJavadocs(type: Javadoc) {
    source = 'src/main/java,src/main/aidl'
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from 'src/main/java'
}

artifacts {
    archives androidSourcesJar
    archives androidJavadocsJar
}

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

    classpath = files()
}

check.dependsOn('checkstyle')
connectedCheck.dependsOn('checkstyle')

android.libraryVariants.all { variant ->
    def name = variant.buildType.name
    if (name.equals('com.android.builder.BuilderConstants.DEBUG')) {
        return; // Skip debug builds.
    }
    def task = project.tasks.create "jar${name.capitalize()}", Jar
    task.dependsOn variant.javaCompileProvider
    task.from variant.javaCompileProvider.get().destinationDir
    task.baseName 'anjlab-iabv3'
    task.doLast {
        println "Copying jar to sample project..."
        copy {
            from task.archivePath
            into '../sample/libs'
            rename { String fileName -> 'anjlab-iabv3-current.jar' }
        }
    }
    artifacts.add('archives', task);
}
