上传库
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需授予对您的库的访问权限,您需要选择代码库。
本页面将为您介绍与选择代码库类型相关的注意事项,以及如何使用 Maven Publish 插件创建发布内容。
在上传库之前,请确保您已准备好要发布的库,并已配置好必要的所有发布内容变体或测试夹具。
选择代码库类型
库将以 AAR 文件的形式发布。这些文件包含经过编译的代码(作为字节码和原生库)、Android 清单和资源。软件包本身不会声明任何身份、版本或对其他库的依赖关系。
通常,通过代码库提供 AAR 是最佳做法,而不是直接分发 AAR。它可以帮助用户更好地了解库的来源,而不会迫使用户去处理缺少版本等重要详情的 name.aar
文件。升级到更高版本的库时,使用代码库有助于确保只添加较新版本所需的依赖项,这样用户就不必手动更新依赖项了。
使用代码库发布库具有多项优势:
- Gradle 可以自动将库的依赖项添加到依赖关系图。
- Gradle 可以确保依赖关系图中包含库的单个版本;如果您的库不断变化并有多个不同版本包含在关系图中,这样可以解决冲突。
- Android Gradle 插件 (AGP) 可以更高效地进行脱糖(如果您的库使用 Java 8 或更高版本的语言功能),从而缩短用户的构建时间。
- 您的库可以使用变体发布,并且可以包含测试夹具等功能。
直接分发 AAR 不会向用户提供与库的身份、版本或依赖项相关的任何信息。发布到代码库时,分发操作将由代码库机制中的一个单独文件来处理。对于 Maven 代码库,则是由 POM 文件处理。因此,强烈建议您使用代码库发布库,而不要手动分发 AAR 文件。
代码库类型
代码库有 3 种类型:
- 借助免费的在线代码库(例如 Maven Central),任何人都可以上传和下载库。
- 借助私有代码库(可通过登录进行访问),您可以对私有库进行受控分发。
- 借助基于文件夹的本地代码库,您可以通过手动下载来分发库。
使用基于文件夹的本地代码库与为用户提供指向 AAR 的手动下载链接或通过电子邮件发送 AAR 非常相似。主要区别在于:您发送的内容不仅包含 AAR,还包含有关身份、版本和依赖项的额外信息。
您可以分发基于文件夹的代码库的 ZIP 文件,其中包含您的 AAR 和相关元数据。然后,用户可以提取文件的内容,将内容添加到项目,并将 Gradle 指向该项目。之后,用户即可使用 Maven 坐标声明对相应库的依赖关系(就像该库位于在线代码库中一样),并享受前面提及的所有优势。
创建发布内容
使用 Gradle Maven Publish 插件发布。借助 Maven Publish 插件,您可以声明发布内容和代码库,并创建任务以将这些发布内容发布到代码库。这些发布内容会使用由驱动 build 的插件(可能是 AGP 或 java-library
插件)创建的 SoftwareComponent
实例。
请注意,使用 AGP 运行 Maven Publish 插件时,系统不会在应用此插件时创建软件组件,它们是
创建于
afterEvaluate()
回调步骤。因此,选择相应软件组件的发布内容也必须在执行 afterEvaluate()
步骤期间进行配置。
模块级 build.gradle
文件的以下代码段会为使用 singleVariant()
或 multipleVariants()
创建的给定变体创建发布内容。
Groovy
publishing {
publications {
release(MavenPublication) {
groupId = 'com.my-company'
artifactId = 'my-library'
version = '1.0'
afterEvaluate {
from components.release
}
}
}
}
Kotlin
publishing {
publications {
register<MavenPublication>("release") {
groupId = "com.my-company"
artifactId = "my-library"
version = "1.0"
afterEvaluate {
from(components["release"])
}
}
}
}
在上述示例中,组件的名称 (components.release
) 基于为 singleVariant()
或 multipleVariants()
指定的名称。
声明发布内容后,您必须创建目标代码库。
发布到本地代码库
发布到本地代码库与发布到远程代码库非常相似,不同之处在于代码库声明。请参阅上一部分,了解如何发布到远程代码库,创建一个能够发布所需变体的发布内容。然后,创建本地代码库:
Groovy
publishing {
publications {
release(MavenPublication) {
...
}
}
repositories {
maven {
name = 'myrepo'
url = layout.buildDirectory.dir("repo")
}
}
}
Kotlin
publishing {
publications {
register<MavenPublication>("release") {
...
}
}
repositories {
maven {
name = "myrepo"
url = uri(layout.buildDirectory.dir("repo"))
}
}
}
这会创建一个名为 publishReleaseToMyRepoRepository
的任务(由发布内容名称和代码库名称组成)。运行此任务会将代码库生成到所提供的位置。在此示例中,代码库是在项目的 build 文件夹中的 repo
目录下生成。
如果您希望自动生成此代码库的 ZIP 文件,可以使用以下代码执行此操作:
Groovy
tasks.register('generateRepo', Zip) {
def publishTask = tasks.named('publishReleasePublicationToMyrepoRepository')
from publishTask.map { it.getRepository().getUrl() }
into 'mylibrary'
archiveFileName.set('mylibrary.zip')
}
Kotlin
tasks.register<Zip>("generateRepo") {
val publishTask = tasks.named(
"publishReleasePublicationToMyrepoRepository",
PublishToMavenRepository::class.java)
from(publishTask.map { it.repository.url })
into("mylibrary")
archiveFileName.set("mylibrary.zip")
}
上述代码会创建一个名为 generateRepo
的 Zip
任务,该任务会使用并压缩发布任务的内容,同时确保名为 mylibrary
的顶级文件夹中存在相关 ZIP 条目。输出位于 build/distributions
下。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Upload your library\n\nTo grant access to your library, you need to choose a repository.\nThis page guides you through the considerations related to choosing a\nrepository type and shows how to create a publication using the\n[Maven Publish Plugin](https://docs.gradle.org/current/userguide/publishing_maven.html).\n\nBefore uploading your library, make sure you have [prepared your library for\nrelease](/studio/publish-library/prep-lib-release) and configured any necessary\n[publication variants](/studio/publish-library/configure-pub-variants) or\n[test fixtures](/studio/publish-library/configure-test-fixtures).\n\nChoose a repository type\n------------------------\n\nLibraries are published as AAR files. These files contain compiled code as\nbytecode and native libraries, an Android manifest, and resources. The package\nitself does not declare any identity, version, or dependencies on other\nlibraries.\n\nProviding AARs through a repository is generally the best practice, rather\nthan distributing the AAR directly. It\nhelps users have a better understanding of where the library is coming from\nrather than having to deal with a `name.aar` file without important details,\nlike version. When upgrading to a newer version of a library, use a\nrepository to ensure that only the required dependencies of the newer version\nare added, so that users don't have to manually update dependencies.\n\nThere are multiple benefits to using a repository to publish your library:\n\n- Gradle can automatically add your library's dependencies to the [dependency\n graph](https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html).\n- Gradle can ensure that a single version of your library is in the dependency graph, resolving conflicts if your library is transitively included more than once with different versions.\n- The Android Gradle Plugin (AGP) can do more efficient desugaring if your library uses Java 8 or higher language features, reducing build times for your users.\n- Your library can use variant publishing and include features like test fixtures.\n\nDistributing the AAR directly doesn't provide your user with any information\nregarding the identity, version, or dependencies of your library. When\npublishing to a repository, distribution is handled by a separate file that is\npart of the repository mechanism. For Maven repositories, this is the\n[POM file](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html). Therefore, it is strongly recommended to publish libraries using\nrepositories rather than manually distributing AAR files.\n\n### Types of repositories\n\nThere are three types of repositories:\n\n- Free online repositories, like Maven Central, let anyone upload and download libraries.\n- Private repositories, with access via login, allow controlled distribution of private libraries.\n- Local, folder-based repositories allow distribution of libraries through manual download.\n\nUsing local, folder-based repositories is very similar to providing your users\nwith a manual download link to the AAR or sending the AAR by email. The main\ndifference is that you are not sending just the AAR but also the additional\ninformation about identity, version, and dependencies.\n\nYou distribute a zip file of the folder-based repository containing your AAR\nplus the metadata. Your users can then extract the file's contents, add the\ncontents to their project, and point Gradle to it. From then on, the users can\ndeclare a dependency on the library using Maven coordinates, as if the library\nwere in an online repository, and benefit from all the advantages mentioned\nearlier.\n\nCreate the publication\n----------------------\n\nPublish using the [Gradle Maven Publish Plugin](https://docs.gradle.org/current/userguide/publishing_maven.html). The Maven Publish Plugin lets you declare publications and\nrepositories and creates tasks to publish these publications to the\nrepositories. These publications consume a `SoftwareComponent` instance that\nthe plugin that drives the build creates, which could be AGP or the\n`java-library` plugin.\n\nNote that when running the Maven Publish Plugin with AGP, the software\ncomponents are not created directly when the plugin is applied. They are instead\ncreated during the\n[`afterEvaluate()`](https://docs.gradle.org/current/userguide/build_lifecycle.html)\ncallback step. Therefore, the publication that selects the software component\nmust also be configured during the `afterEvaluate()` step.\n\nThe following code snippet of the module-level `build.gradle` file creates a\npublication for a given variant created with `singleVariant()` or\n`multipleVariants()`: \n\n### Groovy\n\n```groovy\npublishing {\n publications {\n release(MavenPublication) {\n groupId = 'com.my-company'\n artifactId = 'my-library'\n version = '1.0'\n\n afterEvaluate {\n from components.release\n }\n }\n }\n}\n```\n\n### Kotlin\n\n```kotlin\npublishing {\n publications {\n register\u003cMavenPublication\u003e(\"release\") {\n groupId = \"com.my-company\"\n artifactId = \"my-library\"\n version = \"1.0\"\n\n afterEvaluate {\n from(components[\"release\"])\n }\n }\n }\n}\n```\n\nIn the preceding example, the name of the component (`components.release`) is\nbased on the name that was given to either `singleVariant()`\nor `multipleVariants()`.\n\nOnce you declare a publication, you must create a target repository.\n\n### Publish to a local repository\n\nPublishing to a local repository is very similar to publishing to a remote\nrepository, except for the repository declaration. Read the preceding section to\nlearn how to [publish to a remote repository](#create-pub) to create a\npublication that publishes the desired variant or variants. Then create a local\nrepository: \n\n### Groovy\n\n```groovy\npublishing {\n publications {\n release(MavenPublication) {\n ...\n }\n }\n repositories {\n maven {\n name = 'myrepo'\n url = layout.buildDirectory.dir(\"repo\")\n }\n }\n}\n```\n\n### Kotlin\n\n```kotlin\npublishing {\n publications {\n register\u003cMavenPublication\u003e(\"release\") {\n ...\n }\n }\n repositories {\n maven {\n name = \"myrepo\"\n url = uri(layout.buildDirectory.dir(\"repo\"))\n }\n }\n}\n```\n\nThis creates a task called\n`publish`**Release** `To`**MyRepo**`Repository` that consists of\nthe name of the publication and the name of the repository. Run this task\nto generate the repository to the location provided. In this example, the\nrepository is generated inside the build\nfolder of the project, under a `repo` directory.\n\nIf you want to automatically generate a zip file of the repository, do\nso using the following code: \n\n### Groovy\n\n```groovy\ntasks.register('generateRepo', Zip) {\n def publishTask = tasks.named('publishReleasePublicationToMyrepoRepository')\n from publishTask.map { it.getRepository().getUrl() }\n into 'mylibrary'\n archiveFileName.set('mylibrary.zip')\n}\n```\n\n### Kotlin\n\n```kotlin\ntasks.register\u003cZip\u003e(\"generateRepo\") {\n val publishTask = tasks.named(\n \"publishReleasePublicationToMyrepoRepository\",\n PublishToMavenRepository::class.java)\n from(publishTask.map { it.repository.url })\n into(\"mylibrary\")\n archiveFileName.set(\"mylibrary.zip\")\n}\n```\n\nThis code creates a `Zip` task called `generateRepo` that consumes the content\nof the publishing task and compresses it while ensuring that the zip entries are in a\ntop-level folder called `mylibrary`. The output is located under\n`build/distributions`."]]