使用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

相關文章
相關標籤/搜索