ปลั๊กอิน Gradle ของ Baseline Profile ช่วยให้สร้างและบำรุงรักษา Baseline Profile ได้ง่ายขึ้น ซึ่งจะช่วยให้คุณ ทํางานต่อไปนี้ได้
- สร้างโปรไฟล์พื้นฐานใหม่สำหรับแอป
- สร้างโปรไฟล์พื้นฐานใหม่สำหรับคลัง
- ปรับแต่งการสร้างโปรไฟล์พื้นฐาน
หน้านี้อธิบายวิธีใช้ปลั๊กอิน Gradle ของ Baseline Profile เพื่อปรับแต่ง การสร้าง Baseline Profile
ข้อกำหนดของปลั๊กอิน
- AGP 8.0 ขึ้นไป
- ขึ้นอยู่กับปลั๊กอิน Gradle เวอร์ชันล่าสุด
ใช้อุปกรณ์ที่มีการจัดการจาก Gradle เพื่อสร้าง Baseline Profile
หากต้องการใช้อุปกรณ์ที่ Gradle จัดการ (GMD) เพื่อสร้าง Baseline Profile ให้เพิ่มอุปกรณ์ในbuild.gradle.kts
การกำหนดค่า
ของโมดูลโปรดิวเซอร์โปรไฟล์ ซึ่งน่าจะเป็นโมดูลทดสอบ :baselineprofile
ดังที่แสดงในตัวอย่างต่อไปนี้
Kotlin
android { testOptions.managedDevices.devices { create<com.android.build.api.dsl.ManagedVirtualDevice>("pixel6Api31") { device = "Pixel 6" apiLevel = 31 systemImageSource = "aosp" } } }
Groovy
android { testOptions.managedDevices.devices { pixel6Api31(ManagedVirtualDevice) { device 'Pixel 6' apiLevel = 31 systemImageSource 'aosp' } } }
ใช้ GMD เพื่อสร้าง Baseline Profile โดยเพิ่มลงในการกำหนดค่าปลั๊กอิน Gradle ของ Baseline Profile
ดังนี้ใน build.gradle.kts
ของ
:baselineprofile
โมดูลทดสอบ
Kotlin
baselineProfile { managedDevices += "pixel6Api31" }
Groovy
baselineProfile { managedDevices = ['pixel6Api31'] }
เมื่อใช้ GMD เพื่อสร้างโปรไฟล์พื้นฐาน ให้ตั้งค่า useConnectedDevices
เป็น
false
ในโมดูลการทดสอบ :baselineprofile
ดังนี้
Kotlin
baselineProfile { ... useConnectedDevices = false }
Groovy
baselineProfile { ... useConnectedDevices false }
สร้างโปรไฟล์พื้นฐานสำหรับตัวแปรต่างๆ
คุณสร้าง Baseline Profile ต่อตัวแปร ต่อรสชาติ หรือเป็นไฟล์เดียว
เพื่อใช้กับตัวแปรทั้งหมดได้ ควบคุมลักษณะการทำงานนี้ผ่านการตั้งค่าการผสานตามที่แสดงในตัวอย่างต่อไปนี้ใน build.gradle.kts
ของโมดูลแอปพลิเคชันหรือไลบรารี
Kotlin
baselineProfile { mergeIntoMain = true }
Groovy
baselineProfile { mergeIntoMain true }
หากต้องการผสานรวมโปรไฟล์ที่สร้างขึ้นสำหรับตัวแปรทั้งหมดเป็นโปรไฟล์เดียว ให้ตั้งค่า
mergeIntoMain
เป็น true
คุณจะสร้าง Baseline
Profiles ต่อตัวแปรไม่ได้เมื่อการตั้งค่านี้เป็น true
ดังนั้นจึงมี Gradle Task รายการเดียวที่ชื่อ generateBaselineProfile
ระบบจะแสดงโปรไฟล์ที่
src/main/generated/baselineProfiles
หากต้องการปิดใช้การผสานและมีโปรไฟล์ 1 รายการต่อตัวแปร ให้ตั้งค่า mergeIntoMain
เป็น
false
ในกรณีนี้ จะมีงาน Gradle หลายรายการที่เฉพาะเจาะจงกับตัวแปร ตัวอย่างเช่น เมื่อมี 2 รสชาติ เช่น ฟรีและแบบชำระเงิน และประเภทบิลด์รุ่น 1 รายการ งานจะเป็นดังนี้
* `generateFreeReleaseBaselineProfile`
* `generatePaidReleaseBaselineProfile`
* `generateReleaseBaselineProfile`
หากต้องการระบุลักษณะการผสานต่อรูปแบบ ให้ใช้โค้ดต่อไปนี้
Kotlin
baselineProfile { variants { freeRelease { mergeIntoMain = true } } }
Groovy
baselineProfile { variants { freeRelease { mergeIntoMain true } } }
ในตัวอย่างก่อนหน้า ตัวแปรที่มีการตั้งค่าสถานะเป็น true
จะผสานรวมเป็น src/main/generated/baselineProfiles
ทั้งหมด ส่วนโปรไฟล์ของตัวแปรที่มีการตั้งค่าสถานะเป็น false
จะยังคงอยู่ในโฟลเดอร์ src/<variant>/generated/baselineProfiles
โดยค่าเริ่มต้น ระบบจะตั้งค่า mergeIntoMain
เป็น true
สำหรับไลบรารี และ false
สำหรับแอป
สร้าง Baseline Profile โดยอัตโนมัติเมื่อประกอบรุ่นใหม่
คุณสามารถกำหนดค่าโปรไฟล์พื้นฐานให้สร้างโดยอัตโนมัติในทุกรุ่น
บิลด์ แทนการใช้ทาสก์ generateBaselineProfile
ด้วยตนเอง เมื่อใช้
การสร้างอัตโนมัติ โปรไฟล์ที่อัปเดตล่าสุดจะรวมอยู่ในบิลด์ที่เผยแพร่
หากต้องการเปิดใช้การสร้างอัตโนมัติสำหรับบิลด์รุ่นที่เผยแพร่ ให้ใช้แฟล็ก
automaticGenerationDuringBuild
Kotlin
baselineProfile { automaticGenerationDuringBuild = true }
Groovy
baselineProfile { automaticGenerationDuringBuild true }
การตั้งค่าแฟล็ก automaticGenerationDuringBuild
เป็น true
จะทําให้เกิด
การสร้าง Baseline Profile ใหม่สําหรับการประกอบแต่ละรุ่น ซึ่งหมายความว่า
การเรียกใช้ทาการสร้างรุ่นที่รวบรวม เช่น ./gradlew:app:assembleRelease
จะทริกเกอร์ :app:generateReleaseBaselineProfile
ด้วย ซึ่งจะเริ่มการทดสอบเครื่องมือ Baseline Profile
และสร้างบิลด์ Baseline Profile ที่ใช้ในการทดสอบ
แม้ว่าการสร้างอัตโนมัติจะช่วยให้ผู้ใช้ได้รับประโยชน์ด้านประสิทธิภาพสูงสุด แต่ก็ยังเพิ่มเวลาในการสร้างเนื่องจากการสร้างและการทดสอบเครื่องมือ 2 ครั้ง
นอกจากนี้ คุณยังระบุลักษณะการทำงานนี้ต่อตัวแปรได้ด้วย ดังตัวอย่างต่อไปนี้
Kotlin
baselineProfile { variants { freeRelease { automaticGenerationDuringBuild = true } } }
Groovy
baselineProfile { variants { freeRelease { automaticGenerationDuringBuild true } } }
ในตัวอย่างก่อนหน้า งาน generateFreeReleaseBaselineProfile
จะทำงาน
เมื่อเริ่ม assembleFreeRelease
ซึ่งจะเป็นประโยชน์ในกรณีที่ผู้ใช้ต้องการมี เช่น release
สำหรับบิลด์การเผยแพร่ที่สร้างโปรไฟล์เสมอเมื่อสร้าง และบิลด์ releaseWithoutProfile
สำหรับการทดสอบภายใน
คุณยังปิดใช้การสร้างจากบรรทัดคำสั่งได้ดังนี้แทนการเพิ่มรูปแบบใหม่ที่ไม่มี Baseline Profile
./gradlew assembleRelease -Pandroid.baselineProfile.automaticGenerationDuringBuild=false
จัดเก็บโปรไฟล์พื้นฐานของร้านค้าไว้ในแหล่งข้อมูล
คุณจัดเก็บ Baseline Profile ในไดเรกทอรีแหล่งที่มาได้ผ่านsaveInSrc
แฟล็กในbuild.gradle.kts
ของโมดูลแอปพลิเคชันหรือไลบรารี
true
: โปรไฟล์พื้นฐานจะจัดเก็บไว้ในsrc/<variant>/generated/baselineProfiles
ซึ่งจะช่วยให้คุณสามารถคอมมิตโปรไฟล์ล่าสุด ที่สร้างขึ้นพร้อมกับแหล่งที่มาfalse
: ระบบจะจัดเก็บโปรไฟล์พื้นฐานไว้ในไฟล์ชั่วคราวในไดเรกทอรีบิลด์ ด้วยวิธีนี้ เมื่อคอมมิตโค้ด คุณจะไม่บันทึกโปรไฟล์ที่สร้างล่าสุด
Kotlin
baselineProfile { saveInSrc = true }
Groovy
baselineProfile { saveInSrc true }
นอกจากนี้ คุณยังระบุลักษณะการทำงานนี้ต่อตัวแปรได้ด้วย โดยทำดังนี้
Kotlin
baselineProfile { variants { freeRelease { saveInSrc = true } } }
Groovy
baselineProfile { variants { freeRelease { saveInSrc true } } }
ปิดใช้คำเตือน
โดยค่าเริ่มต้น Gradle Plugin ของ Baseline Profile จะเตือนคุณถึงสถานการณ์ที่
อาจทำให้เกิดปัญหา หากต้องการปิดใช้คำเตือน คุณสามารถตั้งค่าตัวเลือกที่เกี่ยวข้องเป็น
false
ในไฟล์ build.gradle.kts
ได้ ตัวเลือกคำเตือนมีดังนี้
baselineProfile {
warnings {
/**
* Warn when the Android Gradle Plugin version is higher than the max
* tested one.
*/
maxAgpVersion = true
/**
* Warn when a benchmark or baseline profile variant has been disabled.
*/
disabledVariants = true
/**
* Warn that running `generateBaselineProfile` with AGP 8.0 doesn't
* support running instrumentation tests for multiple build types at
* once.
*/
multipleBuildTypesWithAgp80 = true
/**
* Warn when no baseline profiles are generated after running the
* generate baseline profile command.
*/
noBaselineProfileRulesGenerated = true
/**
* Warn when no startup profiles are generated after running the generate
* baseline profile command.
*/
noStartupProfileRulesGenerated = true
}
}
กฎโปรไฟล์ตัวกรอง
ปลั๊กอิน Gradle ของ Baseline Profile ช่วยให้คุณกรองกฎ Baseline Profile ที่สร้างขึ้นได้ ซึ่งจะเป็นประโยชน์อย่างยิ่งสำหรับไลบรารี หากคุณต้องการยกเว้นกฎโปรไฟล์สำหรับคลาสและเมธอดที่เป็นส่วนหนึ่งของทรัพยากร Dependency อื่นๆ ของแอปตัวอย่างหรือไลบรารีเอง ตัวกรองสามารถรวมและยกเว้นแพ็กเกจและคลาสที่เฉพาะเจาะจงได้ เมื่อคุณระบุเฉพาะการยกเว้น ระบบจะยกเว้นเฉพาะกฎ Baseline Profile ที่ตรงกัน และจะรวมรายการอื่นๆ ทั้งหมด
ข้อกําหนดตัวกรองอาจเป็นอย่างใดอย่างหนึ่งต่อไปนี้
- ชื่อแพ็กเกจที่ลงท้ายด้วยไวลด์การ์ดคู่เพื่อให้ตรงกับแพ็กเกจที่ระบุและ
แพ็กเกจย่อยทั้งหมด เช่น
com.example.**
ตรงกับcom.example.method
และcom.example.method.bar
- ชื่อแพ็กเกจที่ลงท้ายด้วยไวลด์การ์ดเพื่อให้ตรงกับแพ็กเกจที่ระบุเท่านั้น เช่น
com.example.*
ตรงกับcom.example.method
แต่ไม่ตรงกับcom.example.method.bar
- ชื่อคลาสที่จะจับคู่กับคลาสที่เฉพาะเจาะจง เช่น
com.example.MyClass
ตัวอย่างต่อไปนี้แสดงวิธีรวมและยกเว้นแพ็กเกจที่เฉพาะเจาะจง
Kotlin
baselineProfile { filter { include("com.somelibrary.widget.grid.**") exclude("com.somelibrary.widget.grid.debug.**") include("com.somelibrary.widget.list.**") exclude("com.somelibrary.widget.list.debug.**") include("com.somelibrary.widget.text.**") exclude("com.somelibrary.widget.text.debug.**") } }
Groovy
baselineProfile { filter { include 'com.somelibrary.widget.grid.**' exclude 'com.somelibrary.widget.grid.debug.**' include 'com.somelibrary.widget.list.**' exclude 'com.somelibrary.widget.list.debug.**' include 'com.somelibrary.widget.text.**' exclude 'com.somelibrary.widget.text.debug.**' } }
ปรับแต่งกฎตัวกรองสำหรับตัวแปรต่างๆ ดังนี้
Kotlin
// Non-specific filters applied to all the variants. baselineProfile { filter { include("com.myapp.**") } } // Flavor-specific filters. baselineProfile { variants { free { filter { include("com.myapp.free.**") } } paid { filter { include("com.myapp.paid.**") } } } } // Build-type-specific filters. baselineProfile { variants { release { filter { include("com.myapp.**") } } } } // Variant-specific filters. baselineProfile { variants { freeRelease { filter { include("com.myapp.**") } } } }
Groovy
// Non-specific filters applied to all the variants. baselineProfile { filter { include 'com.myapp.**' } } // Flavor-specific filters. baselineProfile { variants { free { filter { include 'com.myapp.free.**' } } paid { filter { include 'com.myapp.paid.**' } } } } // Build-type specific filters. baselineProfile { variants { release { filter { include 'com.myapp.**' } } } } // Variant-specific filters. baselineProfile { variants { freeRelease { filter { include 'com.myapp.**' } } } }
นอกจากนี้ คุณยังกรองกฎโดยใช้อาร์กิวเมนต์ filterPredicate
ใน
BaselineProfileRule.collect()
ได้ด้วย แต่เราขอแนะนำให้ใช้ปลั๊กอิน Gradle เพื่อ
กรอง เนื่องจากเป็นวิธีที่ง่ายกว่าในการกรองแพ็กเกจย่อยและเป็นที่เดียว
ในการกำหนดค่าทั้งโมดูล
ปรับแต่งประเภทการสร้างเกณฑ์เปรียบเทียบและโปรไฟล์พื้นฐาน
ปลั๊กอิน Gradle ของ Baseline Profile จะสร้างประเภทบิลด์เพิ่มเติมเพื่อสร้าง
โปรไฟล์และเรียกใช้การทดสอบประสิทธิภาพ ประเภทบิลด์เหล่านี้มีคำนำหน้าเป็น
benchmark
และ nonMinified
เช่น สำหรับrelease
ประเภทบิลด์
ปลั๊กอินจะสร้างประเภทบิลด์ benchmarkRelease
และ nonMinifiedRelease
ระบบจะกำหนดค่าประเภทบิลด์เหล่านี้โดยอัตโนมัติสำหรับกรณีการใช้งานที่เฉพาะเจาะจง และโดยทั่วไปแล้วไม่จำเป็นต้องมีการปรับแต่งใดๆ แต่ในบางกรณี การใช้ตัวเลือกที่กำหนดเองบางอย่างอาจยังมีประโยชน์อยู่ เช่น การใช้การกำหนดค่าการลงนามที่แตกต่างกัน
คุณปรับแต่งประเภทบิลด์ที่สร้างขึ้นโดยอัตโนมัติได้โดยใช้ชุดย่อยของ พร็อพเพอร์ตี้ประเภทบิลด์ และระบบจะลบล้างพร็อพเพอร์ตี้ที่ใช้ไม่ได้ ตัวอย่างต่อไปนี้แสดงวิธีปรับแต่งประเภทบิลด์เพิ่มเติมและพร็อพเพอร์ตี้ที่ถูกลบล้าง
Kotlin
android { buildTypes { release { ... } create("benchmarkRelease") { // Customize properties for the `benchmarkRelease` build type here. // For example, you can change the signing config (by default // it's the same as for the `release` build type). signingConfig = signingConfigs.getByName("benchmarkRelease") } create("nonMinifiedRelease") { // Customize properties for the `nonMinifiedRelease` build type here. signingConfig = signingConfigs.getByName("nonMinifiedRelease") // From Baseline Profile Gradle plugin 1.2.4 and higher, you can't // customize the following properties, which are always overridden to // avoid breaking Baseline Profile generation: // // isJniDebuggable = false // isDebuggable = false // isMinifyEnabled = false // isShrinkResources = false // isProfileable = true // enableAndroidTestCoverage = false // enableUnitTestCoverage = false } } }
Groovy
android { buildTypes { release { ... } benchmarkRelease { // Customize properties for the `benchmarkRelease` build type here. // For example, you can change the signing config (by default it's the // same as for the `release` build type.) signingConfig = signingConfigs.benchmarkRelease } nonMinifiedRelease { // Customize properties for the `nonMinifiedRelease` build type here. signingConfig = signingConfigs.nonMinifiedRelease // From Baseline Profile Gradle plugin 1.2.4 and higher, you can't use // the following properties, which are always overridden to avoid breaking // Baseline Profile generation: // // isJniDebuggable = false // isDebuggable = false // isMinifyEnabled = false // isShrinkResources = false // isProfileable = true // enableAndroidTestCoverage = false // enableUnitTestCoverage = false } } }
หมายเหตุเพิ่มเติม
เมื่อสร้างโปรไฟล์พื้นฐาน สิ่งที่คุณควรทราบเพิ่มเติมมีดังนี้
โปรไฟล์พื้นฐานที่คอมไพล์แล้วต้องมีขนาดเล็กกว่า 1.5 MB ซึ่งไม่ รวมถึงรูปแบบข้อความในไฟล์ต้นฉบับ ซึ่งโดยปกติจะมีขนาดใหญ่กว่ามาก ก่อนการคอมไพล์ ยืนยันขนาดของ Baseline Profile แบบไบนารีโดยค้นหาในอาร์ติแฟกต์เอาต์พุตที่อยู่ใต้
assets/dexopt/baseline.prof
สำหรับ APK หรือBUNDLE-METADATA/com.android.tools.build.profiles/baseline.prof
สำหรับ AABกฎแบบกว้างที่รวบรวมแอปพลิเคชันมากเกินไปอาจทำให้การเริ่มต้นทำงานช้าลง เนื่องจากการเข้าถึงดิสก์เพิ่มขึ้น หากเพิ่งเริ่มต้นใช้งานโปรไฟล์พื้นฐาน ไม่ต้องกังวลเกี่ยวกับเรื่องนี้ อย่างไรก็ตาม การเพิ่มเส้นทางจำนวนมากอาจส่งผลให้ประสิทธิภาพต่ำกว่าที่ควรจะเป็น ทั้งนี้ขึ้นอยู่กับแอปของคุณ รวมถึงขนาดและจำนวนเส้นทาง ทดสอบประสิทธิภาพของแอปโดยลองใช้โปรไฟล์ต่างๆ และยืนยันว่าประสิทธิภาพไม่ได้ลดลงหลังจาก เพิ่มโปรไฟล์