Tạo Hồ sơ cơ sở cho một thư viện
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Để tạo Hồ sơ cơ sở cho một thư viện, hãy dùng trình bổ trợ Gradle cho Hồ sơ cơ sở.
Có 3 mô-đun liên quan đến việc tạo Hồ sơ cơ sở cho một thư viện:
- Mô-đun ứng dụng mẫu: chứa ứng dụng mẫu sử dụng thư viện của bạn.
- Mô-đun thư viện: mô-đun mà bạn muốn tạo hồ sơ.
- Mô-đun Hồ sơ cơ sở: mô-đun kiểm thử tạo Hồ sơ cơ sở.
Để tạo Hồ sơ cơ sở cho thư viện, hãy thực hiện các bước sau:
- Tạo một mô-đun
com.android.test
mới – ví dụ: :baseline-profile
.
- Định cấu hình tệp
build.gradle.kts
cho mô-đun :baseline-profile
. Cấu hình về cơ bản giống như ứng dụng, nhưng hãy nhớ đặt targetProjectPath
thành mô-đun ứng dụng mẫu.
- Tạo một bài kiểm thử Hồ sơ cơ sở trong mô-đun kiểm thử
:baseline-profile
. Đây phải là bài kiểm thử dành riêng cho ứng dụng mẫu và phải sử dụng mọi chức năng của thư viện.
- Cập nhật cấu hình trong tệp
build.gradle.ktss
của mô-đun thư viện, chẳng hạn như :library
.
- Áp dụng trình bổ trợ
androidx.baselineprofile
.
- Thêm phần phụ thuộc
baselineProfile
vào mô-đun :baseline-profile
.
- Áp dụng cấu hình trình bổ trợ đối tượng sử dụng mà bạn muốn, như trong ví dụ sau.
Kotlin
plugins {
id("com.android.library")
id("androidx.baselineprofile")
}
android { ... }
dependencies {
...
// Add a baselineProfile dependency to the `:baseline-profile` module.
baselineProfile(project(":baseline-profile"))
}
// Baseline Profile Gradle plugin configuration.
baselineProfile {
// Filters the generated profile rules.
// This example keeps the classes in the `com.library` package all its subpackages.
filter {
include "com.mylibrary.**"
}
}
Groovy
plugins {
id 'com.android.library'
id 'androidx.baselineprofile'
}
android { ... }
dependencies {
...
// Add a baselineProfile dependency to the `:baseline-profile` module.
baselineProfile ':baseline-profile'
}
// Baseline Profile Gradle plugin configuration.
baselineProfile {
// Filters the generated profile rules.
// This example keeps the classes in the `com.library` package all its subpackages.
filter {
include 'com.mylibrary.**'
}
}
- Thêm trình bổ trợ
androidx.baselineprofile
vào tệp build.gradle.kts
trong mô-đun ứng dụng :sample-app
.
Kotlin
plugins {
...
id("androidx.baselineprofile")
}
Groovy
plugins {
...
id 'androidx.baselineprofile'
}
- Tạo hồ sơ bằng cách chạy đoạn mã sau:
./gradlew :library:generateBaselineProfile
.
Khi bạn kết thúc nhiệm vụ tạo, Hồ sơ cơ sở sẽ được lưu trữ tại library/src/main/generated/baselineProfiles
.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Create Baseline Profiles for a library\n\nTo create Baseline Profiles for a library, use the\n[Baseline Profile Gradle plugin](/topic/performance/baselineprofiles/configure-baselineprofiles).\n\nThere are three modules involved in creating Baseline Profiles for a library:\n\n- Sample app module: contains the sample app that uses your library.\n- Library module: the module you want to generate the profile for.\n- Baseline Profile module: the test module that generates the Baseline Profiles.\n\nTo generate a Baseline Profile for a library, perform the following steps: \n1. Create a new `com.android.test` module---for example, `:baseline-profile`.\n2. Configure the `build.gradle.kts` file for the `:baseline-profile` module. [The configuration is\n essentially the same as for an app](/topic/performance/baselineprofiles/create-baselineprofile#create-new-profile-plugin), but make sure to set the `targetProjectPath` to the sample app module.\n3. Create a Baseline Profile test in the `:baseline-profile` test module. This needs to be specific to the sample app and must use all the functionalities of the library.\n4. Update the configuration in `build.gradle.ktss` file in the library module, say `:library`.\n 1. Apply the plugin `androidx.baselineprofile`.\n 2. Add a `baselineProfile` dependency to the `:baseline-profile` module.\n3. Apply the consumer plugin configuration you want, as shown in the following example. \n\n### Kotlin\n\n```kotlin\nplugins {\n id(\"com.android.library\")\n id(\"androidx.baselineprofile\")\n}\n\nandroid { ... }\n\ndependencies {\n ...\n // Add a baselineProfile dependency to the `:baseline-profile` module.\n baselineProfile(project(\":baseline-profile\"))\n}\n\n// Baseline Profile Gradle plugin configuration.\nbaselineProfile {\n\n // /topic/performance/baselineprofile/configure-baselineprofiles#filter-profile-rules the generated profile rules. \n // This example keeps the classes in the `com.library` package all its subpackages.\n filter {\n include \"com.mylibrary.**\"\n }\n}\n```\n\n### Groovy\n\n```groovy\nplugins {\n id 'com.android.library'\n id 'androidx.baselineprofile'\n}\n\nandroid { ... }\n\ndependencies {\n ...\n // Add a baselineProfile dependency to the `:baseline-profile` module.\n baselineProfile ':baseline-profile'\n}\n\n// Baseline Profile Gradle plugin configuration.\nbaselineProfile {\n\n // /topic/performance/baselineprofile/configure-baselineprofiles#filter-profile-rules the generated profile rules. \n // This example keeps the classes in the `com.library` package all its subpackages.\n filter {\n include 'com.mylibrary.**'\n }\n}\n```\n5. Add the `androidx.baselineprofile` plugin to the `build.gradle.kts` file in the app module `:sample-app`. \n\n ### Kotlin\n\n ```kotlin\n plugins {\n ...\n id(\"androidx.baselineprofile\")\n }\n ```\n\n ### Groovy\n\n ```groovy\n plugins {\n ...\n id 'androidx.baselineprofile'\n }\n ```\n6. Generate the profile by running the following code: `./gradlew :library:generateBaselineProfile`.\n\nAt the end of the generation task, the Baseline Profile is stored at\n`library/src/main/generated/baselineProfiles`."]]