在jCenter中分发Android库以在gradle中使用

Rod*_*zke 3 android android-gradle-plugin jcenter

我有一个库项目,其中包含一个仅用于库类和视图的模块.我一直在互联网上搜索如何在jCenter中分发它作为一个gradle依赖,但没有任何作用.

虽然还没有完成,但我如何在其他项目中使用此模块?

PS:我在Windows 10上使用Android Studio.

Sur*_*gch 11

许多在线教程和指示都已过时或很难遵循.我刚刚学会了如何做到这一点,所以我希望能为你提供一个快速解决方案.它包括以下几点

  • 从您的Android库开始
  • 设置Bintray帐户
  • 编辑项目的gradle文件
  • 将您的项目上传到Bintray
  • 将其链接到jCenter

您要共享的库

到目前为止,您可能已经设置了一个库.为了这个例子,我在Android Studio中创建了一个带有一个demo-app应用程序模块和一个my-library库模块的新项目.

以下是使用Project和Android视图的样子:

在此输入图像描述

设置Bintray帐户

Bintray托管jCenter存储库.去Bintray并建立一个免费帐户.

登录后,单击" 添加新存储库"

在此输入图像描述

命名存储库maven.(但是,如果要将多个库项目组合在一起,可以将其称为其他内容.如果这样,您还需要bintrayRepo在下面的gradle文件中更改名称.)

选择Maven作为存储库类型.

在此输入图像描述

您可以根据需要添加说明.然后单击Create.这就是我们现在在Bintray所需要做的一切.

编辑gradle文件

我将尽可能剪切和粘贴,但不要忘记编辑必要的部分.您无需对演示应用程序模块的build.gradle文件执行任何操作,只需对项目和库的gradle文件执行任何操作.

项目build.gradle

将Bintray和Mavin插件添加到项目build.gradle文件中.这是我的整个文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'

        // Add these lines (update them to whatever the newest version is)
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

Bintray的最新版本就在这里,Maven就在这里.

库build.gradle

ext下面的块中编辑您需要的所有内容.

apply plugin: 'com.android.library'

// change all of these as necessary
ext {
    bintrayRepo = 'maven'  // this is the same as whatever you called your repository in Bintray
    bintrayName = 'my-library' // your bintray package name. I am calling it the same as my library name.

    publishedGroupId = 'com.example'
    libraryName = 'my-library'
    artifact = 'my-library' // I'm calling it the same as my library name

    libraryDescription = 'An example library to make your programming life easy'

    siteUrl = 'https://github.com/example/my-library'
    gitUrl = 'https://github.com/example/my-library.git'

    libraryVersion = '1.0.0'

    developerId = 'myID' // Maven plugin uses this. I don't know if it needs to be anything special.
    developerName = 'My Name'
    developerEmail = 'myemail@example.com'

    licenseName = 'The MIT License (MIT)'
    licenseUrl = 'https://opensource.org/licenses/MIT'
    allLicenses = ["MIT"]
}

// This next section is your normal gradle settings
// There is nothing special that you need to change here
// related to Bintray. Keep scrolling down.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

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

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

// Maven section
// You shouldn't need to change anything. It just uses the
// values you set above.

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

                }
            }
        }
    }
}

// Bintray section
// As long as you add bintray.user and bintray.apikey to the local.properties
// file, you shouldn't have to change anything here. The reason you 
// don't just write them here is so that they won't be publicly visible
// in GitHub or wherever your source control is.

apply plugin: 'com.jfrog.bintray'

version = libraryVersion

if (project.hasProperty("android")) { // Android libraries
    task sourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }

    task javadoc(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
} else { // Java libraries
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }
}

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 = bintrayRepo
        name = bintrayName
        desc = libraryDescription
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = allLicenses
        publish = true
        publicDownloadNumbers = true
        version {
            desc = libraryDescription
            gpg {
                // optional GPG encryption. Default is false.
                sign = false
                //passphrase = properties.getProperty("bintray.gpg.password")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

local.properties

build.gradle上面的库文件引用了local.properties文件中的一些值.我们现在需要添加它们.该文件位于项目的根目录中.它应该包括在内.gitignore.(如果不是,那么添加它.)在这里放置你的用户名,api密钥和加密密码是为了使它在版本控制中不会公开显示.

## 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=/home/yonghu/Android/Sdk

# Add these lines (but change the values according to your situation)
bintray.user=myusername
bintray.apikey=1f2598794a54553ba68859bb0bf4c31ff6e71746
Run Code Online (Sandbox Code Playgroud)

有关于不修改此文件的警告,但无论如何它似乎都运行良好.以下是获取值的方法:

  • bintray.user:这是你的Bintray用户名.
  • bintray.apikey:转到Bintray菜单中的编辑配置文件,然后选择API密钥.从这里复制它.

在此输入图像描述

上传项目到Bintray

打开终端并转到项目的根文件夹.或者只是在Android Studio中使用终端.

在此输入图像描述

输入以下命令

./gradlew install
./gradlew bintrayUpload
Run Code Online (Sandbox Code Playgroud)

如果一切都设置正确,它应该将您的库上传到Bintray.如果它失败则谷歌解决方案.(我第一次尝试时不得不更新我的JDK.)

转到Bintray的帐户,您应该看到在您的存储库下输入的库.

链接到jCenter

在Bintray的库中有一个Add to jCenter按钮.

在此输入图像描述

点击它并发送您的请求.如果您获得批准(需要一两天),那么您的库将成为jCenter的一部分,世界各地的开发人员只需向应用程序build.gradle依赖项块添加一行即可将库添加到项目中.

dependencies {
    compile 'com.example:my-library:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

恭喜!

笔记

  • 您可能希望添加PGP加密,尤其是在将其链接到Maven Central时.(尽管如此,jCenter已将Maven Central替换为Android Studio的默认设置.)请参阅本教程以获取帮助.但也从Bintray 读到这个.

如何添加新版本

您最终希望将新版本添加到Bintray/jCenter库中.有关如何操作的说明,请参阅此答案.

进一步阅读