I've been working on a mobile app for a couple of months. Now I want to develop other apps but reusing the code I've written. I'd like to have reusable code (activities) in order to be used in many projects. In this way, if there is some bug in one of these, I'd fix the bug and then apply the changes to all the projects that use my library. In short words, I'd like to know what is the best way to develop my own library for Android with Android Studio. I've been researching but I couldn't find something useful. In Eclipse I could create a project and make it a library. Then, in the new project, I created a reference to that library. I would like to know what's the best way to achieve something like that with Android Studio.
Creating libraries is a natural part of Android development. Bigger apps are almost never created as a single application without any libraries.
You can either use jar
or aar
libraries (historically apklib
was used with Maven build configuration, but that's currently obsolete).
In Android Studio (Gradle build) you can use libraries in 3 ways:
libs
directory).In multi-module project you add libaries this way:
dependencies {
project(":library-module")
}
If you plan to create multiple apks with a common library you can create a mutli-module project (some modules are libraries, some modules are applications). But I wouldn't recommend that. It's better to have separate projects (in separate repositories) to create multiple apps. Therefore, I would prefer one of the two remaining approaches (preferably number 3).
Locally stored library can be added to project this way:
dependencies {
compile files('libs/some_library.jar')
}
It has the disadvantage that you have to update the local jar (thus making changes to the project) every time you update the library.
Deployed libraries are the best way to use libraries in a project. The libraries are then added this way:
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Using deployed libraries mean that you have a repository. It's preferable to use remote repository, but you need a server for that (unless you create an open source, in that case you can use a public repository like Maven Central or Jcenter). If you don't have a remote repository, you can also use a local repository (available only in your computer), but that's difficult when you cooperate with other developers (everyone has to deploy the library into his local repository).
To deploy a library you need to apply a plugin that capable of doing that (e.g. 'maven'
or 'maven-publish'
plugins) and to add some configuration into build.gradle
.
Some description how to do that can be found e.g. here, here or here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With