发布Library到JCenter,步步为营

前言

将自己写的库上传到Jcenter或者Maven提供给自己或者别人使用,在构建项目的时候只要写上一行如下类似的引用代码即可引用自己的库

compile 'com.sus.library:imagelib:1.0.0'

看到这篇文章的同学可能之前已经踩了不少坑,希望下面的介绍可以帮你解惑
如果有什么问题欢迎提出!趁热乎哈!

下面将逐步介绍如何将Library发布到JCenter

具体案例为:ImageLoaderUtil
所有下文涉及到的文件和配置都包含在其中,如果你觉得对你有用,麻烦STAR一下

1、进入Bintray官网

Bintray官网首页

  • 这里选择“For an Open Source Account Sign Up Here”,而非“START YOUR FREE TRIAL”,如果选择 “START YOUR FREE TRIAL”,可能会碰到下面的问题 Bintray link to jcenter missing
  • 问题结论就是:”Add To JCenter” is not enabled for Enterprise Trial users. You need to be OSS or Premium organization/user in order to link your packages to JCenter.

2、注册账号

账号注册页面
填写相关信息,邮箱尽量使用Gmail邮箱地址(国内邮箱有可能无法注册或者注册成功无法收到激活邮件),注册完成之后到你填写的邮箱里面去激活Bintray账号即可
举例:

  • First Name : Shuai
  • Last Name : Su
  • Username : su2008shuai
  • Password : xxxxxxxxxxxxxx
  • Emai Address : su2008shuai@gmail.com
  • Select Country : China

3、创建代码仓库

View Profile

点击上图所示的“Add NewRepository”按钮,添加代码仓库,点击后就会跳转到下图的界面

创建仓库

举例:

  • Name : me
  • Type : Maven

4、获取头像对应的API KEY

点击右上角个人头像进入到个人信息主界面,点击Edit按钮即可进入到下图所示的界面。点击“API KEY”,输入Bintray本账号的登陆密码,即可查看到本账号的API KEY
API KEY

5、Add New Package

创建包
举例:

6、Project的build.gradle添加如下信息

在Project的build.gradle添加如下信息:

//用于打包Maven所需文件
 classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 
//用于上传Maven生成的文件到Bintray
 classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 

我的Project的build.gradle的完整信息:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'

        //用于打包Maven所需文件
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 
        //用于上传Maven生成的文件到Bintray
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            name 'glide-snapshot'
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }

    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

7、配置并应用bintrayUpload.gradle,配置bintray.properties和project.properties

project目录结构

7.1 在imagelib Module的根目录下创建bintrayUpload.gradle文件

apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

//加载属性文件
Properties properties = new Properties()
File localPropertiesFile = project.file("bintray.properties");
if (localPropertiesFile.exists()) {
    properties.load(localPropertiesFile.newDataInputStream())
}
File projectPropertiesFile = project.file("project.properties");
if (projectPropertiesFile.exists()) {
    properties.load(projectPropertiesFile.newDataInputStream())
}

//读取属性
def projectRepositoryName = properties.getProperty("project.repositoryName")
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectArtifactId = properties.getProperty("project.artifactId")
def projectVersionName = android.defaultConfig.versionName
def projectPackaging = properties.getProperty("project.packaging")
def projectSiteUrl = properties.getProperty("project.siteUrl")
def projectGitUrl = properties.getProperty("project.gitUrl")
def projectVersionDesc = properties.getProperty("project.versiondesc")
def projectVersionVcsTag = properties.getProperty("project.versionvcstag")

def developerId = properties.getProperty("developer.id")
def developerName = properties.getProperty("developer.name")
def developerEmail = properties.getProperty("developer.email")

def bintrayUser = properties.getProperty("bintray.user")
def bintrayApikey = properties.getProperty("bintray.apiKey")
def bintrayOrganizationId = properties.getProperty("bintray.organizationId");

def javadocName = properties.getProperty("javadoc.name")

group = projectGroupId

// 配置生成POM.xml文件的参数
install {
    repositories.mavenInstaller {
        pom {
            project {
                name projectName
                groupId projectGroupId
                artifactId projectArtifactId
                version projectVersionName
                packaging projectPackaging
                url projectSiteUrl
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id developerId
                        name developerName
                        email developerEmail
                    }
                }
                scm {
                    connection projectGitUrl
                    developerConnection projectGitUrl
                    url projectSiteUrl
                }
            }
        }
    }
}

//生成sources.jar
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.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

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

bintray {
    user = bintrayUser
    key = bintrayApikey
    configurations = ['archives']
    pkg {
        //userOrg = bintrayOrganizationId
        repo = projectRepositoryName
        name = projectName
        websiteUrl = projectSiteUrl
        vcsUrl = projectGitUrl
        licenses = ["Apache-2.0"]
        publish = true
        version {
            name = projectVersionName
            desc = projectVersionDesc
            vcsTag = projectVersionVcsTag
        }
    }
}

7.2 在imagelib Module的build.gradle中应用上面创建的bintrayUpload.gradle文件,添加如下代码

apply from: "bintrayUpload.gradle"

这里注意会遇到一个奇葩问题:

  • Where:
    Script ‘/Users/sus/share/ImageLoaderUtil/imagelib/bintrayUpload.gradle’ line: 85
    *What went wrong:
    A problem occurred evaluating script.
    android.compileSdkVersion is missing!

其实就是「 android.compileSdkVersion is missing!」 这个问题很奇葩,需要把
apply from: “bintrayUpload.gradle”这句话放在最下面,如下完整文件信息所示,我碰到这个问题的时候是直接把这句话放在apply plugin: ‘com.android.library’的后面了

完整文件信息:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.0'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.squareup.okhttp3:okhttp:3.4.2'
    compile 'com.github.bumptech.glide:glide:3.8.0-SNAPSHOT'
    compile 'com.github.bumptech.glide:okhttp-integration:1.5.0-SNAPSHOT'
}

apply from: "bintrayUpload.gradle"

7.3 在imagelib Module的根目录下创建7.1要读取的配置文件

创建bintray.properties用于配置bintray和开发者信息

#配置bintray账号相关信息
#bintray用户名,不是登陆邮箱,是个人中心右上角显示的名字
bintray.user=su2008shuai
#bintray的ApiKey
bintray.apiKey=xxxxxxxxxxxxx

#bintray的Organization Id
#bintray.organizationId=soulrelay


#配置开发者信息
#昵称
developer.id=sushuai
#姓名
developer.name=sushuai
#邮箱
developer.email=su2008shuai@gmail.com

创建project.properties用于配置项目信息

#project
#仓库名称,就是在bintray官网建立的仓库的名称
project.repositoryName=me
#项目名称
project.name=imagelib
#项目组id
project.groupId=com.sus.library
#项目id,一般同project.name
project.artifactId=imagelib
#打包类型
project.packaging=aar
#项目官方网站地址
project.siteUrl=https://github.com/soulrelay/ImageLoaderUtil
#项目git地址
project.gitUrl=https://github.com/soulrelay/ImageLoaderUtil
#生成的javadoc名称
javadoc.name=imagelib

project.versiondesc = 1.0.0 normal
project.versionvcstag = 1.0.0 tag

7.4 在Terminal窗口下输入如下指令上传到Bintray

gradlew install
gradlew bintrayUpload

期间可能会碰到如下问题

  • -bash: gradlew: command not found

解决方案:
gradlew is not in your global path. To execute the ‘clean’ task (or any task for that matter) using the gradle wrapper (gradlew) in your project directory in your terminal, specify the current directory with the ‘./’:

  • ./gradlew clean

Running mac, you also have to do “chmod 755 gradlew” on the file before to make it executable.

归结起来的话:

  • chmod 755 gradlew
  • ./gradlew install
  • ./gradlew bintrayUpload

若出现BUILD SUCCESSFUL则说明成功上传到了Bintray(有时候在执行./gradlew bintrayUpload的时候报错,但这时去bintray官网查看它也上传成功了,可能是缓存的问题,可以Invalidate Caches or clean restart一下试试)

8、添加imagelib Package到JCenter

进入到Bintray网站,找到刚才上传的项目,点击右下角的“Add To JCenter”按钮
然后填写项目描述点击“Send”提交审核即可(这里可以什么都不干,直接点击Send按钮),如果审核成功,它会给你发送一封站内信(同时你注册的邮箱优也会收到通知,1天之内肯定可以收到通知)通知你。

审核成功通知

访问这个链接:https://jcenter.bintray.com/com/sus/library/imagelib/1.0.0/

jcenter库链接
审核成功后的包结构

9、其它问题和说明

类似上面的问题的原因都是配置不对,bintray.properties和project.properties上的配置一定要和bintray线上的配置一致,否则会报各种找不到xx的问题

发布了97 篇原创文章 · 获赞 87 · 访问量 32万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览