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

android {
    compileSdk rootProject.ext.coreCompileSdkVersion

    publishing {
        multipleVariants {
            allVariants()
            withJavadocJar()
        }
    }

    defaultConfig {
        minSdkVersion rootProject.ext.coreMinSdkVersion
        targetSdkVersion rootProject.ext.coreTargetSdkVersion
        consumerProguardFiles 'adjust-proguard-rules.txt'
    }

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

// read local properties
File localPropsFile = project.rootProject.file('local.properties')
if (localPropsFile.exists()) {
    Properties p = new Properties()
    new FileInputStream(localPropsFile).withCloseable { is ->
        p.load(is)
    }
    p.each { name, value ->
        ext[name] = value
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    /*
     * according to https://developer.android.com/studio/write/annotations
     *  If you use annotations in your own library module,
     *  the annotations are included as part of the Android Archive (AAR) artifact
     *  in XML format in the annotations.zip file.
     *  Adding the androidx.annotation dependency doesn't introduce a dependency
     *  for any downstream users of your library.
     */
    implementation 'androidx.annotation:annotation:1.8.0'
}

task adjustCoreAarRelease(type: Copy) {
    dependsOn 'assembleRelease'

    from('build/outputs/aar/')
    include 'sdk-core-release.aar'
    destinationDir file('build/libs/')
    rename 'sdk-core-release.aar', 'adjust-sdk-release.aar'
}

task adjustCoreAarDebug(type: Copy) {
    dependsOn 'assembleDebug'

    from('build/outputs/aar/')
    include 'sdk-core-debug.aar'
    destinationDir file('build/libs/')
    rename 'sdk-core-debug.aar', 'adjust-sdk-debug.aar'
}

task adjustCoreJarRelease(type: Jar) {
    dependsOn 'compileReleaseJavaWithJavac'
    from('build/intermediates/javac/release/classes/')
    archiveFileName  = project.provider {"adjust-sdk-release.jar"}
}

task adjustCoreJarDebug(type: Jar) {
    dependsOn 'compileDebugJavaWithJavac'
    from('build/intermediates/javac/debug/classes/')
    archiveFileName  = project.provider {"adjust-sdk-debug.jar"}
}

task adjustCoreAndroidSourcesJar(type: Jar) {
    setArchiveClassifier('sources')
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives adjustCoreJarRelease
    archives adjustCoreAndroidSourcesJar
}
afterEvaluate {
    publishing {
        publications {
//        mavenAndroidCoreJar(MavenPublication) {
//            customizePomForJar(pom)
//            groupId rootProject.ext.adjustGroupId
//            artifactId 'adjust-android'
//            version rootProject.ext.coreVersionName
//
//            artifact adjustCoreJarRelease
//            artifact adjustCoreAndroidJavadocsJar
//            artifact adjustCoreAndroidSourcesJar
//        }

            mavenAndroidCoreAar(MavenPublication) {
                customizePomForAar(pom)
                groupId rootProject.ext.adjustGroupId
                artifactId 'adjust-android'
                version rootProject.ext.coreVersionName

                artifact("$buildDir/outputs/aar/sdk-core-release.aar") {
                    builtBy tasks.getByPath("adjustCoreAarRelease")
                }
//                artifact adjustCoreAndroidJavadocsJar
                artifact adjustCoreAndroidSourcesJar
            }

        }

        repositories {
            maven {
                if (rootProject.ext.coreVersionName.toString().contains('SNAPSHOT')) {
                    println "Using Maven SNAPSHOT repository for publishing"
                    url "https://central.sonatype.com/repository/maven-snapshots/"
                } else {
                    url "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2"
                }
                if (project.hasProperty("sonatypeUsername")) {
                    credentials {
                        username sonatypeUsername
                        password sonatypePassword
                    }
                }
            }
        }
    }
    signing {
        sign publishing.publications.mavenAndroidCoreAar
    }
}

def customizePomForJar(pom) {
    pom.withXml {
        // Add all items necessary for maven central publication.
        asNode().children().last() + {
            resolveStrategy = Closure.DELEGATE_FIRST
            description 'The Adjust SDK for Android'
            name 'Adjust Android SDK'
            url 'https://github.com/adjust/android_sdk'
            organization {
                name 'adjust GmbH'
                url 'http://www.adjust.com'
            }
            licenses {
                license {
                    name 'MIT License'
                    url 'http://www.opensource.org/licenses/mit-license.php'
                }
            }
            scm {
                url 'git@github.com:adjust/android_sdk.git'
                connection 'scm:git:git@github.com:adjust/android_sdk.git'
                developerConnection 'scm:git:git@github.com:adjust/android_sdk.git'
            }
            developers {
                developer {
                    name 'Pedro Silva'
                    email 'pedro@adjust.com'
                }
                developer {
                    name 'Ugljesa Erceg'
                    email 'ugljesa@adjust.com'
                }
            }
        }
    }
}

def customizePomForAar(pom) {
    pom.withXml {
        // Remove the existing packaging field, as it has to be overridden with type aar
        asNode().remove(asNode().get("packaging"))

        // Add all items necessary for maven central publication.
        asNode().children().last() + {
            resolveStrategy = Closure.DELEGATE_FIRST
            description 'The Adjust SDK for Android'
            name 'Adjust Android SDK'
            url 'https://github.com/adjust/android_sdk'
            packaging 'aar'
            organization {
                name 'adjust GmbH'
                url 'http://www.adjust.com'
            }
            licenses {
                license {
                    name 'MIT License'
                    url 'http://www.opensource.org/licenses/mit-license.php'
                }
            }
            scm {
                url 'git@github.com:adjust/android_sdk.git'
                connection 'scm:git:git@github.com:adjust/android_sdk.git'
                developerConnection 'scm:git:git@github.com:adjust/android_sdk.git'
            }
            developers {
                developer {
                    name 'Pedro Silva'
                    email 'pedro@adjust.com'
                }
                developer {
                    name 'Ugljesa Erceg'
                    email 'ugljesa@adjust.com'
                }
            }
            dependencies {
                dependency {
                    groupId 'com.adjust.signature'
                    artifactId 'adjust-android-signature'
                    version '3.47.0'
                    scope 'compile'
                }
            }
        }
    }
}

model {
//    tasks.generatePomFileForMavenAndroidCoreJarPublication {
//        destination = file("${project.buildDir}/generated-pom.xml")
//    }
    /* TODO check if/how to replace this tasks
    tasks.publishMavenAndroidCorePublicationToMavenLocal {
        dependsOn project.tasks.signArchives
    }
    tasks.publishMavenAndroidCorePublicationToMavenRepository {
        dependsOn project.tasks.signArchives
    }
    */
    tasks.generatePomFileForMavenAndroidCoreAarPublication {
        destination = file("${project.buildDir}/generated-pom.xml")
    }
    /* TODO check if/how to replace this tasks
    tasks.publishMavenAndroidCoreAARPublicationToMavenLocal {
        dependsOn project.tasks.signArchives
    }
    tasks.publishMavenAndroidCoreAARPublicationToMavenRepository {
        dependsOn project.tasks.signArchives
    }
    */
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}