gpt4 book ai didi

android - 无法在空对象上获取属性 'kotlinOutputDir' - Kotlin 和 Spock

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:14 25 4
gpt4 key购买 nike

我正在尝试使用 Spock Framework 测试 Kotlin 类。当我的项目中只有 Java 类时,一切正常。但是在将 1.1.51 版本的 kotlin 添加到我的项目后,测试停止工作,现在我收到这样的错误:

Error:Execution failed for task ':app:compileDebugUnitTestGroovyWithGroovyc'.
> Cannot get property 'kotlinOutputDir' on null object

我正在使用 Android Studio 3.0 Beta 7

这是我的项目和模块 build.gradle 文件:

项目build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.51'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta7'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

模块build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'groovyx.android'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.redione.debtscalc"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-auth:11.4.2'
implementation 'com.google.firebase:firebase-database:11.4.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

testImplementation 'org.mockito:mockito-core:2.10.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})

testImplementation 'org.codehaus.groovy:groovy:2.4.11:grooid'
testImplementation('org.spockframework:spock-core:1.1-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
}


apply plugin: 'com.google.gms.google-services'
repositories {
mavenCentral()
}

最佳答案

更新

Groovy Android Gradle Plugin 发布了v2.0.0,兼容Android Gradle Plugin 3.0.0。编辑你的根 build.gradle:

classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:2.0.0'

原来的答案(不应该使用!),我留作历史用途。

原始答案

简短的回答是,这个问题应该很快就会得到解决,目前正在进行中的 Groovy Android Gradle 插件 v2.0.0。

较长的答案(立即得到返回)是您可以按如下方式解决此问题。参见 my comment关于 Github 上的问题。

应将以下内容添加到 app/build.gradle:

// Ugly workaround for incompatibility between Groovy Android Gradle Plugin 1.2.0 and Android Gradle Plugin 3.0.0
// Should be fixed with Groovy Android Gradle Plugin 1.3.0. https://github.com/groovy/groovy-android-gradle-plugin/pull/155
task copyFlavorDebugUnitTestKotlinClasses(type: CopyKotlinClasses)
class CopyKotlinClasses extends DefaultTask {

def kotlinOutputDir = ["$project.rootDir/app/build/tmp/kotlin-classes/flavorDebugUnitTest"]

@TaskAction
def doNothing() {
}
}

请注意,您必须将“flavor”替换为您项目的实际 flavor ,如果您不使用产品 flavor ,则将其删除。

关于android - 无法在空对象上获取属性 'kotlinOutputDir' - Kotlin 和 Spock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46731894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com