How to create reusable actions in Android?

I have been working on a mobile application for several months. Now I want to develop other applications, but reuse the code that I wrote. I would like to have reusable code (actions) to use it in many projects. Thus, if there is an error in one of them, I fixed the error, and then I applied the changes to all projects using my library. In short, I would like to know what is the best way to develop my own Android library with Android Studio. I do research, but I could not find anything useful. In Eclipse, I could create a project and make it a library. Then in a new project I created a link to this library. I would like to know what is the best way to achieve something similar in Android Studio.

  • Is it possible to add my own library to gradle?
  • Is good practice creating a jar and adding it to build.gradle in my projects?
  • If you think in the Android Library module (Android Studio), how can I use it in many projects?
  • Do you offer another alternative?
+7
android android-studio android-gradle gradle
source share
1 answer

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 .

+3
source share

All Articles