排毒 - 无法初始化类 DefaultKotlinSourceSetKt

Posted

技术标签:

【中文标题】排毒 - 无法初始化类 DefaultKotlinSourceSetKt【英文标题】:Detox - Could not initialize class DefaultKotlinSourceSetKt 【发布时间】:2020-08-26 02:29:25 【问题描述】:

我已按照此处的 android 排毒指南安装到我的 react-native 项目 - https://github.com/wix/Detox/blob/master/docs/Introduction.Android.md。但是在运行react-native run-android 构建应用程序之后。我收到以下错误评估项目:detox

1: Task failed with an exception.
-----------
* Where:
Build file 'C:\Users\brian\Documents\Projects\react-native-prototyping\node_modules\detox\android\detox\build.gradle' line: 2

* What went wrong:
A problem occurred evaluating project ':detox'.
> Could not initialize class org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSetKt

我查看了他们的问题跟踪器,但没有发现相关问题。查看围绕 Kotlin 的类似问题,似乎我的 kotlin 和 gradle 版本之间存在冲突。但我不确定如何确定要使用的正确版本。我还在 android 文件夹中做了一个./gradlew clean 无济于事。

问题:

如何解决“无法初始化类 ..sources.DefaultKotlinSourceSetKt”错误?

我的 gradle 和包设置的一些细节如下:

"devDependencies": 
        "detox": "^16.5.0",
    ,

root build.gradle:

buildscript 
    ext 
        buildToolsVersion = "28.0.3"
        minSdkVersion = 18
        compileSdkVersion = 28
        targetSdkVersion = 28
        kotlinVersion = '1.3.0'

    
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath("com.android.tools.build:gradle:3.5.2")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    

app/build.gradle:

android 
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    

    defaultConfig 
        applicationId "com.testapp"
        minSdkVersion rootProject.ext.minSdkVersion
        compileSdkVersion rootProject.ext.compileSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        // detox automated tests config
        // This will later be used to control the test apk build type
        testBuildType System.getProperty('testBuildType', 'debug')  
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    
    splits 
        abi 
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        
    
    signingConfigs 
        debug 
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        
        release 
            storeFile file(System.getenv("KEYSTORE") ?: "keystore.jks")
            storePassword System.getenv("KEYSTORE_PASSWORD")
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
        
    
    buildTypes 
        debug 
            signingConfig signingConfigs.debug
        
        release 
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            // Detox-specific additions to pro-guard
            proguardFile "$rootProject.projectDir/../node_modules/detox/android/detox/proguard-rules-app.pro"
        
    

    packagingOptions 
        pickFirst "lib/armeabi-v7a/libc++_shared.so"
        pickFirst "lib/arm64-v8a/libc++_shared.so"
        pickFirst "lib/x86/libc++_shared.so"
        pickFirst "lib/x86_64/libc++_shared.so"
    



dependencies 
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules


    if (enableHermes) 
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
     else 
        implementation jscFlavor
    

    androidTestImplementation(project(path: ":detox"))

【问题讨论】:

【参考方案1】:

原来我必须在 build.gradle 中更改我的 kotlin 版本以匹配我的 gradle 版本 - buildscript。见-https://github.com/wix/Detox/blob/master/examples/demo-react-native/android/build.gradle

 
    ext 
        buildToolsVersion = "28.0.3"
        minSdkVersion = 18
        compileSdkVersion = 28
        targetSdkVersion = 28
        kotlinVersion = '1.3.41'

    
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath("com.android.tools.build:gradle:3.5.2")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    

【讨论】:

很高兴你解决了这个问题。请注意,指南现在会提示您去检查最新的 kotlin 版本。 在我的项目中,我只需要将 kotlinVersion 从 1.3.11 更新到 1.3.72。似乎新的 gradle plagun v4 使用了一些新的语言功能。【参考方案2】:

This answer 帮助了我。我需要更新我的 Kotlin 版本。

工具 -> Kotlin -> 配置 Kotlin 插件更新 -> 立即检查更新

更新后,我更改了 build.gradle(项目)中的版本以匹配新更新的版本。即

buildscript 
    ext 
        kotlin_version = '1.3.72'
        ...
    

【讨论】:

以上是关于排毒 - 无法初始化类 DefaultKotlinSourceSetKt的主要内容,如果未能解决你的问题,请参考以下文章

运行排毒测试命令时无法读取未定义的属性“类型”

排毒测试时,React Native 在初始屏幕上超时完成

无法从“init.js”中找到模块“detox/runners/jest/adapter” - 排毒

由于在应用程序安装期间超时而无法运行排毒测试

无法排毒找到 iPhone X 或 iPhone 11 之前的模拟器

仅排毒测试启动画面