发布开源项目到Jcenter从0到1,自身经历

把自己的依赖放到JCenter 首先需要注册Bintray ,因为Bintray 是 JCenter 库的运营商。

  1. 首先你要有一个项目,准确的说应该有一个Android moudle并把项目上传到GitHub

  2. 注册 :网址https://bintray.com/signup/oss这里注册一定要注册个人账号

  3. Add New Repository

    发布开源项目到Jcenter从0到1,自身经历_第1张图片

    发布开源项目到Jcenter从0到1,自身经历_第2张图片


  4. Repository添加成功后会跳转到Repository首页,接下来需要Add a Package

    发布开源项目到Jcenter从0到1,自身经历_第3张图片

  5. package添加成功后去package主页Add a Version

    发布开源项目到Jcenter从0到1,自身经历_第4张图片


    到此,在Bintray上的操作就告一段落

    现在回到AndroidStudio进行配置

  6. 在Project的build.gradle添加依赖(项目最外层build.gradle)

    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'
        //以下两项依赖需要添加
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
        classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    
  7. 在local.properties配置Bintray的用户名和Apikey

    bintray.user=你的用户名
    bintray.apikey=你的apikey
    
  8. 在moudle的build.gradle添加如下属性注意是最上面(在apply plugin: 'com.android.library’之上)

    ext {
        bintrayRepo = 'appUpdate' // 之前添加的Repository name
        bintrayName = 'com.sanzhi.appupdate' // 之前添加的package name
        publishedGroupId = 'com.sanzhi.appupdate'   // 默认应该和package name一样 this is the ID we want to see in implementation line
        libraryName = 'appupdate'     // moudle的名称this is the module name of library
        artifact = 'appupdate'        // moudle的名称this is the artifact we want to see in implementation line
        libraryDescription = 'self app updata library' // description of library
        siteUrl = 'https://github.com/parade0393/AppUpdate'    // 项目再github的主页git repo url
        gitUrl = 'https://github.com/parade0393/AppUpdate.git' // 项目再GitHub的仓库地址git repo vcs url
        libraryVersion = '1.0.0'      // 之前添加的版本号library version后续更新也是改这个版本号
        developerId = 'parad0393'                // This is your bintray username
        developerName = 'parad0393'              // Developer's name
        developerEmail = '[email protected]'                // Developer's email
        licenseName = 'The Apache Software License, Version 2.0'  // for example, The Apache Software License, Version 2.0
        licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'   // for example, http://www.apache.org/licenses/LICENSE-2.0.txt
        allLicenses = ["Apache-2.0"]    // array of licenses, for example, ["Apache-2.0"]
    }
    
  9. 在moudle下新建publish.gradle文件,并在文件添加如下内容

    apply plugin: 'com.jfrog.bintray'
    apply plugin: 'com.github.dcendents.android-maven'
    version = libraryVersion
    group = publishedGroupId    // Maven Group ID for the artifact
    install {
        repositories.mavenInstaller {
            // This generates POM.xml with proper parameters
            pom {
                project {
                    packaging 'aar'
                    // 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
                    }
                }
            }
        }
    }
    task sourcesJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
        archiveClassifier  = 'sources'
    }
    artifacts {
        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
            }
        }
    }
    //这里不需要做修改
    
  10. 回到moudle的build.gradle在apply plugin: 'com.android.library’后面添加apply from: ‘publish.gradle’

  11. 至此在AndroidStudio配置完成,接下来就是打包上传到Bintray,在Studio的terminal中依次输入以下两个命令

    gradlew install
    gradlew bintrayUpload
    
  12. 正常情况下执行完以上两个命令,代码就已经上传到Bintray里,现在还剩最后一步,回到Bintray,打开本package页面 Add to Jcenter

    发布开源项目到Jcenter从0到1,自身经历_第5张图片

  13. 等待审核通过就可以在AndroidStudio里直接implementation了

  14. 参考文章

    1. How to Publish an Open Source Android Library on JCenter
    2. 新版bintray教你上传JCenter仓库

你可能感兴趣的:(Android)