使用Android Studio 配置Gradle 搭建远端仓库

之前我本身整理的工具类会打一个jar包,而后处处复制。自从IDE换到AS后,使用gradle导入其余工程,只要简单的一行代码,甚至还有界面化搜索功能,实在是好用的很,因此最近整理了一些工具类就想作成这种方式。因而查了些资料得知,JCenter如今是Android Studio中repositories的默认节点,因此要把项目上传到JCenter这个远端仓库,https://bintray.com/网站管理着JCenter仓库。java

因为上传项目时,遇到了一些小问题,我根据网上的资料,整理一下这个过程。android

配置Gradlegit

Module的build.gradle:github

apply plugin: 'com.android.library' //把application改为library

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        //applicationId "包名"  //须要注释掉applicationId,由于在rebuild的时候报错,具体缘由多是由于第一行改为library的缘由,不是很清楚。
        minSdkVersion 9
        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'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
}

//以上部分基本是由IDE生成

//添加插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

//定义版本
version = "0.0.1"

//定义至关网站
def siteUrl = 'https://github.com/gitusername/projectname'    // project homepage
def gitUrl = 'https://github.com/gitusername/projectname.git' // project git

//定义GROUP
group = "包名"

//定义pom并打包aar
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name ''//一些描述
                url siteUrl
                licenses {
                    license {
                        name 'The Apache software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {//开发者信息
                        id ''
                        name ''
                        email ''
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

//打包javadocjar和sourcejar
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
}

javadoc {
    options{
        encoding 'UTF-8'
        charSet 'UTF-8'
        author true
    }
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

//上传到maven仓库,从local.properties读取user和apikey
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"  //在网站上建立的仓库名,在https://bintray.com/上注册完账号时,已经有了一个组织(organization),可是没有仓库(Repositories),须要本身先建立一个仓库
        name = "projectname"                // project name in maven
        userOrg = ''  //组织名
        //这里是困我最长时间的地方,网站上建立组织(organization)时组织名称不能与用户名(user)相同
        //而在上传的时候,默认路径是user/repo/name 正确的位置应该是organization/repo/name
        //因为默认使用user,因此上传时一直报404错误
        //网上的几个资料都没有指出配置userOrg,我天然也不知道用什么字段能够指定正确路径
        //因为我猜想字段是organization,而后报了错,并提示了一个网址,因而经过这个网址找到了bintray的GITHUB项目(https://github.com/bintray/gradle-bintray-plugin),找到了相关配置
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

Module的build.gradle:web

// 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'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        //添加下边这两句
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

local.properties:apache

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/dean/Dev/bak/adt-bundle-mac-x86_64-20140702/sdk

bintray.user=      //网站用户名  若是报401错误,多是user或者apikey写错了
bintray.apikey=    //登陆网站后,找到编辑简介(Edit Profile)-> API Key

Rebuild一下,使用命令行(AS自带)上传:api

gradlew bintrayUploadapp

 

上传成功后,在网站(https://bintray.com)本身建立的仓库里找到上传的项目,在项目详情中找到"Add to JCenter" ,等待审核经过。maven

相关文章
相关标签/搜索