Android 依赖库发布(上传 Library 到 JCenter)支持gradle4.5+

1,646 阅读1分钟

Bintray的配置可以参考这里 Android 依赖库发布(上传 Library 到 JCenter)gradle最高支持4.4

配置项目

在项目的build.gradle中配置如下:

下面提供代码方便复制:

classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
 gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }

如果注释中有中文,可以添加下面代码防止乱码

allprojects {
    tasks.withType(Javadoc) {
        options {
            encoding "UTF-8"
            charSet 'UTF-8'
            links "http://docs.oracle.com/javase/7/docs/api"
        }
    }
}

要发布的 module 下的 build.gradle 对比如下代码添加配置:

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
// This is the library version used when deploying the artifact
version = "1.0.1"
android {
    compileSdkVersion 28
    resourcePrefix "androidTool"    //这个随便填
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName version
    }
    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'

}
def siteUrl = 'https://github.com/lsmya/androidTool'     
def gitUrl = 'https://github.com/lsmya/androidTool.git'  
def issuesUrl = 'https://github.com/lsmya/androidTool/issues'  
group = "cn.lsmya"                                      
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'Android tool'    //项目描述
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'lsmya'      
                        name 'lsmya'
                        email 'lsmya@qq.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
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
}
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"
        name = "tool"    //发布到JCenter上的项目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        issueTrackerUrl = issuesUrl
        publish = true
    }
}

项目的local.properties文件中添加:

bintray.user=用户名
bintray.apikey= bintray上的apikey

上传项目到Bintray

在Android Studio的终端(Terminal)中使用命令行: Win请使用:

gradlew install
gradlew bintrayUpload

Mac请使用:

./gradlew install
./gradlew bintrayUpload

如果两个命令行都出现 BUILD SUCCESS,则表示上传成功。 回到Bintray的首页点击之前创建的仓库就可以看到上传的library了

将项目发布到JCenter

点击Add to JCenter,在新页面中点击Send,之后就等待Bintrary的审核吧,如果审核通过会以邮件通知你。