在jCenter中分发Android库以在gradle中使用 [英] distribute Android library in jCenter to use in gradle

查看:63
本文介绍了在jCenter中分发Android库以在gradle中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a library project with a module that is just for library classes and views. I've been searching over the internet how to distribute it in jCenter to use as a gradle dependency but nothing works.

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

While this isn't done yet, how can I use this module in others projects?

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

PS: I use Android Studio on Windows 10.

推荐答案

许多在线教程和指南已经过时或很难遵循.我只是自己学习了如何做,因此我希望添加对您有帮助的快速解决方案.它包括以下几点

Many of the tutorials and directions online are out of date or are very hard to follow. I just learned how to do this myself, so I am adding what will hopefully be a quick solution for you. It includes the following points

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

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

By now you probably already have a library set up. For the sake of this example I made a new project with one demo-app application module and one my-library library module in Android Studio.

这是使用Project和Android视图的外观:

Here is what it looks like using both the Project and Android views:

Bintray托管jCenter存储库. 转到Bintray并设置一个免费帐户.

Bintray hosts the jCenter repositories. Go to Bintray and set up a free account.

登录后,点击添加新存储库

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

Name the repository maven. (You can call it something else, though, if you want to group several library projects together. If you do you will also need to change the bintrayRepo name in the gradle file below.)

选择 Maven 作为存储库类型.

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

You can add a description if you want. Then click Create. That's all we need to do in Bintray for now.

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

I'm going to make this as cut-and-paste as possible, but don't forget to edit the necessary parts. You don't need to do anything with the demo app module's build.gradle file, only the gradle files for the project and the library.

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

Add the Bintray and Mavin plugins to your project build.gradle file. Here is my whole file:

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的最新版本在这里在下面的ext块中编辑所需的所有内容.

Edit everything you need to in the ext block below.

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密钥和加密密码的目的是,使其在版本控制中不会公开可见.

local.properties

The library build.gradle file above referenced some values in the local.properties file. We need to add those now. This file is located in the root of your project. It should be included in .gitignore. (If it isn't then add it.) The point of putting your username, api key, and encryption password here is so that it won't be publicly visible in version control.

## 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

关于不修改此文件的警告,但它似乎仍然可以正常工作.这是获取值的方式:

There is a warning about not modifying this file but it seems to work well anyway. Here is how you get the values:

  • bintray.user:这是您的Bintray用户名.
  • bintray.apikey:转到Bintray菜单中的编辑配置文件,然后选择 API密钥.从这里复制.
  • bintray.user: This is your Bintray username.
  • bintray.apikey: Go to Edit Profile in the Bintray menu and choose API Key. Copy it from here.

打开一个终端,然后转到项目的根文件夹.或者只是使用Android Studio中的终端.

Open a terminal and go to your project's root folder. Or just use the terminal in Android Studio.

输入以下命令

./gradlew install
./gradlew bintrayUpload

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

If everything is set up right it should upload your library to Bintray. If it fails then Google the solution. (I had to update my JDK the first time I tried.)

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

Go to your account in Bintray and you should see the library entered under your repository.

在Bintray的图书馆中,有一个添加到jCenter 按钮.

In your library in Bintray there is an Add to jCenter button.

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

Click it and send your request. If you are approved (which takes a day or two), then your library will be a part of jCenter and developers around the world can add your library to their projects simply by adding one line to the app build.gradle dependencies block.

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

恭喜!

  • You may want to add PGP encryption, especially if you are linking it to Maven Central. (jCenter has replaced Maven Central as the default in Android Studio, though.) See this tutorial for help with that. But also read this from Bintray.

您最终将想要向Bintray/jCenter库添加新版本.请参阅此答案有关如何操作的说明.

You will eventually want to add a new version to your Bintray/jCenter library. See this answer to directions on how to do it.

  • How to distribute your own Android library through jCenter and Maven Central from Android Studio
  • Creating and Publishing an Android Library

这篇关于在jCenter中分发Android库以在gradle中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆