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 '[email protected]'//设置自己邮箱
                    }
                }
                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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

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

  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/

猜你喜欢

转载自blog.csdn.net/duoluo9/article/details/80237189