Using kotlin-compiler in Android


#1

Hi all,
I have to build an Android application using Kotlin that evaluates Kotlin codes. In order to accomplish this I need to import as dependences org.jetbrains.kotlin:kotlin-compiler and org.jetbrains.kotlin:kotlin-script-util as below:

rks module gradle

buildscript {
    ext.kotlin_version = '1.2.31'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-compiler:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

than app module gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "it.matteopellegrino.kotlinsample"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "0.0.1"
        testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner"
        }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile project(":rks")
}

When importing these dependences Gradle sync works, but run fails with this error

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
More than one file was found with OS independent path 'META-INF/deserialization.kotlin_module'
I **cannot** exclude it in packagingOptions, because it would broke the entire kotlin run.

According to this article it seems a naming conflict between libraries, but the solutions provided does not work for me.

How can I solve this? Are there other ways to evaluate Kotlin code at runtime on Android?

(Outside Android env, with Intellij IDEA, it works as desidered)