apply plugin: 'com.android.library'
apply plugin: 'maven'

group = 'org.jraf'
ext.groupPath = group.replace('.', '/')
ext.artifactId = 'android-switch-backport'
version = '1.4.0'


android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles 'proguard-rules.txt', getDefaultProguardFile('proguard-android-optimize.txt')
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

// Use "gradle uploadArchives" to deploy the aar to your local maven repository
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: mavenLocal().getUrl())

            pom.artifactId = artifactId
        }
    }
}

android.libraryVariants.all { variant ->
    task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
        description "Generates Javadoc for $variant.name."
        source = variant.javaCompile.source
        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
        options.links("http://docs.oracle.com/javase/7/docs/api/");
        options.links("http://d.android.com/reference/");
        options.tags(["attr", "X"])
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

android.libraryVariants.all { variant ->
    task("jar${variant.name.capitalize()}Javadoc", type: Jar, dependsOn: "generate${variant.name.capitalize()}Javadoc") {
        description "Generate a Javadoc jar for $variant.name."
        classifier = 'javadoc'
        from buildDir.getPath() + '/docs/javadoc'
    }
}

android.libraryVariants.all { variant ->
    task("jar${variant.name.capitalize()}Sources", type: Jar) {
        description "Generate a Sources jar for $variant.name."
        classifier = 'sources'
        from variant.javaCompile.source
    }
}

// Use "gradle install" to deploy the aar to your local maven repository, as well as the javadoc and the source jars
task install(dependsOn: ["assembleRelease", "uploadArchives", "jarReleaseSources", "jarReleaseJavadoc"]) << {
    description "Make the library, generate a source jar, generate a javadoc jar, and copy everything to the local maven repository"

    // Copy the sources jar
    copy {
        from buildDir.getPath() + '/libs/library-sources.jar'
        into "${repositories.mavenLocal().getUrl()}/${groupPath}/${artifactId}/${version}"
        rename 'library-sources.jar', "${artifactId}-${version}-sources.jar"
    }

    // Copy the javadoc jar
    copy {
        from buildDir.getPath() + '/libs/library-javadoc.jar'
        into "${repositories.mavenLocal().getUrl()}/${groupPath}/${artifactId}/${version}"
        rename 'library-javadoc.jar', "${artifactId}-${version}-javadoc.jar"
    }
}
