【Android】 项目(库)上传Jcenter方法参考(内含两种方法)

Android 项目上传Jcenter

本文仅用于个人学习记录使用,不为帮助他人,如果你觉得我写的对你有用,我表示感谢,如果没用,请跳过,拒绝网络愤青,拒绝网络暴力。批评指点可以帮助别人,接受批评指点,不接受喷子。

准备工作

去bintray官网注册账号,账号类型有两种,第一种是公司类型(Sign Up to a Free Trial):https://bintray.com/signup

第二种是个人开源类型( Sign Up to an Open Source account):https://bintray.com/signup/oss

我们要用的是第二个链接!第二个链接!!第二个链接!!!


注册邮箱不要用QQ、163,我就是在QQ邮箱这里郁闷了半晌,主要是这网站它不提醒邮箱有误,只是把邮箱那一栏给用红色边框圈了起来,这谁TM知道啥意思啊,算了不吐槽了,后来用了Gmail(偷偷告诉你,翻墙用的SSR),算注册成了。

不想上图了,直接文字描述吧,真的感觉写这个好费劲啊,真心感觉那些提供文章的朋友们,太辛苦了,感同身受啊。
1、注册登录成功后,点击 Add New Repository 进入添加仓库界面
2、仓库名(Name)称可以随便起,没试过中文,用英文吧,哪怕是拼音
3、类型(Type)选择Maven
4、开源协议(Default Licenses(Optional))可以选择你喜欢的协议,个人喜欢Apache-2.0
5、仓库描述(Description(Potional))这个就随便写了,不过本着认真负责的态度,你应该写个能表示仓库作用的文档

接下来就是获取ApiKey

1、页面右上角有个个人账号的位置,点击之后的下拉框中选择Edit Profile选项
2、新的页面中,点击左边的一列选项的最后一栏,API Key ,然后输入你的登录密码,就可以获取到一串API key了

下面是正文

第一种方式

看法:个人认为这种方式比较全面一些,能配置的东西也比较多,但是比较复杂,坑也不少,不过,不跳几次坑又怎能体会从坑里爬出来的喜悦呢?

  • 1、android studio中新建项目,然后再新建一个library的module,这个过程大家应该都已经轻车熟路了,不再浪费篇幅,记住把library依赖到app的module下就行了

  • **2、项目的build.gradle 的dependencies 项代码结果

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        //以下两项是添加的
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
}
  • *3、library下的build.gradle文件,我尽量为我能想到的每一项加上了注释
apply plugin: 'com.android.library'
//以下两项是添加的插件
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.3"

    defaultConfig {
        minSdkVersion 15
        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'
        }
    }
    //这项可以不让报过多检查的错误,不知道理解对不对,反正加了也不多,那就加吧
    lintOptions {
        abortOnError false
    }
}

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.4.0'
    testCompile 'junit:junit:4.12'
}

//这里读取本地的local.properties文件
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

//这里读取local.properties文件中配置的属性
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectPackaging = properties.getProperty("project.packaging")
def projectSiteUrl = properties.getProperty("project.siteUrl")
def projectGitUrl = properties.getProperty("project.gitUrl")

def versionName = properties.getProperty("version.name")
def versionDesc = properties.getProperty("version.desc")

def pkgName = properties.getProperty("pkg.name")
def pkgRepo = properties.getProperty("pkg.repo")
def pkgUserOrg = properties.getProperty("pkg.userOrg")

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")

//以下两项也加上吧,不知道能不能缺,没时间测试了
group = projectGroupId
version = versionName

bintray {
    //bintray用户名
    user = bintrayUser
    //上面获取的API Key
    key = bintrayApikey

    pkg {
        //创建的仓库名字,就是点击Add New Repository 时新建的仓库的Name,
        //不然系统不知道传到哪个仓库
        repo = pkgRepo
        //自己bintray创建的organization名称,个人的其实就是用户名
        userOrg = pkgUserOrg 
        //项目名称,名字起得有点怪,别介意,其实它不起啥毛用
        name = pkgName
        //以下两项最好都有,不然别人就只能依赖你的项目,当想下载你的源码看看时就找不到地方了
        //项目首页,比如https://www.github/***/***
        websiteUrl = projectSiteUrl
        //项目拉取地址,比如https://www.github/***/***.git
        vcsUrl = projectGitUrl

        licenses = ['Apache-2.0']//创建仓库时选择的license type
        publish = true // 是否是公开项目。

        version {
            //版本号
            name = versionName
            //版本描述
            desc = versionDesc
            released = new Date()
            vcsTag = 'v1.0'
            attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
        }
    }
    configurations = ['archives']
}



// This generates POM.xml with proper parameters
install {
    repositories.mavenInstaller {
        pom {
            project {
                //项目名,感觉没啥重要作用
                name projectName
                //包名,比如( com.ergouzi )
                groupId projectGroupId
                //项目名称,可以自己定义一个,比如( test ),不一定就是android studio项目名
                version versionName
                //打包类型,一般是aar
                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
                        id developerId
                        //开发者姓名
                        name developerName
                        //开发者邮箱
                        email developerEmail
                    }
                }
                scm {
                    connection projectGitUrl
                    developerConnection projectGitUrl
                    url projectSiteUrl
                }
            }
        }
    }
}

//打包源码
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}
//打包源码doc文档
task javadoc(type: Javadoc) {
    //这里设置的两个UTF-8的编码是为了不让报 “编码UTF-8的不可映射字符”的错,现在看到这个错就恶心
    options.encoding = "UTF-8"
    options.charSet = 'UTF-8'
    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
}


//提交命令:首先执行gradlew install,成功后执行gradlew bintrayUpload,再次成功后就已经成功上传到bintray了

  • 4、下面给出我的本地的local.properties配置作为参考
ndk.dir=D\:\\ANDROID\\AndroidNDK\\android-ndk-r14b-windows-x86_64\\android-ndk-r14b
sdk.dir=D\:\\ANDROID\\AndroidSDK

#用户名和API Key
bintray.user=xiao******
bintray.apikey=ab58**************************489e

#project
#项目名
project.name=项目名
project.groupId=com.ergouzi包名
project.packaging=aar
project.siteUrl=https://www.github/***/***
project.gitUrl=https://www.github/***/***.git

#version
version.name=1.0.0最好从1.0.0开始(阿里强条)
version.desc=项目描述.

#pkg
pkg.name=
pkg.userOrg=xiao******
#仓库名
pkg.repo=

#developer
developer.id=xiao******
developer.name=ergouzi
developer.email=liu********@gmail.com
  • 最后就是在android studio 的Terminal中输入gradlew install命令,等成功后,再输入gradlew bintrayUpload进行提交到bintray,再次成功后就可以去bintray网站上去申请上传到Jcenter上了

第二种方式

看法:这种方式实在是简单,但是,容易下载novoda这个插件失败,我试了很久才成功。

  • 1、android studio中新建项目,这个module须是library类型的,就是项目仅仅有一个库,没有application,不然怎么提交都提交不上,官方文档好像也是这么做的,我就是在这个坑里爬了一天才爬出来

  • 2、项目的build.gradle 的 dependencies 项和 allprojects 项代码结果

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        //以下两项是添加的
        classpath 'com.novoda:bintray-release:0.7.0'
}

allprojects {
    repositories {
        google()
        jcenter()
    }
    //下面这项解决  编码UTF-8的不可映射字符  亲测可用
    tasks.withType(Javadoc) {
        options{
            encoding "UTF-8"
            charSet 'UTF-8'
            links "http://docs.oracle.com/javase/7/docs/api"
        }
    }
}
  • 3、library下的build.gradle文件,我尽量为我能想到的每一项加上了注释
apply plugin: 'com.android.library'
//下面这项是添加的插件
apply plugin: 'com.novoda.bintray-release'



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

    lintOptions {
        abortOnError false
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
// read properties

//账户名
def bintrayUser = properties.getProperty("bintray.user")
//项目apikey
def bintrayApikey = properties.getProperty("bintray.apikey")
//novoda默认认识的bintray项目名称是maven,但是也可以自己定义,但是名称要和bintray上的对应
def publishRepoName     = properties.getProperty("publish.repoName")
//账户名
def publishUserOrg      = properties.getProperty("publish.userOrg")
//包名
def publishGroupId      = properties.getProperty("publish.groupId")
//项目名
def publishArtifactId   = properties.getProperty("publish.artifactId")
//版本号
def version             = properties.getProperty("publish.publishVersion")
//项目描述
def publishDesc         = properties.getProperty("publish.desc")
//项目首页地址
def publishWebsite      = properties.getProperty("publish.website")

//这种方式简单倒是简单了,但是一直找不到解决编码错误的问题
//注释有中文时总是报  GBK的不可映射文件  的错,找不到解决办法,于是回到老方式去了
publish {
    repoName        = publishRepoName
    userOrg         = publishUserOrg
    groupId         = publishGroupId
    artifactId      = publishArtifactId
    publishVersion  = version
    desc            = publishDesc
    website         = publishWebsite
}


/*
项目上传bintray命令,在android studio Terminal中执行

gradlew clean build bintrayUpload -PbintrayUser=hyman -PbintrayKey=xxxxxxxxxxxxxxxxxxxxxx -PdryRun=false

***一行输入,不要回车,中间的分割是空格***
 */

  • 4、下面给出我的本地的local.properties配置作为参考
ndk.dir=D\:\\ANDROID\\AndroidNDK\\android-ndk-r14b-windows-x86_64\\android-ndk-r14b
sdk.dir=D\:\\ANDROID\\AndroidSDK

#账户名
bintray.user=xiao******
#项目apikey
bintray.apikey=ab58***************************489e

#novoda默认认识的bintray项目名称是maven,看publish的源码发现repoName这个属性默认值是maven,也是相当的坑啊,但是也可以自己定义,不过名称要和bintray上的仓库名要对应
publish.repoName=maven
#账户名
publish.userOrg=xiao******
#包名
publish.groupId=com.ergouzi
#项目名
publish.artifactId=
#版本号
publish.publishVersion=1.0.0
#项目描述
publish.desc=.
#项目首页地址
publish.website=https://github.com/****/****

最后 就是在android studio 的Terminal中输入

gradlew clean build bintrayUpload -PbintrayUser=### -PbintrayKey=##### -PdryRun=false

命令,PbintrayUser是你的bintray用户名,PbintrayKey是你的API Key,等成功后就可以去bintray网站上去申请上传到Jcenter上了

具体申请方式也说一下吧
//在Add New Repository 按钮的下边是已经成功创建的仓库,如果你的本地项目已经上传成功,那么点击这个项目,里面会有一个上传成功的项目在下面被一个框圈起来,还有几个评分的小星星,点击这个项目,然后在右边的中下部会有一个 Add to Jcenter 的按钮,点击,然后写一下申请理由,发送申请就行了,应该不到一天就会给回复。

  • 终于写完了,成功通过申请后就可以在gradle中引用了,网络一大堆东西都看了,不知道谁对谁错,就不写参考链接了,有具体通不过的地方直接百度吧。

成功后查看地址:https://jcenter.bintray.com/com/liuchaoya/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值