Android studio 制作aar 使用Gradle发布项目到JCenter仓库

    为什么发布自己项目(aar)到JCenter呢,这个答案显而易见,把自己开发功能库或者插件库制作成aar并且发布上去,这样开发者想使用项目的功能或者插件,就可以通过Android Studio自带的gradle方式来添加aar下载到开发者自己的项目里面,简单地说,简洁,方便调用。还有一方面JCenter兼容maven,Android Studio可以默认使用JCenter了。下面我通过自己发布项目Android 自定义通讯录(仿Ios反弹效果+模糊搜索+查看手机通讯录+拉伸导航条)为例子来讲解下发布项目到JCenter库所需要的步骤。

  1. Bintray账号
    需要一个bintray网站的账号,需要有github帐号可以直接登,如果没有就只能注册,bintray传送门

  2. 新建项目,在项目中添加所需插件
    这边就是我自己项目为例子,在根项目的build.gradle的添加上传到公共的JCenter仓库所需俩个插件
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    ,如图:这里写图片描述

  3. 构建module模块build.gradle信息

apply plugin: 'com.android.library'
//添加申请生成插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'


def siteUrl = 'https://github.com/WX-JIN/JContact' //项目在github主页地址
def gitUrl = 'https://github.com/WX-JIN/JContact.git'   //Git仓库的地址

group = "com.soubw"//发布aar前缀根节点

version = "0.0.1"//发布aar的库版本

//最后生成是compile 'com.soubw:jcontactlib:0.0.1' 就是group + :+module名字 + :+version

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

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


install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'JContact'//添加项目描述
                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 'wx_jin'//设置自己ID
                        name 'WangXiaojin'//设置自己名字
                        email '85268837@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")
    key = properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "jcontact" //项目在JCenter的名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
}

按配置,修改有中文字既可以了!

  1. 配置自己网站的信息
    我们上传到jcenter的网站Bintray,需要用户验证,一个网站用户名一个是配置清单的API KEY 这个API KEY是从网站清单获取的,在https://bintray.com/profile/edit 可以看到API KEY一栏,这边需要你再次网站密码,才能看到你API KEY 这里写图片描述
    这俩个配置信息即我们上面配置bintray.user和bintray.apikey,在步骤3的时候,我们获取这个信息是从local.properties这个文件读取的,我们需要在自己项目的local.properties添加
    bintray.user=你bintray的用户名
    bintray.apikey=你的网站的API KEY

    这里写图片描述
    这里为什么要写在local.properties呢,因为一般我们都是使用github上传项目到github,这时github会自动忽略local.properties这个文件上传。或者使用git(可以利用gitignore忽略这个文件到git)

    1. 执行
      在stuido的terminal执行
      gradlew install 这里写图片描述
      这里写图片描述
      如果提示成功接着进行上传代码
      gradlew bintrayUpload
      这里写图片描述
      这边如果出错,一般编码问题,自己修改下就可以了!

    2. 在网站上提交申请到Jcenter
      当bintrayUpload成功之后,到自己网站项目提交到申请这里写图片描述

最后差不多几小时,一般都通过申请!


参考文章:
https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值