您的位置:首页 > 移动开发 > Android开发

android studio创建代码库分上传到jcenter,使大家一行代码引用

2016-02-22 12:14 573 查看

第一步:

先新建一个项目,然后在这个项目下新建一个Android LIbrary的Module,自己的库就在这个Module里面开发,然后,我们原来新项目,可能当作一个演示。


第二步:

在这个新建的Module里面,开发自己的开源项目,然后,在原来的项目里是可以直接引用Module里在代码的。但运行的时候会报错,需要在项目的build.gradle中添加
compile project(':buttonlibrary')


后面单引号里面,是自己创建的Module名字。
然后,再重新运行整个项目,就可以把Module也编译进去了。

第三步:

编写build.gradle文件。
先打开项目的build.gradle文件,一定是项目的build文件,然后在里面添加:

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'


添加完这些话以后,完整的应该是这样的:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1" classpath 'com.github.dcendents:android-maven-gradle-plugin:1.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
}


注意,这里只是我的库,这个文件基本没怎么动,有的项目,可能会根据项目,重新编写这个文件。但其实 ,添加的,只是dependencies里面。

然后,

打开,注意,一定要是这个Module的build.gradle文件,改成这样的,

apply plugin: 'com.android.library'

version = "1.0.2"                                //项目版本号
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 15
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'
}
ext {
bintrayRepo = 'maven'
bintrayName = 'voiceLine'                                //项目名字

publishedGroupId = 'com.carlos.voiceline'                //GroupId,这个要别人引用的时候会用到,
libraryName = 'voiceLineLibrary'                         //这个是项目名字
artifact= 'mylibrary'                                   //artifactId,这个也是别人引用的时候,会用到,它要和Module名字一样

libraryDescription = '一个可以根据音量显示波形的控件'       //项目描述

siteUrl = 'https://github.com/ws123/VoiceLine'          //把项目分享到github后的项目地址
gitUrl = 'https://github.com/ws123/VoiceLine.git'       //分享后的项目git地址

libraryVersion = '1.0.2'
developerId = 'carlos'                             //这个开发者id,自己写
developerName = 'carlos'                           //这个是开发者名字
developerEmail = '*****@ll.com'                    //开发者邮箱

licenseName = 'The Apache Software License, Version 2.0'     //开源协议
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'


基本上,需要自定义的地方,都已经加上注释了,

然后在需要在本地的local.properties里面添加以下内容:

bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY


需要在jcenter是注册一个自己的账户,第一个账户名字,第二个是账户的API key,这个都可以在自己的账户设置页面找到。
这个文件不会上传到网上,不会使自己的账户泄露。

第四步:

打开android studio的命令行:



输入命令:

gradlew install
gradlew bintrayUpload
这两个命令都要成功,显示BUILD SUCCESSFUL才可以。
然后,打开jcenter页面,打开这个项目,



点击Add to JCenter,进入下一个页面,直接点SEND就可以了。
要审核,两三个小时就可以了。
然后,你就可以引用了

compile(group: 'com.carlos.notificatoinbutton', name: 'buttonlibrary', version: '1.0.0', ext: 'pom', ')


这个代码就是引用的格式,和你填写的文件是对应的,分别是
publishedGroupId
artifact
和版本号。

最后给大家介绍两篇类似的文章,大家参考:
http://blog.csdn.net/wangdong20/article/details/50098535 http://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: