androidgradlebuild.gradle

What issue could I have in Gradle managed device setup?


There was introduced a new feature Gradle managed devices (see for example here: https://developer.android.com/studio/preview/features?hl=fr)

The setup seems to be pretty straightforward, just copy a few lines to the module level build.gradle file and everything should work.

Sadly it is not the case for me and I strive for some advice, please. The code is red and the script doesn't succeed. See my build.gradle.kts file:

enter image description here

The underlined ManagedVirtualDevice shows the following error:

enter image description here

My Android studio version is Android Studio Bumblebee | 2021.1.1 Canary 11 Build #AI-211.7628.21.2111.7676841, built on August 26, 2021.

Syncing Gradle shows this:

org.gradle.internal.exceptions.LocationAwareException: Build file '/*****/app/build.gradle.kts' line: 112
Script compilation errors:

  Line 112:             pixel2api29 (com.android.build.api.dsl.ManagedVirtualDevice) {
                        ^ Unresolved reference: pixel2api29

  Line 112:             pixel2api29 (com.android.build.api.dsl.ManagedVirtualDevice) {
                                                               ^ Classifier 'ManagedVirtualDevice' does not have a companion object, and thus must be initialized here

  Line 114:                 device = "Pixel 2"
                            ^ Unresolved reference: device

  Line 115:                 apiLevel = 29
                            ^ Unresolved reference: apiLevel

  Line 117:                 systemImageSource = "google"
                            ^ Unresolved reference: systemImageSource

  Line 118:                 abi = "x86"
                            ^ Unresolved reference: abi

6 errors
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageOf$cacheDir$1.invoke(Interpreter.kt:666)
    at org.gradle.kotlin.dsl.execution.Interpreter$ProgramHost$compileSecondStageOf$cacheDir$1.invoke(Interpreter.kt:387)
    at org.gradle.kotlin.dsl.provider.CompileKotlinScript.execute(KotlinScriptEvaluator.kt:375)
    at org.gradle.internal.execution.steps.ExecuteStep.executeInternal(ExecuteStep.java:89)
    at org.gradle.internal.execution.steps.ExecuteStep.access$000(ExecuteStep.java:40)
    at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:53)
    at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:50)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:200)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:195)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)
    ... 

Which


Solution

  • Just ran into the same issue - you need to instantiate a ManagedVirtualDevice object and configure it, before adding it to your devices list:

    import com.android.build.gradle.internal.dsl.ManagedVirtualDevice
    
    testOptions {
        devices {
            add(
                ManagedVirtualDevice("pixel2api30").apply {
                    device = "Pixel 2"
                    apiLevel = 30
                    systemImageSource = "aosp-atd"
                    abi = "x86"
                }
            )
        }
    }
    

    Note that the import is subtly different from what Google's documentation says - in their example they're passing the interface, not an implementation.