开源android项目到jcenter,教你如何将自己的Android Studio项目开源并发布到JCenter

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

JCenter是当前世界上最大的Java和Android开源软件构件仓库,是新版Android Studio的默认远程仓库。

如何发布开源项目到JCenter

Step1

在你的github中新建repository,将你的项目上传到该目录下,这样你就有了项目的主页,可以在这里添加README.md介绍一下你的项目。

Step 2

注册一个 Bintray 账号。

Step 3

配置项目下的local.properties,新增两行。bintray.user后填入的是你在上一步注册的账号的用户名,bintray.apikey可以在你的账户信息中找到。1

2bintray.apikey=[your api key]

bintray.user=[your user name]

Step 4

配置项目下的build.gradle(project最外层的,而非module下的),仅有两行是新增的,已经标注出来。1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20buildscript {

repositories {

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:1.3.0'

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' //add

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'//add

// 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

}

Step 5

配置你想要发布的module下的build.gradle,这里以我发布的一个开源项目AndroidCalendarView为例。1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94apply plugin: 'com.android.library'

apply plugin: 'com.github.dcendents.android-maven' //add

apply plugin: 'com.jfrog.bintray' //add

version = "1.0.0" //add,表示发布的版本号

android {

compileSdkVersion 23

buildToolsVersion "23.0.2"

resourcePrefix "calendarview__" //add,表示res目录下的资源id的前缀

defaultConfig {

minSdkVersion 7

targetSdkVersion 23

versionCode 1

versionName "1.0"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.2.0'

}

//以下均为新增

def siteUrl = 'https://github.com/laserwave/Android-CalendarView' //github项目主页url

def gitUrl = 'https://github.com/laserwave/Android-CalendarView.git'//项目仓库url

group = "cn.zhikaizhang.calendar" // Maven Group ID for the artifact

install {

repositories.mavenInstaller {

// This generates POM.xml with proper parameters

pom {

project {

packaging 'aar'

name 'Android-CalendarView' //填写项目描述

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 'zhikaizhang' //填写开发者基本信息

name 'zhikaizhang'

}

}

scm {

connection gitUrl

developerConnection gitUrl

url siteUrl

}

}

}

}

}

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

}

task javadocJar(type: Jar, dependsOn: javadoc) {

classifier = 'javadoc'

from javadoc.destinationDir

}

artifacts {

archives javadocJar

archives sourcesJar

}

Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {

user = properties.getProperty("bintray.user")

key = properties.getProperty("bintray.apikey")

configurations = ['archives']

pkg {

repo = "maven"

name = "Android-CalendarView" //填写发布到JCenter上的项目名字

websiteUrl = siteUrl

vcsUrl = gitUrl

licenses = ["Apache-2.0"]

publish = true

}

}

Step 6

生成javadoc.jar,sources.jar,aar等文件。在项目目录下打开命令行,执行以下命令。这条命令在第一次执行时花费时间较长(由于需要下载一些文件),以后再次生成就会快上很多。1gradlew install

Step 7

上传至Bintray1gradlew bintrayUpload

Step 8

打开这个网址,点击Include My Package按钮,然后搜索到你的项目,填好Comments后提交到Bintray,经过几十分钟的审核,就OK了。此时,你已经可以通过添加gradle依赖来引用你自己的这个项目了。

如何使用你刚刚开源的这个项目呢

以Android-CalendarView项目为例,可以像下面这样使用,格式是 groupId:artifactId:version。其中groupId和version均是在前述的配置文件中设定的,而artifactId则是你开源的这个module的名称。1

2

3dependencies {

compile 'cn.zhikaizhang.calendarview:library:1.0.0'

}

如何更新你的开源项目

修改好要发布的module后,在该module下的build.gradle中增大版本号,再次执行上面步骤的Step 6, 7中的两行代码即可完成更新。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值