[[["容易理解","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-08-27 (世界標準時間)。"],[],[],null,["This page describes the properties and options needed to prepare your\n[Android library](/studio/projects/android-library) project for publication\nusing the [Android Gradle plugin (AGP)](/studio/releases/gradle-plugin).\nEven if you set some of these properties at the outset of creating your\nlibrary, review the following guidance to optimize your\nsettings.\n\nChoose a namespace\n\nAndroid libraries need to declare a namespace so that they can generate a unique\n`R` class when their resources are compiled. This namespace should closely match\nthe library's root class package to avoid confusion when users import regular\nclasses from the library and its `R` class.\n\nStarting with AGP 7.0, you can set the\n[namespace](/reference/tools/gradle-api/7.0/com/android/build/api/dsl/CommonExtension#namespace:kotlin.String)\nin the app's `build.gradle` file, as shown in the following code example: \n\nGroovy \n\n```groovy\nandroid {\n namespace = 'com.example.library'\n}\n```\n\nKotlin \n\n```kotlin\nandroid {\n namespace = \"com.example.library\"\n}\n```\n\nThe namespace is a developer-facing property of the library. It is not\nrelated to the application identity, which is set using the\n[`applicationId`](/studio/build/configure-app-module#set_the_application_id)\nproperty.\n\nIn previous versions of AGP, both the `applicationId` property (for an\napp) and the `namespace` property (for a library) could be set using the\nmanifest's [`package`](/guide/topics/manifest/manifest-element#package)\nattribute, which led to confusion.\n| **Note:** A library project does not have an `applicationId`; that is a property of applications only.\n\nChoose a `minSdkVersion` value\n\nChoosing a [`minSdkVersion`](/guide/topics/manifest/uses-sdk-element#min) for\nyour library is an important aspect of publishing your library. The\n`minSdkVersion` should reflect the minimum version of Android that your code can\nsupport.\n\nBe aware of the following considerations when choosing a `minSdkVersion`:\n\n- **Choosing a low `minSdkVersion` generally allows for wider distribution of\n your library.**\n\n A library's code is generally not executed unless the app\n calls it explicitly. An app can still run on a version of Android that\n is lower than required by a library dependency---if the library is not\n essential to core app functionality---by doing runtime checks before calling\n the library. Therefore, set your library's `minSdkVersion` low enough that\n it can be embedded in apps, and called when possible, to help reach more\n users.\n- **Choosing a high `minSdkVersion` might prevent applications from including\n the library.**\n\n The manifest merger, which is a step in AGP that merges manifest files\n from the app and from its dependencies, enforces that no\n dependencies have a higher `minSdkVersion` than the app.\n- **Choosing a high `minSdkVersion` might prompt app developers to disable\n manifest merger safety checks, causing issues later in the build process.**\n\n Because the manifest merger prevents app projects from including\n libraries with a higher `minSdkVersion` than the app itself, app developers\n might disable the safety checks of the manifest merger to minimize build\n errors. However, this risks true incompatibility issues downstream.\n- **Choosing a high `minSdkVersion` might be necessary in special cases where\n a library's manifest includes a broadcast receiver or some other mechanism by\n which its code is triggered automatically.**\n\n In these cases, choosing a high `minSdkVersion` ensures that code can run.\n Alternatively, you can disable the automated behavior so that the app can opt\n in to executing the library after doing the right checks.\n\nTo allow embedding in apps, use the\n[`RequiresApi`](/reference/androidx/annotation/RequiresApi) annotation in your\nlibrary to indicate to its callers that they need to do runtime checks. Android\nLint uses the `RequiresApi` information for its inspections. For more resources\non using annotations to improve your API code and APIs, see [Improve code\ninspection with annotations](/studio/write/annotations).\n\nSet up AAR metadata\n\nAn Android library is packaged in the form\nof an Android Archive (AAR) file. AAR metadata consists of properties that help\nAGP consume libraries. If your library is consumed by an incompatible\nconfiguration, and AAR metadata is set up, users are presented with an error\nmessage to help them resolve the issue.\n| **Note:** AAR metadata does not contain properties that are important at runtime and therefore is not typically found in compiled Android apps.\n\nChoose a minCompileSdk value\n\nStarting with version 4.1, AGP supports\n[`minCompileSdk`](/reference/tools/gradle-api/7.1/com/android/build/api/variant/AarMetadata#mincompilesdk).\nThis indicates the minimum\n[`compileSdk`](/reference/tools/gradle-api/7.1/com/android/build/api/dsl/CommonExtension#compileSdk:kotlin.Int)\nthat consuming projects can use. If your library contains manifest entries or\nresources that make use of newer platform attributes, you need to\nset this value.\n\nThe `minCompileSdk` value can be set in the `defaultConfig{}`,\n`productFlavors{}`, and `buildTypes{}` blocks in the module-level `build.gradle`\nfile: \n\nGroovy \n\n```groovy\nandroid {\n defaultConfig {\n aarMetadata {\n minCompileSdk = 29\n }\n }\n productFlavors {\n foo {\n ...\n aarMetadata {\n minCompileSdk = 30\n }\n }\n }\n}\n```\n\nKotlin \n\n```kotlin\nandroid {\n defaultConfig {\n aarMetadata {\n minCompileSdk = 29\n }\n }\n productFlavors {\n register(\"foo\") {\n ...\n aarMetadata {\n minCompileSdk = 30\n }\n }\n }\n}\n```\n\nIf you set `minCompileSdk` in multiple places, Gradle prioritizes the settings\nlocations as follows during the build process:\n\n1. `buildTypes{}`\n\n2. `productFlavors{}`\n\n3. `defaultConfig{}`\n\nIn the preceding example, where `minCompileSdk` is defined in both\n`defaultConfig{}` and `productFlavors{}`, `productFlavors{}` is prioritized\nand `minCompileSdk` is set to 30.\n\nTo learn more about how Gradle\nprioritizes settings when combining code and resources, see [Build with source\nsets](/studio/build/build-variants#sourceset-build).\n\nEnable test fixtures\n\n[Test fixtures](/reference/tools/gradle-api/8.12/com/android/build/api/dsl/TestFixtures)\nare commonly used to set up the code being tested or facilitate the tests of a\ncomponent. Starting with version 7.1, AGP can create test fixtures for library\nprojects in addition to application and dynamic-feature projects.\n\nWhen publishing a library for others to consume, consider creating test\nfixtures for your API. Test fixtures can be turned on in the module-level\n`build.gradle` file: \n\nGroovy \n\n```groovy\nandroid {\n testFixtures {\n enable = true\n }\n}\n```\n\nKotlin \n\n```kotlin\nandroid {\n testFixtures {\n enable = true\n }\n}\n```\n\nWhen you turn on test fixtures, Gradle automatically creates a\n`src/testFixtures` source set where you can write test fixtures.\n\nFor more information, refer to Gradle's documentation about [using\ntest fixtures](https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures)."]]