上傳程式庫
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您必須先選擇存放區,才能授予程式庫存取權。
本頁內容會說明選擇 VM 連線的考量事項
存放區類型,以及如何使用
Maven Publish 外掛程式。
上傳媒體庫前,請務必備妥您的媒體庫
發布任何必要版本
發布項目變數或
測試韌體。
選擇存放區類型
程式庫以 AAR 檔案的形式發布。這些檔案包含已編譯的程式碼
位元碼和原生資料庫、Android 資訊清單和資源。包裹
本身未宣告其他任何身分、版本或依附元件
程式庫
一般而言,透過存放區提供 AAR 是最佳做法
比起直接發布 AAR這項服務
這樣使用者就能更瞭解程式庫的來源
不必處理不含重要細節的 name.aar
檔案
就像版本一樣如要升級至新版程式庫,請使用
確保僅有新版本所需的依附元件
,這樣使用者就不必手動更新依附元件。
使用存放區發布程式庫有多種好處,包括:
- Gradle 可自動將程式庫的依附元件加入
依附元件
圖表。
- Gradle 可確保程式庫的單一版本在依附元件圖表中,假如系統連帶納入程式庫的多個版本,就能解決衝突問題。
- 如果您的 Android Gradle 外掛程式 (AGP) 應用程式
程式庫使用 Java 8 以上版本的語言功能,縮短了
使用者。
- 程式庫可使用變數發布及納入測試韌體等功能。
直接發布 AAR 並不會為使用者提供任何資訊
您程式庫的身分、版本或依附元件。時間
發布至存放區時,系統會透過
存放區機制的一部分在 Maven 存放區中
POM 檔案。因此,我們強烈建議您使用存放區發布程式庫,而不是自行發布 AAR 檔案。
存放區類型
存放區可分為以下三種類型:
- 免費線上存放區 (例如 Maven Central):可讓任何人
下載程式庫
- 私人存放區 (透過登入存取):可控管如何發布私人程式庫。
- 本機資料夾存放區:可透過讓使用者自行下載的方式發布程式庫。
使用本機資料夾存放區與為使用者提供應用程式的做法很相似
並附上手動下載自動套用最佳化建議的連結,或透過電子郵件傳送自動套用最佳化建議功能。主要
在於您傳送的不僅僅是「自動套用最佳化建議」功能
身分、版本和依附元件的相關資訊
您發布的是包含 AAR 的資料夾存放區 ZIP 檔案
還有中繼資料這樣一來,使用者就能擷取檔案內容,將
把內容指向自己的專案,並將 Gradle 指向該專案。此後,使用者可以
使用 Maven 座標宣告程式庫的依附元件,就像程式庫一樣
這個目錄中列出的所有優勢
建立發布項目
使用 Gradle Maven Publish 外掛程式進行發布。Maven Publish 外掛程式可讓您宣告出版品
以及建立工作,將這些出版品發布到
與存放區這些發布項目使用的 SoftwareComponent
例項如下:
驅動建構作業的外掛程式,可以是 AGP 或
java-library
外掛程式。
請注意,搭配 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
,其中包含
出版品名稱和存放區名稱執行這項工作
並對指定位置產生存放區在這個範例中,
存放區會在建構作業中產生
資料夾。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
。輸出檔案位於 build/distributions
底下。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間: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`."]]