Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse modules in Android Studio

Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.

Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?

like image 439
Abhishek Kumar Avatar asked Nov 14 '15 16:11

Abhishek Kumar


People also ask

Can we import module in Android Studio?

Import a module To import an existing module into your project, proceed as follows: Click File > New > Import Module. In the Source directory box, type or select the directory of the module(s) that you want to import: If you are importing one module, indicate its root directory.

What is AAR file in Android?

AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods. AAR files can contain C/C++ libraries for use by the app module's C/C++ code.

What is multi module in Android?

A project with multiple Gradle modules is known as a multi-module project. In a multi-module project that ships as a single APK with no feature modules, it's common to have an app module that can depend on most modules of your project and a base or core module that the rest of the modules usually depend on.

Where is the AAR file in Android Studio?

Change Project structure from Android to Project. Then paste "aar" in libs folder. Click on File at top left of android studio and click "Sync Project with Gradle Files" as below. That's it.


1 Answers

You have different ways to achieve it:

  1. using a local module referring the right path
  2. adding an aar file
  3. using a maven repo

CASE 1:
Using gradle and a local library, inside a project you can refer an external module.

Just use:

Project
|__build.gradle
|__settings.gradle
|__app (application module)
   |__build.gradle

In settings.gradle:

include ':app' 
include ':myLib'
project(':myLib').projectDir=new   File('pathLibrary')

In app/build.gradle:

dependencies {
    compile project(':myLib')
}

Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.

CASE 2:
Compile the library module, get the aar file, and then add to the main project:

Add the folder where you put the aar file as repository:

repositories {
    jcenter()
    flatDir {
        dirs 'libs'
    }
}

Then add the dependency:

dependencies {
    compile(name:'nameOfAarFile', ext:'aar')
}

CASE 3: Another way is to to publish your module into a maven repository.

In this way, just use:

dependencies {
    compile ('mypackage:mymodule:X.Y.Z')
}
like image 191
Gabriele Mariotti Avatar answered Oct 14 '22 11:10

Gabriele Mariotti