使用 gradle-bintray-plugin 發佈項目到jcenter

1.註冊

1.在Bintary上註冊賬號,紅色框框看見沒

2.Add New Repository 創建maven

2.工程

1.創建Android工程 及 Library

創建一個工具類 調用showLog() 則打印    Log.w("hello world","i am from androidlibrary")

3.配置 gradle-bintray-plugin

1.Project的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // 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.8.4'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

    }
}

allprojects {
    repositories {
        google()
        jcenter()
      
    }
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
        options.addStringOption('charSet', 'UTF-8')
    }
}

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

//完成2.3.後配置
tasks.getByPath(":androidlibrary:javadoc").enabled = false

2.LibraryModule目錄下新建 install.gradle 及 publish.gradle

install.gradle

//instal.gradle
apply plugin: 'com.github.dcendents.android-maven'

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

                }
            }
        }
    }
}

publish.gradle

//publish.gradle
apply plugin: 'com.jfrog.bintray'

version = libraryVersion


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
}
artifacts {
    archives javadocJar
    archives sourcesJar
}

task findConventions << {
    println project.getConvention()
}

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

3.LibrarModule的build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'//需添加的
apply plugin: 'com.jfrog.bintray'//需添加的
version = "1.0.0"//版本號,每次更新記得修改
android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    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:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

//gradlew bintrayUpload
//下面到結尾都是需要添加的
ext {
    bintrayRepo = 'MyTestMaven' // 對應Bintray網站中,創建Repository的命名
    bintrayName = 'androidlib' // 對應Bintray網站中,創建的package的命名(會新建)

    publishedGroupId = 'com.cuzyou.android' // 隨意,對應之後引用工程的 第一段名稱
    libraryName = 'MyTestLibrary'    // 最好是工程名
    artifact = 'MyTestLibrary'    // 最好是工程名,對應之後引用工程的 第二段名稱

    libraryDescription = 'Library of tools used in development' // 工程的描述

    siteUrl = 'https://github.com/cuzyou' // github項目地址
    gitUrl = 'https://github.com/cuzyou' // github項目地址git

    libraryVersion = this.version // 隨意,對應之後工程的 第三段名稱

    developerId = 'cuzyou' // 用戶名ID,對應創建bintray賬號的名稱ID
    developerName = 'cuzyou' // 用戶名ID,對應創建bintray賬號的名稱ID
    developerEmail = '****.com' // 郵箱,對應創建bintray賬號的郵箱

    licenseName = 'The Apache Software License, Version 2.0' // 固定
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' // 固定
    allLicenses = ["Apache-2.0"] // 固定
}

apply from: 'install.gradle'
apply from: 'publish.gradle'

javadoc { // 引用這個,是爲了解決註釋中,有中文,然後編譯不通過的坑
    options {
        encoding "UTF-8"
        charSet 'UTF-8'
        //author true
        version true
        title 'A LibSDK Support For Android'   // 文檔標題
    }
}

4.在local.properties 配置Bintary的 user 和 apikey

## This file must *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.
#Thu Nov 22 10:54:41 CST 2018
bintray.user=Bintray上的user
bintray.apikey=Bintray上的apikey

4.上傳項目 gradlew bintrayUpload

1.如果有如下報錯

已知解決方案:

(1)掛上你的VPN

(2)

(3)

再次 gradlew bintrayUpload ,build successful 則成功上傳

5.審覈使用

1. Add to JCenter 進行審覈,內容不要爲空

    審覈成功就可以直接 implementation 'com.cuzyou.android:MyTestLibrary:1.0.0'

2. 使用審覈中的開源項目

(1) 在Project的 build.gradle中添加

allprojects {
    repositories {
        google()
        jcenter()
        maven {url 'https://dl.bintray.com/cuzyou/MyTestMaven'}
    }
}

(2) 在app的build.gradle添加依賴

implementation 'com.cuzyou.android:MyTestLibrary:1.0.0'

3.測試:調用MyTestLibrary中工具類的方法

6.更新Library

修改LibraryModel的build.gradle中  version = "1.0.0",每次版本號必須大於上次版本號

7.友情提示

項目提交GitHub的時候記得別提交local.properties哈,附上.gitignore

# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/caches

# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md

*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild

8.項目地址有毛病不關我事哈

9. 嗎的這麼長,告辭

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章