2

I've followed the Detox guide for Android here to install on my react-native project - https://github.com/wix/Detox/blob/master/docs/Introduction.Android.md. But after running react-native run-android to build the app. I get the following error evaluating project :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

I've looked in their issue tracker but didn't see a related issue. Looking at similar issues around Kotlin it seems it could be a conflict between my kotlin and gradle version. But I'm not sure how to determine the correct versions to use. I also did a ./gradlew clean in the android folder to no avail.

Question:

How can you resolve 'Could not initialize class ..sources.DefaultKotlinSourceSetKt' error?

Some details of my gradle and package setup are as follows:

"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"))
}
Brian Var
  • 6,029
  • 25
  • 114
  • 212

3 Answers3

4

Turns out I had to change my kotlin version in build.gradle to match my gradle version - buildscript. See - 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
    }
}
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • Great that you had this worked out. Note the guide now prompts to go and check for the latest kotlin version. – d4vidi Jul 08 '20 at 07:25
  • Turns out in my project I just needed to update kotlinVersion from 1.3.11 to 1.3.72. Seems new gradle plagun v4 use some new language features. – vitalii Jul 29 '20 at 14:58
2

This answer helped me. I needed to update my version of Kotlin.

Tools -> Kotlin -> Configure Kotlin Plugin Updates -> Check for updates now

Once it was updated I changed the version in build.gradle (project) to match the newly updated version. i.e.

buildscript {
    ext {
        kotlin_version = '1.3.72'
        ...
    }
}
Amber
  • 2,413
  • 1
  • 15
  • 20
0

Open Android Folder in AndroidStudtio and Gradle clean works for me :)

aman yadav
  • 11
  • 2