Android studio 使用Gradle发布Android开源项目到JCenter 总结

1、注册账号

    先到https://bintray.com注册一个账号。  这个网站支持 github 账户直接登录的

 

2、获取  bintray.user  和 bintray.apikey 

       2.1:bintray.user 就是你的用户名,注意是用户名,不是邮箱

           在注册的时候会出现这个界面

              

              或者

 

                                                   

 

       2.2 获取 bintray.apikey 

            

 

3、在Android studio 项目中创建一个 library Module  , 这个 Module 的名字 是 ZYJ ,如下

  

 

4、在项目的根目录的 local.properties ( 这个文件是个人隐私 , 利用gitignore忽略这个文件到git ) 文件填写  bintray.user  和 bintray.apikey 

      

 

5、在项目的根目录的 build:gradle 文件里面添加插件 代码 

 

 classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
 classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

  截图如下 

       

 

6、配置 库文件项目中的 build:gradle 文件,也就是 ZYJ Module  的build:gradle 文件

apply plugin: 'com.android.library'

//配置插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.0"       //这个是版本号,必须填写

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    resourcePrefix "less_"     //这里随便填

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'

}

def siteUrl = 'https://github.com/zyj1609wz'      // 项目的主页   这个是说明,可随便填
def gitUrl = 'https://github.com/zyj1609wz'      // Git仓库的url  这个是说明,可随便填
group = "zyj.com.mylibrary"    // 这里是groupId ,必须填写  一般填你唯一的包名

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'Android BounceProgressBar Widget'     //项目描述
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'zhaoyanjun'        //填写开发者的一些基本信息
                        name 'zhaoyanjun'    //填写开发者的一些基本信息
                        email '1643915516@qq.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))
}
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")    //读取 local.properties 文件里面的 bintray.user
    key = properties.getProperty("bintray.apikey")   //读取 local.properties 文件里面的 bintray.apikey
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "ZYJ"    //发布到JCenter上的项目名字,必须填写
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

 

7、同步代码 

     

 

8、如果第7步没有问题了,就可以把项目上传到Bintray

     8.1  下载一遍Gradle 

            执行命令  gradlew install

            怎么才能执行上面的命令呢??

         8.1.1、打开Android studio 左下角的  Terminal 

              

       

         8.1.2 在输入框中输入  gradlew install ,然后回车执行 

                 

 

         

        8.1.3 在8.1.2 可能出现的错误 

               错误截图  

 

                      

           

             解决方案 : 在ZYJ Module 的 build:gradle  中添加以下代码              

javadoc {
    options{
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "http://docs.oracle.com/javase/7/docs/api"
    }
}

   

         8.2   然后执行如下命令(Windows中)完成上传 

                  执行命令  gradlew bintrayUpload 

 

                 

 

9、进入Bintray 网站,如果看到下面这些就代表你上次成功了

       

                 

                  

10、如果看到如下的信息就代表可以把 项目添加到 JCenter  

     

           

 

 

 

     11 、把项目提交到 Jcenter 中              

             

 

                    

 

      提交成功后,等待几个小时,就可以审核通过了。

 

12、审核通过了,如何使用 ?

        在 app Module   的 build:gradle  里面添加 

         compile 'zyj.com.mylibrary:ZYJ:1.0.0'

        注意: 

                  zyj.com.mylibrary:是groupId 

                   ZYJ: 项目名

                   1.0.0:版本号           

         

       

13、如何 删除  bintray.com 里面已经发布的 项目

        

 

                     

 

14、如何发布更新版本

       只需要在发布的时候把 版本号更新,就可以了,发布完成,不需要 add jcenter 操作。等待几个小时,就可以使用新版本了。

        

     

 

posted @ 2015-11-25 16:07  赵彦军  阅读(4631)  评论(2编辑  收藏  举报