apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'checkstyle'

android {

    defaultConfig {
        compileSdk = 34
        buildToolsVersion = '30.0.3'

        minSdkVersion 21
        targetSdkVersion 34
        consumerProguardFiles 'progress-proguard.txt'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

        // Always show the result of every unit test, even if it passes.
        all {
            testLogging {
                events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
            }
        }
    }
}

dependencies {
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    implementation 'androidx.annotation:annotation:1.3.0'
    implementation 'com.android.billingclient:billing:7.0.0'
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompileProvider.get().classpath
        }
    }
    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

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

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                from components.release

                artifact androidJavadocsJar
                artifact androidSourcesJar

                groupId = 'com.anjlab.android.iab.v3'
                artifactId = 'library'
                version = '2.2.0'

                pom {
                    name = 'Android In-App Billing v3 Library'
                    description = 'A lightweight implementation of Android In-app Billing Version 3'
                    url = 'https://github.com/anjlab/android-inapp-billing-v3'
                    packaging = 'aar'
                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution = 'repo'
                        }
                    }
                    developers {
                        developer {
                            id = 'serggl'
                            name = 'Sergey Glukhov'
                            email = 'sergey.glukhov@gmail.com'
                        }
                    }
                    scm {
                        url = 'scm:git@github.com:anjlab/android-inapp-billing-v3.git'
                        connection = 'scm:git@github.com:anjlab/android-inapp-billing-v3.git'
                        developerConnection = 'scm:git@github.com:anjlab/android-inapp-billing-v3.git'
                    }
                }
            }
        }
        if (project.hasProperty('sonatypeRepo')) {
            repositories {
                maven {
                    url sonatypeRepo
                    credentials {
                        username sonatypeUsername
                        password sonatypePassword
                    }
                }
            }
        }
    }

    signing {
        required { gradle.taskGraph.hasTask("publish") }
        sign publishing.publications.maven
    }
}

task checkstyle(type: Checkstyle) {
    configFile file("${project.rootDir}/checkstyle.xml")

    source 'src/main/java'
    include '**/*.java'
    exclude '**/gen/**'

    classpath = project.files(android.getBootClasspath())
}

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);
}
