37

I am very new to both the Android and JVM platforms. Using Android Studio, I would like to create an Android app and put most of my business logic in a library. I would also like to just use Kotlin, no Java.

When I go to File > New Module, the options listed are

  • Phone & Tablet module
  • Android Library
  • Instant App
  • Feature Module
  • Android Wear Module
  • Android TV Module
  • Android Things Module
  • Import Gradle Project
  • Import Eclipse ADT Project
  • Import .JAR/.AAR Package
  • Java Library

I can create a Kotlin-based library with the Android Library option, but this also includes a lot of files for dealing with resources and controls.

I just want a pure Kotlin library. What is the easiest way to create one?

  • Can I delete a portion of an Android Library?
  • Can I change some settings in a Java Library?
  • Can I download a plugin that will just give me the option to create a Kotlin library?

I am still a bit confused the file organization in Java/Kotlin/Android projects.

2 Answers 2

57

You need a module with no Android dependencies and resources - this is what a Java library module does, so you start by creating one of those. From there, you just need to enable Kotlin inside this module.

The build.gradle file you get when you create your module is something like this:

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

You need to add the Kotlin plugin and standard library. If you don't have the mavenCentral repository added for all modules in your project yet, you need to add that as well:

apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

repositories {
    mavenCentral()
}

This is assuming that you have a kotlin_version declared in your project level build.gradle file, which is the usual way for Android projects, and I believe that's how Android Studio configures a project if you tick the checkbox to use Kotlin.


With those changes to your library's build.gradle, you can remove the automatically generated MyClass.java file from your library's source folder (/src/main/java/your/package/name), and start adding Kotlin files instead. If you'd like, you can also rename the /src/main/java folder to /src/main/kotlin. Or you can use both folders, this is entirely up to you.

6
  • 2
    "Plugin version is not the same as library version" warning: stackoverflow.com/questions/49956051/…
    – User
    Commented Apr 6, 2019 at 9:23
  • How would I make kotlin classes package private so that apps that use my library cannot import those files?
    – viper
    Commented Jun 13, 2019 at 8:31
  • 1
    @viper look into the internal visibiity, that's what you want in libraries a lot of the time.
    – zsmb13
    Commented Jun 18, 2019 at 13:41
  • Unable to find a matching configuration of project :DD_kotlin_lib: None of the consumable configurations have attributes - though the library builds successfully Commented Aug 1, 2019 at 22:30
  • @zsmb13 Is there a comprehensive explanation why "Java or Kotlin Library" module can't use Android dependencies? In terms of resources and similar stuff it is clear - we have no access to android.jar, right? But what if I want to reference livedata dependencies, why that's not allowed, also because of transient dependencies on android.jar? "Java or Kotlin Library" should be separated from any OS runtime?
    – Yurii Tsap
    Commented Apr 22, 2020 at 10:54
-3

Create Android studio project After that create as many as module which are basically Android library You can select the language of module (java/kotlin)

Extra Upload your module to github and use jitpcak tool to make your module a library like this Implementation 'com. My. Module:1.0'

And use your module any future project

1
  • 2
    Old post but I would like to correct this. OP wants to add a new module to act as the business layer. An Android Module is not the way to do this, OP needs a "Java Library". According to clean architecture, inner modules must not contain anything related to Android APIs. The Android Module adds a lot of Android specific folders and components and they must be removed to adhere to the clean arcihtecture, which is a lot of work. The accepted answer is the easiest way to do this.
    – Batuhan C.
    Commented Nov 1, 2019 at 6:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.