สร้างโปรไฟล์พื้นฐานสำหรับไลบรารี
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หากต้องการสร้างโปรไฟล์พื้นฐานสำหรับไลบรารี ให้ใช้
ปลั๊กอิน Baseline Profile Gradle
การสร้างโปรไฟล์พื้นฐานสำหรับไลบรารีมี 3 โมดูล ดังนี้
- โมดูลแอปตัวอย่าง: มีแอปตัวอย่างที่ใช้ไลบรารีของคุณ
- โมดูลไลบรารี: โมดูลที่คุณต้องการสร้างโปรไฟล์
- โมดูลโปรไฟล์พื้นฐาน: โมดูลทดสอบที่สร้างโปรไฟล์พื้นฐาน
หากต้องการสร้างโปรไฟล์พื้นฐานสำหรับไลบรารี ให้ทำตามขั้นตอนต่อไปนี้
- สร้างโมดูล
com.android.test
ใหม่ เช่น
:baseline-profile
- กำหนดค่าไฟล์
build.gradle.kts
สำหรับ
โมดูล :baseline-profile
การกำหนดค่าคือ
ซึ่งเหมือนกับค่าสำหรับแอป แต่อย่าลืมตั้งค่า
targetProjectPath
ไปยังโมดูลแอปตัวอย่าง
- สร้างการทดสอบโปรไฟล์พื้นฐานใน
:baseline-profile
โมดูลทดสอบ โดยจำเป็นต้องเจาะจงเฉพาะแอปตัวอย่างและต้องใช้ทั้งหมด
ฟังก์ชันการทำงานของไลบรารี
- อัปเดตการกำหนดค่าในไฟล์
build.gradle.ktss
ใน
โมดูลไลบรารี เช่น :library
- ใช้ปลั๊กอิน
androidx.baselineprofile
- เพิ่มการอ้างอิง
baselineProfile
ไปยัง
โมดูล :baseline-profile
- ใช้การกำหนดค่าปลั๊กอินสำหรับผู้ใช้ทั่วไปที่ต้องการ ดังที่แสดงใน
ดังตัวอย่างต่อไปนี้
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.**"
}
}
ดึงดูด
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.**'
}
}
- เพิ่มปลั๊กอิน
androidx.baselineprofile
ลงใน
build.gradle.kts
ในโมดูลแอป
:sample-app
Kotlin
plugins {
...
id("androidx.baselineprofile")
}
ดึงดูด
plugins {
...
id 'androidx.baselineprofile'
}
- สร้างโปรไฟล์โดยเรียกใช้โค้ดต่อไปนี้
./gradlew :library:generateBaselineProfile
เมื่อสิ้นสุดงานการสร้าง โปรไฟล์พื้นฐานจะได้รับการจัดเก็บที่
library/src/main/generated/baselineProfiles
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 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`."]]