Jcenter

如何让别人一行代码就能使用自己的library

序:一直很好奇为什么我使用github上的许多开源项目时,只要在gradle上写上一行compile 'com.android.volley:volley:1.0.0'就OK了。正好这段时间想把自己写的几个自定义view放上去于是花了一天时间终于成功实现了。步骤其实很简单

  1. 准备工具Android Studio
  2. 首先要去bintray注册一个账号。
  3. 打开在module下的local.properties文件,添加一下代码

    bintray.user=USER_NAME//这里自己替换为刚注册的用户名
    bintray.apikey=YOUR_API_KEY//apikey在Your Profile页面的最下面可以看到
    
    #developer
    developer.id=YOUR_ID//你的昵称,可以随意填
    developer.name=YOUR_NAME//真实名字,随意填
    developer.email=YOUR_EMAIL//你的邮箱  
    
  4. 新建一个project.properties文件,如果有就不用新建了。加上一下内容

    #project
    project.name=           //你的项目名称
    project.groupId=        //groupId,如果你的包名为com.android.test。一般填com.anroid就可以了
    project.artifactId=     //项目名,如果你的包名为com.android.test。一般填test就可以了
    project.packaging=aar   //你是Android项目不用改
    project.siteUrl=        //填你项目的所在地址,一般填github的路径
    project.gitUrl=         //git路径,这里我就是在siteUrl上加了个.git
    
    #javadoc
    javadoc.name=       //生成的文档的名字,填project.name就可以了
    
  5. 在module下的build.gradle后面追加以下代码

    ext {
        bintrayRepo = 'maven'   
        bintrayName = 'per.yrj' 
    
        publishedGroupId = 'per.yrj'
        libraryName = 'IndexedListView'
        artifact = 'indexed-list-view'
    
        libraryDescription = 'this is a lib'//描述信息
    
        siteUrl = 'https://github.com/mycolorful/IndexedListView'
        gitUrl = 'https://github.com/mycolorful/IndexedListView.git'
    
        libraryVersion = '1.0.1'
    
        developerId = 'mycolorful'
        developerName = '****'  //替换为自己的
        developerEmail = 'mycolorful@live.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: "bintrayUpload.gradle"//这一句别忘了,用来引用下面的文件
    
  6. 接下来在module下新建一个bintrayUpload.gradle文件(如果你不想做这一步的话那就直接把apply from: "bintrayUpload.gradle"给删了,然后加上apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
    apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'效果是一样的,可以直接跳到第7步了)

apply plugin: 'com.github.dcendents.android-maven'
    apply plugin: 'com.jfrog.bintray'

    version = libraryVersion

    if (project.hasProperty("android")) { // Android libraries
        task sourcesJar(type: Jar) {
            classifier = 'sources'
            from android.sourceSets.main.java.srcDirs
        }

        task javadoc(type: Javadoc) {
            source = android.sourceSets.main.java.srcDirs
            classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        }
    } else { // Java libraries
        task sourcesJar(type: Jar, dependsOn: classes) {
            classifier = 'sources'
            from sourceSets.main.allSource
        }
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }

    artifacts {
        archives javadocJar
        archives sourcesJar
    }

    // Bintray
    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 = bintrayRepo
            name = bintrayName
            desc = libraryDescription
            websiteUrl = siteUrl
            vcsUrl = gitUrl
            licenses = allLicenses
            publish = true
            publicDownloadNumbers = true
            version {
                desc = libraryDescription
                gpg {
                    sign = true //Determines whether to GPG sign the files. The default is false
                    passphrase = properties.getProperty("bintray.gpg.password")
                    //Optional. The passphrase for GPG signing'
                }
            }
        }
    }
    group = publishedGroupId                               // Maven Group ID for the artifact

    install {
        repositories.mavenInstaller {
            // This generates POM.xml with proper parameters
            pom {
                project {
                    packaging 'aar'
                    groupId publishedGroupId
                    artifactId artifact

                    // Add your description here
                    name libraryName
                    description libraryDescription
                    url siteUrl

                    // Set your license
                    licenses {
                        license {
                            name licenseName
                            url licenseUrl
                        }
                    }
                    developers {
                        developer {
                            id developerId
                            name developerName
                            email developerEmail
                        }
                    }
                    scm {
                        connection gitUrl
                        developerConnection gitUrl
                        url siteUrl

                    }
                }
            }
        }
    }

至此就差不多完成了。
7. 开始上传:

打开Terminal,输入gradlew install,如果输出

BUILD SUCCESSFUL

那么恭喜你,我们可以离成功只有一步之遥了。接着输入gradlew bintrayUpload,如果接着输出了(这个过程可能比较久)

SUCCESSFUL

这就说明你已经将项目成功上传到bintray上了。你可以到bintray在maven下面,就能有一个项目了。
8. 进入你的项目,点击Add to Jcenter
直接点send
等待审核通过(两三个小时)。
9. 在这里你可以看到自己的gradle地址,别人使用你的项目时只需要在Gradle中复制这一行上去就可了

参考链接:如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
Android Studio发布项目到Jcenter
http://www.jianshu.com/p/0e7b8e14f0cd/comments/1050253

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值