Search code examples
androidkotlinandroid-gradle-pluginandroid-multi-modulemacrobenchmark

Android Macrobenchmark module doesn't recognise 'com.android.test' plugin


I'm trying to put the MacroBenchmark library in my project, but I'm getting an error to apply com.android.test the plugin. This error happens while syncing Gradle.

I've added the plugin root gradle using the same AGP version (7.3.1). I'm also using Kotlin 1.7.10.

The benchmark module is inside a sub-package and not at the root of the project.

An exception occurred applying plugin request [id: 'com.android.test']
> Failed to apply plugin class 'com.android.build.gradle.api.AndroidBasePlugin'.
   > Extension of type 'TestedExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, SonarQubeExtension, Property<ModuleType>, ReportingExtension, DetektExtension, LibrariesForLibs, VersionCatalogsExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, JavaToolchainService, JavaPluginExtension, NamedDomainObjectContainer<BaseVariantOutput>, TestExtension, TestAndroidComponentsExtension]

I tried to remove this plugin just to build and see if was possible to run some benchmarks. Without success. After removing the plugin I get an error on the:

android {
    ...

    targetProjectPath = ":app"
    ...
}

> Could not set unknown property 'targetProjectPath' for extension 'android' of type com.android.build.gradle.LibraryExtension.

If I remove this targetProjectPath the project builds, but it isn't possible to run any tests.

BTW, I tested it using Android Studio 2022.2.1 Canary 9 and 2021.3.1 Patch 1

EDIT:

Root build.gradle

buildscript {
    apply from: "./buildSystem/android-config.gradle"

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-serialization:${libs.versions.kotlin.get()}"
        classpath 'com.google.firebase:perf-plugin:1.4.1'
    }
}

plugins {
    id 'com.android.test' version '7.3.1' apply false
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.kotlin) apply false
    alias(libs.plugins.hilt) apply false
    alias(libs.plugins.crashlytics) apply false
    alias(libs.plugins.google.services) apply false
    alias(libs.plugins.quadrant) apply false
    alias(libs.plugins.sonarqube)

    alias(libs.plugins.test.gradle.retry) apply false

    // Core-lib plugins
    alias(libs.plugins.corelib.detekt)
    alias(libs.plugins.corelib.googlePlayReleaseNotes)
    alias(libs.plugins.corelib.moduleDependenciesCheck)
    alias(libs.plugins.corelib.coverage)
}

Macrobenchmark gradle

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

android {
    namespace 'com.example.benchmark'
    compileSdk 33

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

    defaultConfig {
        minSdk 23
        targetSdk 33

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    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.
        benchmark {
            debuggable = true
            signingConfig = debug.signingConfig
            matchingFallbacks = ["release"]
        }
    }

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

dependencies {
    implementation 'androidx.test.ext:junit:1.1.4'
    implementation 'androidx.test.espresso:espresso-core:3.5.0'
    implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    implementation 'androidx.benchmark:benchmark-macro-junit4:1.1.1'
}

androidComponents {
    beforeVariants(selector().all()) {
        enabled = buildType == "benchmark"
    }
}

Solution

  • I could figure out (with my teammates' help) what was wrong with it. We were removing TestedExtension from our project. After fixing the plugin, was possible to create the benchmark module without any issues.