将自己的android 项目发布到jCenter上面去提供给别人(待整理)

参考:https://blog.csdn.net/xuchao_blog/article/details/62893851

 

注册Bintray

说到注册,大家第一时间肯定是想到去官网注册。但是,现在官网首页的注册入口(上面第一张图)已经变成组织的注册了,并不是个人注册。所以我们要去个人注册入口:

传送门:https://bintray.com/signup/oss


register2
在上面这个页面注册就对了。也可以用第三方登录,我是直接用Github登录(好像其它两个在我国也登录不了),省事。

创建一个Maven仓库

注册完登录进去后,点击“Add New Repository”,新建仓库:


create
接下来,我们会看到这么一个界面:


create1
Name填maven,Type选Maven,Default Licenses选Apache-2.0,Description是描述,随便填,点击Create。然后回到主页就会在刚刚“Add New Repository”下面多了一个叫maven的仓库。

配置build.gradle

在你Project的build.gradle文件中加入Maven和Jfrog Bintray的依赖插件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        //下面这两句
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        jcenter()
    }
}
 
task clean(type: Delete) {
    delete rootProject.buildDir
}
你不一定要和我用同样版本的插件,说不定你用的时候就不是这个版本了。建议去Github上看看最新的版本:

Maven:https://github.com/dcendents/android-maven-gradle-plugin
Jfrog Bintray:https://github.com/bintray/gradle-bintray-plugin

然后在library的build.gradle文件中进行配置,这里以我的开源控件CircleView为例:


gradle
整个build.gradle文件如下:

apply plugin: 'com.android.library'
//添加这两行
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
 
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
 
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
 
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    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:25.1.0'
    testCompile 'junit:junit:4.12'
}
//项目主页
def siteUrl = 'https://github.com/backkomyoung/CircleView'
//项目的git地址
def gitUrl = 'https://github.com/backkomyoung/CircleView.git'
//发布到JCenter上的项目名字
def libName = "CircleView"
 
//发布到组织名称名字,必须填写
group = "me.songning.CircleView"
// 版本号,下次更新是只需要更改版本号即可
version = "1.0.0"
//上面配置后上传至JCenter后的编译路径是这样的: compile 'me.songning.CircleView:library:1.0.0'
 
//生成源文件
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}
 
//生成Javadoc文档
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
 
//文档打包成jar
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
 
//拷贝javadoc文件
task copyDoc(type: Copy) {
    from "${buildDir}/docs/"
    into "docs"
}
 
//上传到JCenter所需要的源码文件
artifacts {
    archives javadocJar
    archives sourcesJar
}
 
// 配置maven库,生成POM.xml文件
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                //项目描述,随意填
                name 'A colorful circle view with text.'
                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 'backkomyoung'
                        name 'SongNing'
                        email 'backkomyoung@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
 
//上传到JCenter
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仓库的时候的名字一样
        repo = "maven"
        //发布到JCenter上的项目名字
        name = libName
        //项目描述
        desc = 'A colorful circle view with text.'
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}
 
javadoc {
    options{
        //如果你的项目里面有中文注释的话,必须将格式设置为UTF-8,不然会出现乱码
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "http://docs.oracle.com/javase/7/docs/api"
    }
}
除此外,还要将user和key写到local.properties文件中:

sdk.dir=E\:\\Android\\Sdk
//uer和key
bintray.user=username
bintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
user即为你的账号:


user
key的话点击修改信息:


edit
选择左边的“API key”,然后复制key:


key
把user和key填到上面的local.properties文件中就ok啦。准备工作到这里算是完成了。

执行命令

打开Android Studio底部工具栏的Terminal窗口,输入命令:

gradlew install(mac电脑是./gradlew install 


build
漫长的等待...可能是我的网络问题,吃了个饭回来才弄好。


succeed
显示“BUILD SUCCESSFUL”即表示成功。

到这里已经成功一半了,只差上传到Bintray了。然后我们接着在Terminal窗口输入命令:

gradlew bintrayUpload (mac电脑是./gradlew bintrayUpload)


bintrayUpload
这个很快就好~


upload
同样,显示“BUILD SUCCESSFUL”即表示成功。

发布到JCenter

这时候打开我们创建的maven仓库,就会在里面发现已经成功将项目上传到仓库里。


maven
点击我们的项目名,进入详情:


jcenter
点击“Add to JCenter”后:


send
直接点Send就行了。因为时差的关系,接下来就是漫长的等待审核了。


approved
经过几个小时,在Bintray的网站上会有提示审核通过。这时候,就可以在Android Studio上通过compile的方式使用了。

遇到的问题

说说我遇到的问题,就是执行上传命令的时候,显示这个错误:


fail
提示找不到“maven”,原因是我当时还没创建Maven仓库就执行上传命令,创建后再执行一遍命令就成功了。还有就是创建的Maven仓库的名字一定要和build.gradle文件里面的repo值一致。我就遇到这一个问题。

如果你们遇到其他的问题,建议看看这篇:

Android 发布项目到 JCenter 遇到的各种坑(http://www.jianshu.com/p/c518a10fdaed)
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值