Monday, March 20, 2017

How can we create a single library by combining different modules?

Leave a Comment

I want to build a single library from my project , the project uses many dependencies and resources. If cardview is imported as a jar, it won't have the resources with it, but when it is added as aar, it is then added as another module. So i can't combine it into a single aar library. I don't want to submit my project into repository. How can i build the library , thanks in advance!

3 Answers

Answers 1

android-fat-aar should handle this issue for you.

You have to

apply from: 'fat-aar.gradle' 

from the GitHub repository and then replace your compile dependencies with an embedded dependency. So instead of definining a module otherModule dependency as

dependencies {     compile project(':otherModule') } 

you'd only have to define it as

dependencies {     embedded project(':otherModule') } 

The compile configuration depends on the embedded configuration, so you'd have the same behavior locally like before.

But in the end this is a problematic configuration, especially if you want to use some of the Android Support libraries. You'd most likely run into duplication issues.

If you're creating this setup for offline work, better consider to use a Local Maven repository

repositories {     mavenLocal() } 

with a copy of the library artifacts you need to use. Or maybe even a personally managed repository for yourself and anyone else who needs it.

Answers 2

Are you aware you can use a module as a library (sources, no compiling an AAR)? This will mean your library-project can use all gradle dependencies. Here is an example with a library project called 'MyApiLibrary' which will be a standalone module called 'api'

First create your library as a new project (with the module name 'api' for this example, but the name can be whatever you like). In the gradle file for that library module use the library plugin where you would usually use the application plugin

apply plugin: 'com.android.library' //not apply plugin: 'com.android.application' 

Also make sure the library module does not declare an applicationId in the defaultConfig of its gradle file. Then declare all of the library dependencies as you ordinarily would.

Then in your consuming project import the library by including the following in settings.gradle:

//this first line declares a pseudonym to refer to the library module include ':api' //this second line explains where the pseudonym links to  //(the path to the module directory of the library project, relative to the root dir of the current project) project(':api').projectDir = new File('../MyApiLibrary/api/') 

In your consuming module's gradle file, reference the project as a dependency:

dependencies {     ...     compile project(':api') } 

Now everything inside your main project can access all of the classes and imported dependencies of the Library project

Answers 3

If I get it right, you have a multi module project, which has several library projects, and you want to compile a single aar which encompasses your modules and their dependencies. If so:

-Install maven. -Apply the gradle maven plugin (https://github.com/dcendents/android-maven-gradle-plugin) to your modules. use the install task to install the modules as single libraries.

-Create a new module, which imports all the installed versions of your single libraries, and their dependencies. Apply the android shade plugin (https://github.com/zawn/android-shade-plugin , documentation here: http://imperceptiblethoughts.com/shadow/) to this module. The output of that will be an uber-aar, and aar containing your library modules, and their dependencies. You can install this into your local maven instance, or drop the aar into the libs directory in your apps.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment