為程式庫建立基準設定檔

如要為程式庫建立基準設定檔,請使用基準設定檔 Gradle 外掛程式

為程式庫建立基準設定檔時,需要使用以下三個模組:

  • 範例應用程式模組:包含使用程式庫的範例應用程式。
  • 程式庫模組:您會為這個模組產生設定檔。
  • 基準設定檔模組:產生基準設定檔的測試模組。

如要為程式庫產生基準設定檔,請執行下列步驟:

  1. 建立新的 com.android.test 模組,例如 :baseline-profile
  2. 設定 :baseline-profile 模組的 build.gradle.kts 檔案。此設定基本上與應用程式相同,但請務必將 targetProjectPath 設為範例應用程式模組。
  3. :baseline-profile 測試模組中建立基準設定檔測試。這需要根據範例應用程式進行,且必須使用程式庫的所有功能。
  4. 更新程式庫模組中 build.gradle.ktss 檔案的設定,例如 :library
    1. 套用外掛程式 androidx.baselineprofile
    2. baselineProfile 依附元件新增至 :baseline-profile 模組。
    3. 套用所需的消耗者外掛程式設定,如以下範例所示。

    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.**'
        }
    }
    
  5. androidx.baselineprofile 外掛程式新增至應用程式模組 :sample-app 中的 build.gradle.kts 檔案。

    Kotlin

    plugins {
        ...
        id("androidx.baselineprofile")
    }
    

    Groovy

    plugins {
        ...
        id 'androidx.baselineprofile'
    }
    
  6. 執行下列程式碼即可產生設定檔:./gradlew :library:generateBaselineProfile

產生工作結束時,基準設定檔會儲存至 library/src/main/generated/baselineProfiles