Android Studio项目发布到Maven仓库的入坑之路

一、注册账号

首先你需要注册一个Bintray账号,注意此处需要点击Sign Up Here注册(或者直接访问https://bintray.com/signup/oss) 。绿色按钮是注册企业账号的,需要付费,有试用期,我们需要注册开发者账户,有很多文章没有标明这个问题。

记得访问,不然怀疑人生!!

Android Studio项目发布到Maven仓库的入坑之路_第1张图片

 

二、创建仓库

Android Studio项目发布到Maven仓库的入坑之路_第2张图片

Android Studio项目发布到Maven仓库的入坑之路_第3张图片

 

三、使用bintray-release上传,地址 https://github.com/novoda/bintray-release

Step 1 、配置根目录的build.gradle


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //classpath 'com.novoda:bintray-release:'
        classpath 'com.novoda:bintray-release:0.9.1'//上传到jcenter用到
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.18"//上传到jcenter kotlin用到
    }
}

Step 2、配置module的build.gradle


apply plugin: 'com.novoda.bintray-release'

publish {
    userOrg = 'ljyfree' //bintray账户下某个组织id
    groupId = 'com.jy' //maven仓库下库的包名,一般为模块包名
    artifactId = 'baselibrary' //项目名称
    publishVersion = '1.0.1' //版本号
    desc = 'a easy API base frame to use for Android M ' //项目介绍,可以不写
    website = 'https://github.com/jyfree/YLibrary' //项目主页,可以不写
    licences = ['Apache-2.0']
}

注意:

  • groupId、artifactId、publishVersion就是使用时依赖的组成;
  • userOrg组织者,默认为用户名,或手动创建一个。可以点击账户下Create Organization–>Create new organization创建,也可以点击Manage Organizations–>New Organization–>Create new organization创建。创建完成后,将组织ID填写到userOrg中

Step 3、上传

首先我们需要获取API Key,Bintray网站点击右上角用户名–>Edit Your Profile -> API Key -->输入密码–>Submit–>Show。

Android Studio项目发布到Maven仓库的入坑之路_第4张图片

 

 在Android Studio的Terminal面板中执行下面命令,其中BINTRAY_USERNAME替换为你的binatray用户名,BINTRAY_KEY替换为上面获取的API Key,-PdryRun=false会上传到仓库中,如果为true,只会执行gradle任务,但不会上传。替换完成后回车执行

gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false

执行完成看到BUILD SUCCESSFUL就算是成功了。这时候点击组织下maven仓库(或者直接访问https://bintray.com/组织ID/maven)就可以看到上传的类库:

Android Studio项目发布到Maven仓库的入坑之路_第5张图片

 

Step 4、使用

我们需要告诉gradle依赖包仓库的位置,在项目根目录下·build.gradle·中添加:

allprojects {
    repositories {
    google()
    jcenter()
    //仓库地址
    maven { url "https://dl.bintray.com/ljyfree/maven" }
    }
}

和然后在app模块build.gradle中添加依赖:

implementation 'com.jy.baselibrary:baselibrary:1.0.1'

如果我们将项目添加到JCenter中,以后依赖就不需要添加maven { url "https://dl.bintray.com/ljyfree/maven" }了,当然还是需要告诉gradle依赖库的仓库位置,只需要添加jcenter()就行了。

Android Studio项目发布到Maven仓库的入坑之路_第6张图片

注:若有本地aar,则需要此方式引入implementation files(xxx.aar),不然最后打包的aar引用不到(引用后还需手动导入本地aar到app中)

笔者发布的开源项目:https://github.com/jyfree/YLibrary

你可能感兴趣的:(android)