DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

Android studio, load jar library with transitive dependencies

In my app gradle file I have

compile fileTree(dir: 'libs', include: ['*.jar'])

This allows me to use the classes inside my library jar files. But what if the jar itself contains a lib folder with jar's inside? Like

com.mylib.network/
   classes/
   libs/
        com.otherlib.model.jar

I can do:

import com.mylib.network.*;

but not

import com.otherlib.model.*;

Is it possible to load the nested libs with gradle? Any other ideas?

Upvotes: 1

Views: 258

Answers (2)

Hugo Gresse
Hugo Gresse

Reputation: 17879

Usually if you are running gradle, you could execute ./gradlew jarRelease which will create your project classes.jar and other folder with dependencies (not in jar). Is up to you after to jar the dependencies folder like com/squareup/okhttp.

the jarRelease gradle script is this one :

android.libraryVariants.all { variant ->
    def name = variant.buildType.name
    if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
        return; // Skip debug builds.
    }
    def task = project.tasks.create "jar${name.capitalize()}", Jar
    task.dependsOn variant.javaCompile
    task.from variant.javaCompile.destinationDir

    task.from configurations.compile.findAll {
        it.getName() != 'android.jar' && !it.getName().startsWith('junit') && !it.getName().startsWith('hamcrest')
    }.collect {
        it.isDirectory() ? it : zipTree(it)
    }

    artifacts.add('archives', task);
}

To jar a folder :

# Jar .class dependencies 
@cd outputs/aar/ && \
    jar cf libs/com.jar com/

To unjar a folder :

@cd outputs/jar/ && \
    jar xf myjar.jar

Note : you could also if you are using ressources create a library project compatible with eclipse using a makefile and using data from aar and jar.

Upvotes: 1

sockeqwe
sockeqwe

Reputation: 15919

I don't think that this work with jars (transitive). But you can simply unzip your jar to pull out the other jar and place both jars directly into the libs folder used by gradle

Upvotes: 1

Related Questions