Android – Using Kotlin in a library module without using it in the app module

androidgradlekotlin

I'm trying to use Kotlin in a library module without using it in the app module. The app module only uses Java and does not use any Kotlin classes from the library. Gradle won't compile hoever:

Error:(2, 1) A problem occurred evaluating project ':<Library>'.
> Plugin with id 'kotlin-android' not found.

Changes I made to include Kotlin:

{library root} / build.gradle

buildscript {
ext.kotlin_version = '1.1.3-2'

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

allprojects {
repositories {
    jcenter()
}
}

{library root} / {library module} / build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
...

dependencies{
    ...
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

When I add the same to the app module, the project compiles without issue, but I'd like to avoid adding it in the app module because I'd like to use this library in multiple apps without making code changes to those apps

Gradle version used : 3.3
android gradle plugin version: 2.3.3

Edit: @Jushua's answer works, but it still requires to update the project root build.gradle. I was hoping for a solution where only the dependency on the library would have to be added to make the whole thing work.

Best Answer

I am able to do that without any problem.

build.gradle (Project)

buildscript {
    ext.kotlin_version = "1.1.4"
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:2.3.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

build.gradle (app)

apply plugin: "com.android.application"

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 21
        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 {
    compile fileTree(dir: "libs", include: ["*.jar"])
    androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
        exclude group: "com.android.support", module: "support-annotations"
    })
    compile "com.android.support:appcompat-v7:26.0.1"
    testCompile "junit:junit:4.12"
    compile project(path: ":library")
}

build.gradle (library)

apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 13
        versionName "1.1.4"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2", {
        exclude group: "com.android.support", module: "support-annotations"
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "com.android.support:appcompat-v7:26.0.1"
    testCompile "junit:junit:4.12"
}