安卓产品风格与productFlavors的基线配置

6

我正在将基线配置应用于我的应用程序,以提高性能,并且我按照 Android 文档上的链接进行操作:https://developer.android.com/topic/performance/baselineprofiles

这个过程很顺利。但是当我添加了 productFlavors 后,它就失败了。

消费者被配置为查找一个组件的运行时,以及带有值为 'benchmark' 的属性 'com.android.build.api.attributes.BuildTypeAttr' 和值为 '7.1.1' 的属性 'com.android.build.api.attributes.AgpVersionAttr'。但是我们无法在以下项目变体之间进行选择::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.

以下是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 文件? - Mark
1个回答

7

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

你应该至少有两个模块:

  • app
  • benchmark

然后在你的benchmark 模块中,你需要配置性能分析工具,查看Google在他们的"Now In Android" 应用中的做法:https://github.com/android/nowinandroid/blob/aaa60f1b5cbcd5ff5cc1ba4ddcf90789619782f8/benchmark/build.gradle.kts.

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

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部分


3
谢谢@Matpag,我在基准测试模块的defaultConfig中添加了missingDimensionStrategy("default", "live")后,它可以运行。 - TRose
1
很高兴它起作用了,我把那部分添加到答案中了,也许可以帮助其他人 :) - MatPag

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接