Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 296
Nino van Hooff Avatar asked Aug 10 '17 08:08

Nino van Hooff


People also ask

Can I develop Android app using only Kotlin?

Kotlin is an officially supported language for developing Android apps, along with Java.

Can Kotlin be used for app development?

Kotlin is a modern statically typed programming language used by over 60% of professional Android developers that helps boost productivity, developer satisfaction, and code safety.

How do I write a library in Kotlin?

Create a projectIn the left-hand panel, select Kotlin Multiplatform. Enter a project name, then in the Multiplatform section select Library as the project template. Select the Gradle DSL – Kotlin or Groovy. Specify the JDK, which is required for developing Kotlin projects.

How do I use Kotlin library on Android?

To convert Java code to Kotlin, open the Java file in Android Studio, and select Code > Convert Java File to Kotlin File. Alternatively, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file.


1 Answers

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" } 
like image 174
Joshua Avatar answered Oct 08 '22 23:10

Joshua