Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create reusable activities in Android?

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.

  1. Is it possible to add my own library to gradle?
  2. Is it a good practice generate a jar and add it to the build.gradle in my projects?
  3. If you are thinking in an Android Library module (Android Studio), how can I use it in many projects?
  4. Do you suggest any other alternative?
like image 857
Camilo Sacanamboy Avatar asked Apr 04 '15 19:04

Camilo Sacanamboy


1 Answers

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:

  1. Library is a separate module in the project (a multi-module project).
  2. Library can be stored locally in the project (typically in libs directory).
  3. 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.

like image 180
Tomik Avatar answered Oct 07 '22 00:10

Tomik