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:
- Library is a separate module in the project (a multi-module project).
- Library can be stored locally in the project (typically in
libs
directory).
- Library can be deployed into a repository (Maven, Ivy repository).
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.