Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 690
Smeagol Avatar asked Mar 01 '23 11:03

Smeagol


2 Answers

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.

like image 86
ditn Avatar answered Mar 04 '23 08:03

ditn


The proper equivalent is using methods for a NamedDomainObjectContainer:

import com.android.build.api.dsl.ManagedVirtualDevice
android {
    testOptions {
        devices {
            // create it now (if it doesn't exist yet) and configure it (exactly as in Groovy):
            maybeCreate<ManagedVirtualDevice>("pixel2api29").apply { ... }

            // or

            // create it now and configure it:
            create<ManagedVirtualDevice>("pixel2api29") { ... }

            // or

            // create it later and configure it:
            register<ManagedVirtualDevice>("pixel2api29") { ... }

            // or

            // if it already exists (created elsewhere), configure it:
            "pixel2api29"(ManagedVirtualDevice::class) { ... }

or using by in case you need to reference the device (e.g. groups):

devices {
    val pixel2api29 by creating(ManagedVirtualDevice::class) { ... }
    // or
    val pixel2api29 by registering(ManagedVirtualDevice::class) { ... }
    deviceGroups {
        "phoneAndTablet" {
            targetDevices.add(pixel2api29)
        }
    }
}

See org.gradle.kotlin.dsl.NamedDomainObjectContainerScope in
gradle-kotlin-dsl-7.4.jar!/org/gradle/kotlin/dsl/NamedDomainObjectContainerExtensionsKt.class

like image 40
TWiStErRob Avatar answered Mar 04 '23 06:03

TWiStErRob