【问题标题】:distribute Android library in jCenter to use in gradle在 jCenter 中分发 Android 库以在 gradle 中使用
【发布时间】:2016-11-07 17:54:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-gradle-plugin jcenter


    【解决方案1】:

    许多在线教程和指导已过时或很难遵循。我自己刚刚学会了如何做到这一点,所以我添加了希望对您来说是一个快速解决方案的内容。它包括以下几点

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

    您要共享的库

    现在您可能已经建立了一个库。为了这个示例,我在 Android Studio 中创建了一个新项目,其中包含一个 demo-app 应用程序模块和一个 my-library 库模块。

    下面是同时使用 Project 和 Android 视图的样子:

    设置 Bintray 帐户

    Bintray 托管 jCenter 存储库。 Go to Bintray and set up a free account.

    登录后点击添加新存储库

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

    选择 Maven 作为存储库类型。

    您可以根据需要添加说明。然后点击创建。这就是我们现在在 Bintray 中需要做的所有事情。

    编辑 gradle 文件

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

    项目构建.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
    }
    

    Bintray is hereMaven is here 的最新版本。

    库 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")
                }
            }
        }
    }
    

    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
    

    有一个关于不要修改这个文件的警告,但它似乎运行良好。以下是获取值的方法:

    • bintray.user:这是你的 Bintray 用户名。
    • bintray.apikey:进入 Bintray 菜单中的 Edit Profile 并选择 API Key。从这里复制。

    上传项目到 Bintray

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

    输入以下命令

    ./gradlew install
    ./gradlew bintrayUpload
    

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

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

    链接到 jCenter

    在您的 Bintray 库中,有一个 添加到 jCenter 按钮。

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

    dependencies {
        compile 'com.example:my-library:1.0.0'
    }
    

    恭喜!

    注意事项

    • 您可能想要添加 PGP 加密,尤其是当您将其链接到 Maven Central 时。 (不过,jCenter 已将 Maven Central 替换为 Android Studio 中的默认设置。)请参阅 this tutorial 以获取帮助。还有来自 Bintray 的 read this

    如何添加新版本

    您最终会希望将新版本添加到您的 Bintray/jCenter 库中。请参阅this answer 以了解如何操作。

    进一步阅读

    【讨论】:

    • 我在好几个地方都看到过同样的代码,但有些地方我有点困惑,也许你可以回答他们: 1. 为什么会有一个叫sourcesJar的任务?听起来 jar 将被上传到 Bintray 而不是 aar,这听起来令人困惑。 2、“maven 部分”如何连接到“bintray 部分”?我找不到它们之间的任何联系,例如,POM 是否上传到 Bintray?如果是这样,Bintray 任务如何找到它?
    • @Franco,我需要对此进行更多研究才能回答。
    • 非常感谢,我厌倦了尝试很多 tuts,终于你让我开心了
    • @Simon,是的,第一次设置非常痛苦。不过,在这之后,它并没有那么糟糕。当您需要更新库时,请查看 my other answer。这是我每次都遵循的过程。
    • @Suragch 为什么需要这条线? group = publishedGroupId你没有在任何其他地方使用group
    猜你喜欢
    • 2015-12-22
    • 2021-05-08
    • 2015-10-07
    • 2017-11-14
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多