美文网首页武功秘籍
【Android&Jcenter】将Gradle And

【Android&Jcenter】将Gradle And

作者: renkuo | 来源:发表于2018-01-03 10:43 被阅读97次

    1.去jcenter官网注册个账号

    https://bintray.com

    51.png

    2.在项目根目录下build.gradle文件中的dependencies节点中追加如下代码:

    注:保持你的library module的名字同artifactId一样,因为在Bintray上你的项目的maven-metadata.xml文件的路径是gruopId+"/"+module名称。例如你的包名是com.renkuo.personal.utilslibrary,groupId是com.renkuo.personal,artifactId是utilslibrary,module名称是utilslibrary,这样便于别人引用时很容易找到资源

    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
    

    build.gradle示例如下

    buildscript {
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0'
            classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
            classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
        }
    }
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    其中:
    android-maven-gradle-plugin插件是用来打包Maven所需文件的
    gradle-bintray-plugin插件是用来将生成的Maven所需文件上传到Bintray的

    3.添加project.properties文件并放到你的library module目录下

    #project
    project.name=UtilisLibrary
    project.groupId=com.renkuo.personal
    project.artifactId=utilslibrary
    project.packaging=aar
    project.siteUrl=https://github.com/renkuo/UtilsLibraryDemo
    project.gitUrl=https://github.com/renkuo/UtilsLibraryDemo.git
    
    #javadoc
    javadoc.name=UtilisLibrary
    
    • project.name:项目名称
    • project.groupId:项目组ID,通常情况下如果你的包名为com.renkuo.personal.utilslibrary,那么项目组ID就是com.renkuo.personal
    • project.artifactId:项目ID,通常情况下如果你的包名为com.renkuo.personal.utilslibrary,那么项目ID就是utilslibrary
    • project.packaging:包类型,Android库是aar
    • project.siteUrl:项目官方网站的地址,没有的话就用Github上的地址,例如:https://github.com/renkuo/UtilsLibraryDemo
    • project.gitUrl:项目的Git地址,例如:https://github.com/renkuo/UtilsLibraryDemo.git
    • javadoc.name:生成的javadoc打开后主页显示的名称,通常跟项目名称一样即可

    4.添加local.properties文件并放到你的library module目录下

    #bintray
    bintray.user=huoleifeng
    bintray.apikey=**************************************
    
    #developer
    developer.id=renkuo
    developer.name=renkuo
    developer.email=renkuo0617@gmail.com
    
    • bintray.user:你的Bintray的用户名
    • bintray.apikey:你的的Bintray的API Key
    • developer.id:通常是你在开源社区的昵称
    • developer.name:你的姓名
    • developer.email:你的邮箱

    其中apikey获取方式如下图
    注:一定要将local.proerties文件加入忽略列表,以免被提交到Github或其他网站泄露个人信息
    ①.点击Edit Profile选项

    5856.png ②.点击API Key选项然后确认一下密码 75815.png

    5.添加本地bintrayUpload.gradle文件并放到你的library module目录下

    apply plugin: 'com.github.dcendents.android-maven'
    apply plugin: 'com.jfrog.bintray'
    
    // load properties
    Properties properties = new Properties()
    File localPropertiesFile = project.file("local.properties");
    if(localPropertiesFile.exists()){
        properties.load(localPropertiesFile.newDataInputStream())
    }
    File projectPropertiesFile = project.file("project.properties");
    if(projectPropertiesFile.exists()){
        properties.load(projectPropertiesFile.newDataInputStream())
    }
    
    // read properties
    def projectName = properties.getProperty("project.name")
    def projectGroupId = properties.getProperty("project.groupId")
    def projectArtifactId = properties.getProperty("project.artifactId")
    def projectVersionName = android.defaultConfig.versionName
    def projectPackaging = properties.getProperty("project.packaging")
    def projectSiteUrl = properties.getProperty("project.siteUrl")
    def projectGitUrl = properties.getProperty("project.gitUrl")
    
    def developerId = properties.getProperty("developer.id")
    def developerName = properties.getProperty("developer.name")
    def developerEmail = properties.getProperty("developer.email")
    
    def bintrayUser = properties.getProperty("bintray.user")
    def bintrayApikey = properties.getProperty("bintray.apikey")
    
    def javadocName = properties.getProperty("javadoc.name")
    
    group = projectGroupId
    
    // This generates POM.xml with proper parameters
    install {
        repositories.mavenInstaller {
            pom {
                project {
                    name projectName
                    groupId projectGroupId
                    artifactId projectArtifactId
                    version projectVersionName
                    packaging projectPackaging
                    url projectSiteUrl
                    licenses {
                        license {
                            name 'The Apache Software License, Version 2.0'
                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }
                    developers {
                        developer {
                            id developerId
                            name developerName
                            email developerEmail
                        }
                    }
                    scm {
                        connection projectGitUrl
                        developerConnection projectGitUrl
                        url projectSiteUrl
                    }
                }
            }
        }
    }
    
    // This generates sources.jar
    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))
    }
    
    // Add compile dependencies to javadoc
    afterEvaluate {
        javadoc.classpath += files(android.libraryVariants.collect { variant ->
            variant.javaCompile.classpath.files
        })
    }
    
    // This generates javadoc.jar
    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }
    
    artifacts {
        archives javadocJar
        archives sourcesJar
    }
    
    // javadoc configuration
    javadoc {
        options{
            encoding "UTF-8"
            charSet 'UTF-8'
            author true
            version projectVersionName
            links "http://docs.oracle.com/javase/7/docs/api"
            title javadocName
        }
    }
    
    // bintray configuration
    bintray {
        user = bintrayUser
        key = bintrayApikey
        configurations = ['archives']
        pkg {
            repo = "maven"
            name = projectName
            websiteUrl = projectSiteUrl
            vcsUrl = projectGitUrl
            licenses = ["Apache-2.0"]
            publish = true
        }
    }
    

    然后修改你的library module的build.gradle文件,在最后加上apply from: "bintrayUpload.gradle",如下所示:

    apply plugin: 'com.android.library'
    android {
        compileSdkVersion 26
        buildToolsVersion = "26.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 {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
    }
    apply from: "bintrayUpload.gradle"
    

    注:apply from: "bintrayUpload.gradle"一定要放在最后,如果放在开头的话汇报错Error:Cause: android.compileSdkVersion is missing!如下图:

    174311.png

    6.执行命令打包并上传到Bintray

    打开终端进入项目目录下,执行gradlew bintrayUpload命令

    命令执行完成后可能会报错,如下图所示: 175339.png 这是因为你新建的账号中没有建库(已创建库的可以忽略这个问题),步骤如下:
    175601.png ② 75642.png

    7. 请求提交你的项目到jcenter

    上述步骤走完之后实际上只是上传了你的项目到Bintray而已,并没有被包含在jcenter中,要想提交到jcenter中还需要Bintray的审核。步骤如下:

    ①点击进入你的maven项目列表 80310.png ②点击选择你要提交到jcenter的项目 0340.png ③点击Add to JCenter,然后在Comments输入框里随便填写下信息,最后点Send提交请求即可(一般情况下审核需要4到5个小时,耐心等待就行了,审核通过后会给你发邮件通知你,并且以后更新项目就不需要再审核了。) 180437.png

    引用方式

    点击查看你添加到jcenter的库,选择gradle

    `compile 'com.renkuo.personal:utilslibrary:1.0'`
    
    12059.png

    最后

    JitPack也是一个代码仓库,等同于jCenter,目前用的人比较少,但是已经有越来越多的人开始使用了。它不像Jcenter那么麻烦,不需要上传,编译,审核。总之就是so eazy。大家有兴趣可以参考我的另一片文章https://www.jianshu.com/p/2b3eb6d26806

    错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

    相关文章

      网友评论

        本文标题:【Android&Jcenter】将Gradle And

        本文链接:https://www.haomeiwen.com/subject/ljtwgxtx.html