Creating libraries is a natural part of Android development. Large applications are almost never created as one application without any libraries.
You can use the jar or aar (historically apklib used with the Maven build configuration, but is currently deprecated).
In Android Studio (Gradle build), you can use libraries in three ways:
- A library is a separate module in a project (multi-module project).
- The library can be stored locally in the project (usually in the
libs directory). - The library can be deployed to the repository (Maven, Ivy repository).
In a multi-module project, you add libraries this way:
dependencies { project(":library-module") }
If you plan to create several apks with a common library, you can create a mutli-module project (some modules are libraries, some modules are applications). But I would not recommend this. It is better to have separate projects (in separate repositories) for creating several applications. Therefore, I would prefer one of the two remaining approaches (preferably the number 3).
A locally stored library can be added for a project as follows:
dependencies { compile files('libs/some_library.jar') }
The disadvantage is that every time you update the library, you must update the local bank (thereby changing the project).
Deployed libraries are the best way to use libraries in a project. Then the libraries are added as follows:
dependencies { compile 'com.google.code.gson:gson:2.2.4' }
Using deployed libraries means you have a repository. It is advisable to use a remote repository, but for this you need a server (if you do not create open source, in this case you can use a public repository such as Maven Central or Jcenter). If you do not have a remote repository, you can also use a local repository (available only on your computer), but it is difficult when you work with other developers (everyone must deploy the library in their local repository).
To deploy the library, you need to use a plugin that can do this (for example, 'maven' or 'maven-publish' plugins) and add some configuration to build.gradle . Some description of how to do this can be found, for example. here , here or here .