android 基线配置文件与productFlavors

TRo*_*ose 7 performance android baseline-profile

我正在将基线配置文件应用到我的应用程序中以提高性能,并且我按照 android 页面上的此链接https://developer.android.com/topic/performance/baselineprofiles

而且效果很好。但是当我添加productFlavors时它运行失败。

消费者被配置为查找组件的运行时,以及值为“benchmark”的属性“com.android.build.api.attributes.BuildTypeAttr”,属性“com.android.build.api.attributes.AgpVersionAttr”值“7.1.1”。但是,我们无法在项目的以下变体之间进行选择:app: - devBenchmarkRuntimeElements - liveBenchmarkRuntimeElements

有谁知道如何处理这种情况?

下面是完整的错误详细信息和 build.gradle 文件。

Could not determine the dependencies of task 
':benchmark:connectedBenchmarkAndroidTest'.
> Could not determine the dependencies of null.
   > Could not resolve all task dependencies for configuration 
':benchmark:benchmarkTestedApks'.
  > Could not resolve project :app.
    Required by:
        project :benchmark
     > The consumer was configured to find a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1'. However we cannot choose between the following variants of project :app:
         - devBenchmarkRuntimeElements
         - liveBenchmarkRuntimeElements
       All of them match the consumer attributes:
         - Variant 'devBenchmarkRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark':
             - Unmatched attributes:
                 - Provides attribute 'com.android.build.api.attributes.ProductFlavor:default' with value 'dev' but the consumer didn't ask for it
                 - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'devBenchmark' but the consumer didn't ask for it
                 - Provides attribute 'default' with value 'dev' but the consumer didn't ask for it
                 - Provides a library but the consumer didn't ask for it
                 - Provides attribute 'org.gradle.jvm.environment' with value 'android' but the consumer didn't ask for it
                 - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
         - Variant 'liveBenchmarkRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark':
             - Unmatched attributes:
                 - Provides attribute 'com.android.build.api.attributes.ProductFlavor:default' with value 'live' but the consumer didn't ask for it
                 - Provides attribute 'com.android.build.gradle.internal.attributes.VariantAttr' with value 'liveBenchmark' but the consumer didn't ask for it
                 - Provides attribute 'default' with value 'live' but the consumer didn't ask for it
                 - Provides a library but the consumer didn't ask for it
                 - Provides attribute 'org.gradle.jvm.environment' with value 'android' but the consumer didn't ask for it
                 - Provides attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but the consumer didn't ask for it
       The following variants were also considered but didn't match the requested attributes:
         - Variant 'devBenchmarkApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark':
             - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component
         - Variant 'devDebugApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'devDebugRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'devReleaseApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'devReleaseRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'liveBenchmarkApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1', attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark':
             - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component
         - Variant 'liveDebugApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'liveDebugRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'liveReleaseApiElements' capability My Application:app:unspecified declares a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares an API of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a runtime of a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'
         - Variant 'liveReleaseRuntimeElements' capability My Application:app:unspecified declares a runtime of a component, as well as attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '7.1.1':
             - Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release' and the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'benchmark'

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to 
get more log output. Run with --scan to get full insights.
Run Code Online (Sandbox Code Playgroud)

这是 build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.test.myapplication"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
           'proguard-rules.pro'
        }
        benchmark {
            initWith buildTypes.release
            signingConfig signingConfigs.debug
            matchingFallbacks = ['release']
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
                'proguard-benchmark-rules.pro'
            debuggable false
        }
    }
    flavorDimensions "default"
    productFlavors {
        dev {
            dimension "default"
        }
        live {
            dimension "default"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*Pag 8

您是否创建了一个单独的分析模块?

您应该(至少)有 2 个模块:

  • app
  • benchmark

然后在你的benchmark模块中,你需要配置分析工具,检查 Google 在这里做什么:https://github.com/android/nowinandroid/blob/aaa60f1b5cbcd5ff5cc1ba4ddcf90789619782f8/benchmark/build.gradle.kts 在他们的“Now In Android”应用程序中。

最重要的部分是这些配置块:

android {
    //.... other things
    defaultConfig {
        minSdk = 23
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        missingDimensionStrategy("contentType", "demo")
    }
    
    buildTypes {
        // This benchmark buildType is used for benchmarking, and should function like your
        // release build (for example, with minification on). It's signed with a debug key
        // for easy local/CI testing.
        val benchmark by creating {
            isDebuggable = false
            signingConfig = signingConfigs.getByName("debug")
            matchingFallbacks.add("release")
        }
    }

    targetProjectPath = ":app"
    experimentalProperties["android.experimental.self-instrumenting"] = true
}

//.... other things

androidComponents {
    beforeVariants {
        it.enable = it.buildType == "benchmark"
    }
}
Run Code Online (Sandbox Code Playgroud)

您也可以在 GitHub 上检查他们的应用程序结构以及应用程序build.gradle部分

  • 感谢@Matpag,在我将 `missingDimensionStrategy("default", "live")` 添加到基准模块中的 `defaultConfig` 后,它就可以工作了 (3认同)