Create a Kotlin library in Android Studio - android

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.

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.

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

Related

Set up all dependencies in one gradle file?

What I have
I have two different modules in my android project:
app: This is the main app android module, where is located almost all the code of the app.
core: This is another module where I would like to specify all the base code that will be used in the app module, like BaseFragment, BaseViewModel, or interfaces that I would like to implement in the app module.
In the core build.gradle file, I have all of these dependencies:
//Moshi
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
//Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$moshi_converter_version"
//okhttp
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_loggin_interceptor"
//Koin
implementation "io.insert-koin:koin-android:$koin_version"
implementation "io.insert-koin:koin-androidx-viewmodel:$koin_viewmodel_version"
All of these dependencies I would like not to be declared also in the app build.gradle file.
I am reading some blogs and the Gradle official documentation but I am not able to successfully achieve this.
What I want
I want to "inherit" all the dependencies just from the core module gradle file
The structure of the project should be something like this:
core
|_build.gradle (common dependencies)
|_app
|_build.gradle (just specific dependencies)
What I have tried
I have read that this is possible implementing the core project as follows (in the app build.gradle file):
dependencies {
implementation project (":core")
......
}
But I have some classes in the app module that uses core classes` that could not find. For example:
import com.example.core.presentation.base.BaseViewModel
The error says that could not resolve the reference like it not exists.
Some ideas?
Thanks in advance!!
I took a look at your GitHub :-)
I think this is the example you are trying
https://github.com/smoralb/BaseApplication/tree/feature/core?
What you describe in your question should work, so I took a look.
The problem is in your core modules build.gradle line 1
(https://github.com/smoralb/BaseApplication/blob/feature/core/core/build.gradle#L1)
You have this:
apply plugin: 'com.android.application'
When you should have this:
apply plugin: 'com.android.library'
then using
implementation project (":core")
inside your app module, will give you access to the classes from core in app.
Simplest way to achieve this is with a convention plugin. Don't let the plugin part scare you too much, it's a bit of setup but not that complex.
You get 3 options there:
put the plugins in a buildSrc dir and have gradle pick it up automatically
put the plugin in a specific module and include the build (composite build)
put the plugin in a specific module, release it and load it as a binary dependency plugin.
more info can be found here:
https://docs.gradle.org/current/samples/sample_convention_plugins.html
alternatively, if you just want to maintain the versions in 1 place but don't mind putting the dependencies in both build gradle files a plaform might be even easier: https://docs.gradle.org/current/userguide/java_platform_plugin.html

Why dependency defind in lib module is not visible in app module?

I have an Android application. It contains two modules of app and pax-lib. app module depends on pax-lib module.
I have libs folder under pax-lib that contains some jar files. I have linked them in to gradle file of this module and use it across this module without any issue. This is how I have defined them:
dependencies {
...
// Local libs not in Maven Central
implementation files('libs/commons-io-1.3.2.jar')
implementation files('libs/commons-lang3-3.2.1.jar')
implementation files('libs/httpclientandroidlib-4.3.0.jar')
implementation files('libs/Kahuna_442.jar')
implementation files('libs/mapquest-android-sdk-1.0.5.jar')
...
}
This is how I defined this dependency in gradle file of app module.
dependencies {
implementation project(':pax-lib')
...
}
I am able to use all classes I have defined in pax-lib without any issue, however, I am not able to use .jar files that have defined in Gradle file of pax-lib module. My expectation is to be able to use them as I was in Gradle version below 3.0.
I must be able to copy/paste these jar files under app module but I want to make sure I am not doing something wrong first.
Use api rather than implementation

How is "libs" created and maintained in a gradle android project?

I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project.
Note: I'm using maven dependencies.
My question: Are people using custom scripts to generate this folder? I hardly think that this is generated once and then manually maintained when there is a newer version available.
Please enlighten me!
With Android Studio AND Gradle, there is no need to use libs folder (except for old .jar library).
In fact you can develop Android app whitout Android Studio as in your build.gradle there is already a apply plugin: 'com.android.application'
Gralde is using Maven or jCenter via gradle dependencies to import libraries. Gradle and Android Gradle plugin will automaticly download the libs as you sayed in a build/ folder. It is not static and can be clean with the Clean projet on Android Studio. Also, Android Studio will add a warning when a new library version is available automaticly in your build.gradle.
Dont miss the old libs folder used to import .jar library
I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project. Note: I'm using maven dependencies.
Don't confuse the libs folder with build/intermediates/pre-dexed/ folder.
Currently the gradle plugin for Android manages the build process and create these "internal" and intermediates folders.
My question: Are people using custom scripts to generate this folder?
I hardly think that this is generated once
You don't have to create this folder. The Gradle plugin for Android manage it for your. Also it will be deleted and recreated when your run a gradlew clean command.
and then manually maintained when there is a newer version available.
No. Your dependencies are defined in your build.gradle file.
When you define a new version, Gradle downloads the new dependency and updates the intermediate folders.
You can define your dependencies in many ways:
dependencies{
//You can create a folder and put jar files inside. You can use your favorite name, usually it is libs.
compile fileTree(dir: 'libs', include: ['*.jar'])
//The support libraries dependencies are in a local maven managed by SDK
compile 'com.android.support:appcompat-v7:23.0.0'
// A Maven dependency
compile 'com.squareup.picasso:picasso:2.5.2'
//A local library
compile project(':mylibrary')
//An aar file. It requires to define a repository.
//repositories{
// flatDir{
// dirs 'libs'
// }
//}
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}

Best way to add dependency for Wire using Gradle in Android Studio

I'm using Square's Wire library for my Android app, using Android Studio with Gradle.
I originally added the wire-runtime-1.2.0.jar into a libs folder in my module, and added the dependency to Gradle like this in my build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
That worked fine.
I'm new to Gradle and Android Studio, but based on the way I'm depending on the Google Support and Play Services libraries, I thought I might be able to remove the wire-runtime-1.2.0.jar library from my repository and just declare a dependency like this (the line is from the Maven repository):
dependencies {
compile 'com.squareup.wire:wire:1.0.0'
}
But if I do that then I hit this error:
Gradle: package com.squareup.wire does not exist
Is there a way to set up this dependency without importing the JAR file directly? Or does that only work for libraries that you can install through the SDK Manager?
Some packages, like com.squareup.wire, have multiple artifacts in Maven Central. You need to choose the right one for your needs. In this case, the equivalent of wire-runtime-1.2.0.jar is the wire-runtime artifact, not the wire artifact.
Here's what your dependencies section should look like:
dependencies {
compile 'com.squareup.wire:wire-runtime:1.2.0'
}

Proper way to add global library in android-studio/gradle

First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.

Categories

Resources