美文网首页Android开发拾穗程序员安卓资源收集
使用Gradle发布Android arr项目到jCenter

使用Gradle发布Android arr项目到jCenter

作者: cristianohao | 来源:发表于2016-05-11 16:35 被阅读138次

    目的

    发布arr(Android library 项目)到jCenter 远程仓库,这样所有人都可以通过Gradle方式引用。选择jcenter是因为它兼容maven,而且支持更多形式仓库,android studio最新版本已经默认jcenter了。

    平台准备

    https://bintray.com/ 上注册一个账号,获取API key。

    生成 javaDoc 和 source JARS

    Maven Central 和 jCenter都需要上传项目的javaDoc 和 source JARS。添加插件 android-maven-plugin 用来生成 javaDoc 和 source JARS。通过bintray 上传需要添加官方插件 gradle-bintray-plugin。在主项目最外层build.gradle 文件 buildscript/dependencies中添加插件。

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'
            classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
            classpath 'com.github.dcendents:android-maven-plugin:1.2'
            classpath 'net.sf.proguard:proguard-gradle:4.10'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    

    在需要发布的app Module的build.gradle 文件中添加 生成javadoc和source jar的tasks等。下面的以游戏分析1.0 为例子。

    apply plugin: 'com.android.library'
    //需要的插件
    apply plugin: 'com.github.dcendents.android-maven'
    apply plugin: 'com.jfrog.bintray'
    
    // This is the library version used when deploying the artifact
    version = "2.7.5"
    
    android {
        compileSdkVersion 22
        buildToolsVersion '22.0.1'
    
        defaultConfig {
            minSdkVersion 11
            targetSdkVersion 22
            versionCode 275
            versionName "2.7.5"
        }
    
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'pro_DataEye_Android_2.7.1_100271000.pro'
        }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
    }
    
    //定义 group
    group = "com.dataeye.sdk.ga_v1"
    
    def siteUrl = 'https://github.com/DataEye'    // project homepage
    def gitUrl = 'https://github.com/DataEye' // project git
    
    install {
        repositories.mavenInstaller {
            // This generates POM.xml with proper parameters
            pom {
                project {
                    packaging 'aar'
                    name 'GASdk_V1'
                    url siteUrl
                    licenses {
                        license {
                            name 'The Apache Software License, Version 2.0'
                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                developers {
                    developer {
                        id 'cristianohao'
                        name 'cristianohao'
                        email 'gonghao@dataeye.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
    }
    
    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))
    }
    
    javadoc {
        options{
            encoding "UTF-8"
            charSet 'UTF-8'
            links "http://docs.oracle.com/javase/7/docs/api"
        }
    }
    
    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }
    
    artifacts {
        archives javadocJar
        archives sourcesJar
    }
    
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    bintray {
        user = properties.getProperty("bintray.user")
        key = properties.getProperty("bintray.apikey")
        configurations = ['archives']
        pkg {
            repo = "maven"
            name = "GASdk_V1"                // project name in jcenter
            websiteUrl = siteUrl
            vcsUrl = gitUrl
            licenses = ["Apache-2.0"]
            publish = true
        }
    }
    

    本地 local.properties 添加API key

    bintray.user=deandroid  // your bintray user name 
    bintray.apikey=358f11719ba4289156e6195a7e6753584258f6f3 // your bintray api key
    

    通过gradew执行这些任务完成发布。

    gradlew javadocJar
    gradlew sourcesJar
    gradlew install
    gradlew bintrayUpload
    

    关于审核

    当bintrayUpload成功之后,在自己的主页https://bintray.com/openproject/右下部分Latest Activity,会看到提交了一个项目。
    进入详情页,提交审核就可以了。审核时间大概6小时左右。审核通过后,下次更新版本号发布就无需审核了。

    引用

    发布成功后就可以直接引用

    dependencies {
            ...
            compile 'com.dataeye.sdk.ga_v1:ga:2.7.5'
    }

    相关文章

      网友评论

        本文标题:使用Gradle发布Android arr项目到jCenter

        本文链接:https://www.haomeiwen.com/subject/yrjurttx.html