如何在Android Studio上发布项目到Maven和JCenter

前言

我们在Android Studio过程当中,经常会通过在 build.grade 文件中添加一行代码来引入第三方的库。

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.1.1'
}

这对于我们来说真的是很方便、快捷!假如有一天我们自己写一个库,也希望让别人这样很方便的去使用,这应该如何去实现呢?那么接下来我讲的,就是如何去实现这个想法。

Step1:申请Bintray帐号

进入网页之后,进行注册。注册成功之后进入到Your Profile(个人资料页面),取得你的API key

《如何在Android Studio上发布项目到Maven和JCenter》 这里写图片描述

《如何在Android Studio上发布项目到Maven和JCenter》 这里写图片描述

下一步,创建你的Package

《如何在Android Studio上发布项目到Maven和JCenter》 这里写图片描述

填写你的项目信息

《如何在Android Studio上发布项目到Maven和JCenter》 这里写图片描述

这些都弄好了之后,接下来是我们的重头戏了。在Android Studio配置你的项目,这个过程你可能会一次成功,也有可能遭遇很多失败,没关系,让我们慢慢来。

Step2:在Android Studio配置项目

首先我要大家提醒大家的是,Project 的build.gradle 和Module的 build.gradle大家一定要分清楚,千万别搞混了,如果在接下来的步骤当中,如果你配置错了,你可以想想是不是这个原因。
好的我们开始

修改Project(项目)里的build.gradle,增加以下dependencies

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
        classpath 'com.android.tools.build:gradle:2.2.0-alpha6'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'

    }
}

大家看到上面的classpath 'com.android.tools.build:gradle:2.2.0-alpha6 使用的版本是2.2.0,所以你的项目中也尽量和我的一致,否则可能在构建的过程中会发生错误。
如果不一致,请修改你的项目目录下**/gradle/wrapper/gradle-wrapper.properties**中的

**distributionUrl**=https\://services.gradle.org/distributions/gradle-2.10-all.zip

下一步,

配置Module中(也就是你最后要上传的那个Library)中的build.gradle
这里面需要你加的东西,后面都会有注释,然后按照你bintray网站中填写的对应填写就可以了。

apply plugin: 'com.android.library'

ext {
    bintrayRepo = 'maven'
    bintrayName = 'ClearableEditText'//在bintray网站中项目中填写的名称

    publishedGroupId = 'com.zterry.clearableedittext'//一般为包名
    libraryName = 'ClearableEditText'//library名字需要和本地的lib module一致
    artifact = 'ClearableEditText'//同上

    libraryDescription = 'A clearable EditText which can be easy to clear your input.'//项目描述
    libraryVersion = '1.0.2'//项目版本号,只需要在每次更新的时候填写

    developerId = 'tata1989y'//开发者Id
    developerName = 'Terry Liu'//开发者名称
    developerEmail = 'tata1989y@gmail.com'//邮箱

    licenseName = 'The Apache Software License, Version 2.0'
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    allLicenses = ['Apache-2.0']
}


android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
        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(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.1.1'
}

然后在文件的末尾添加一下两行代码

apply from: 'https://raw.githubusercontent.com/attwellBrian/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/attwellBrian/JCenter/master/bintrayv1.gradle'

Step3:配置APIkey和username

打开项目根目录下的local.properties,添加你的用户名和API Key

bintray.apikey=[your apikey]
bintray.user=[your username]

PS: local.properties这个文件一定要添加到.gitIgnore里面,千万可不要上传到github上去哦!~

Step4:上传

打开Android Studio中的Terminal终端,输入以下命令

./gradlew install
./gradlew bintrayUpload

等待上传完毕之后,去自己的仓库首页,找到刚才上传的项目,点击

《如何在Android Studio上发布项目到Maven和JCenter》 这里写图片描述

接下来就是等待管理员审核通过,第一次时间会比较长,如果是更新的会很快!

大功告成!

End:总结:

在一开始,Google很多的资料,但是只有几篇文章是有效果的,因为中间配置的过程中发生了很多的错误,再此我把遇到的错误以及解决方案给大家分享出来。

Could not create package : HTTP/1.1 400 Bad Request
Package license is mandatory

解决:检查apiKey

Maven group, artifact or version defined in the pom file do not match
the file path

解决:检查 libraryName & artifact 名字需要一致

Q :No service of type Factory available in ProjectScopeServices
A :

```
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.0-beta1'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
```

参考文献

    原文作者:刘战塔_Terry
    原文地址: https://www.jianshu.com/p/dae45f21564e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞