将自己的lib上传到Jcenter供项目动态加载

来源:互联网 发布:mysql 0xc000007b 编辑:程序博客网 时间:2024/06/04 19:03

随着动态库越来越多,本地项目变得也越来越臃肿,如果把自己的库上传到公共管理库里面(当然如果你想开源的话),项目动态加载引入,管理起来也方便

 申请Bintray账号

 我们需要一个账号,Bintray传送门,注册完成后,进入个人中心,选择编辑,然后获取API Key

配置gradle

选择项目工程的build.gradle

buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.1.2'        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'        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()    }    tasks.withType(Javadoc) {//配置编码格式        options.addStringOption('Xdoclint:none', '-quiet')        options.addStringOption('encoding', 'UTF-8')    }}
然后在你需要发布的那个module 的build.gradle里配置如下内容:

apply plugin: 'com.android.library'apply plugin: 'com.github.dcendents.android-maven'apply plugin: 'com.jfrog.bintray'// This is the library version used when deploying the artifactversion = "1.0.0"android {    compileSdkVersion 21    buildToolsVersion "21.1.1"    resourcePrefix "wxlikevideo_demo"    defaultConfig {        minSdkVersion 14        targetSdkVersion 21        versionCode 1        versionName version    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt','proguard-rules.pro'        }    }}dependencies {    compile 'com.android.support:support-v4:21.0.3'    compile fileTree(dir:'libs', include:['*.jar'])}def siteUrl='https://github.com/xxx';def gitUrl='https://github.com/xxx.git'; group='xxx';//项目包名 install {    repositories.mavenInstaller {        // This generates POM.xml with proper parameters        pom {            project {                packaging 'aar'                // Add your description here                name 'xxx'    //项目描述                url siteUrl                // Set your license                licenses {                    license {                        name 'The Apache Software License, Version 2.0'                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'                    }                }                developers {                    developer {                        id 'xxx'    //填写的一些基本信息                        name 'xxx'                        email 'xxx'                    }                }                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 = "xxxx"  //发布到JCenter上的项目名字        websiteUrl = siteUrl        vcsUrl = gitUrl        licenses = ["Apache-2.0"]        publish = true    }}
在local.properties文件中(文件一般需gitignore,防止泄露账户信息)配置bintray的账户信息(第一步获取到的)

2
bintray.user=your_user_namebintray.apikey=your_apikey
Rebuild 一下项目,如果没有报错的话,就可以进行上传了,找到android studio 中的Terminal输入如下命令安装gradlew(如果没有的话)

gradlew install
然后开始上传

gradlew bintrayUpload
如果没有问题的话,在Bintray网站上找到你的Repo(当然过程中会有其他一些问题,就自行网上找方案解决吧~)

 我们需要完成最后一步工作,申请你的Repo添加到JCenter。可以进入这个页面, 找到匹配到的项目,然后输入一些内容即可,然后就等管理员批准了,然后网站上会给你一条通过信息,然后就大功告成。(审核可能会需要1个小时左右)

成功后就可以在其它项目里方便的使用你发布的项目了:

123
dependencies {    compile '[group]:library:1.0.0'}
group为上面配置的ID
参考链接:http://rocko.xyz/2015/02/02/%E4%BD%BF%E7%94%A8Gradle%E5%8F%91%E5%B8%83%E9%A1%B9%E7%9B%AE%E5%88%B0JCenter%E4%BB%93%E5%BA%93/
0 0
原创粉丝点击