我在Gradle托管设备设置中可能会遇到什么问题?

km0tfn4u  于 2022-11-14  发布在  其他
关注(0)|答案(2)|浏览(211)

Gradle托管设备引入了一项新功能(例如,请参见此处:(第10页)
安装程序看起来很简单,只需要复制几行到模块级的build.gradle文件中,一切都应该可以正常工作。
很遗憾,我的情况不是这样,我争取一些建议,请。代码是红色的,脚本没有成功。请看我的build.gradle.kts文件:

带下划线的ManagedVirtualDevice显示以下错误:

我的Android工作室版本是Android工作室大黄蜂|2021.1.1金丝雀11构建编号AI-211.7628.21.2111.7676841,构建日期2021年8月26日
同步Gradle显示:

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)
    ...

哪一个

i86rm4rw

i86rm4rw1#

刚刚遇到了同样的问题-您需要示例化一个ManagedVirtualDevice对象并对其进行配置,然后再将其添加到您的设备列表:

import com.android.build.gradle.internal.dsl.ManagedVirtualDevice

testOptions {
    devices {
        add(
            ManagedVirtualDevice("pixel2api30").apply {
                device = "Pixel 2"
                apiLevel = 30
                systemImageSource = "aosp-atd"
                abi = "x86"
            }
        )
    }
}

请注意,导入与Google文档中的描述略有不同--在他们的示例中,他们传递的是接口,而不是实现。

2j4z5cfb

2j4z5cfb2#

正确的等效方法是使用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) { ... }

或者在需要引用设备时使用by(例如,组):

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

请参阅中的org.gradle.kotlin.dsl.NamedDomainObjectContainerScope
gradle-kotlin-dsl-7.4.jar!/org/gradle/kotlin/dsl/NamedDomainObjectContainerExtensionsKt.class

相关问题