【问题标题】:android Baseline Profile with productFlavors带有 productFlavors 的 android Baseline Profile
【发布时间】:2022-11-07 19:31:07
【问题描述】:

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

它工作正常。但是当我添加 productFlavors 时,它运行失败。

消费者被配置为查找组件的运行时,以及属性“com.android.build.api.attributes.BuildTypeAttr”的值为“benchmark”,属性“com.android.build.api.attributes.AgpVersionAttr”为值“7.1.1”。但是,我们无法在以下项目变体中进行选择:app: - 开发基准运行时元素 - 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.

这是 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"
}

【问题讨论】:

  • 您如何处理为每个变体/风味组合存储不同的baseline-prof.txt

标签: android performance baseline-profile


【解决方案1】:

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

你应该有 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"
    }
}

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

【讨论】:

  • 感谢@Matpag,在基准模块中将missingDimensionStrategy("default", "live") 添加到defaultConfig 后它可以工作
  • 很高兴它起作用了,我将那部分添加到答案中,也许可以帮助其他人:)
猜你喜欢
  • 1970-01-01
  • 2017-10-05
  • 2021-02-21
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多