Home > Software engineering >  android Baseline Profile with productFlavors
android Baseline Profile with productFlavors

Time:06-26

I'm applying Baseline Profiles to my app to improve performance and I follow this link on android page https://developer.android.com/topic/performance/baselineprofiles

and it works fine. But when I add productFlavors it runs failed.

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

Does anyone know how to deal with this situation?

Under is the full error detail and build.gradle file.

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.

Here is the 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"
}

CodePudding user response:

Did you create a separate module for profiling?

You should have 2 modules (at least):

  • app
  • benchmark

Then in your benchmark module you need to configure the profiling tool, check what Google is doing here: https://github.com/android/nowinandroid/blob/aaa60f1b5cbcd5ff5cc1ba4ddcf90789619782f8/benchmark/build.gradle.kts in their "Now In Android" app.

Most important parts are those configuration blocks:

android {
    //.... other things
    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"
    }
}

You can check their app structure on GitHub and the app build.gradle part too

  • Related