Skip to content

Instantly share code, notes, and snippets.

@kellyfj
Last active November 8, 2019 20:33
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kellyfj/9666950 to your computer and use it in GitHub Desktop.
Save kellyfj/9666950 to your computer and use it in GitHub Desktop.
Using the Android Library plugin for Gradle - create a .jar file (in addition to a .aar library file) that contains all the classes (and dependent jar files). Solution is based off a combination of solutions from https://gist.github.com/Lien/7150489 and the Winning answer to this Stackoverflow Question http://stackoverflow.com/questions/19307341…
android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equals(com.android.builder.BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
//Include Java classes
task.from variant.javaCompile.destinationDir
//Include dependent jars with some exceptions
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);
}
@skrtrifork
Copy link

The location of BuilderConstants moved in 0.11 to: com.android.builder.core.BuilderConstants.DEBUG

@nberserk
Copy link

is there any way to add resources files ?

@danieloskarsson
Copy link

@nberserk if you want to include resource files create a .aar instead of a .jar

@laszlourszuly
Copy link

Maybe even consider changing "jar${name.capitalize()}" to "jar${variant.name.capitalize()}" in order to support multiple productFlavors.

@galvanu
Copy link

galvanu commented Jan 12, 2015

Thanks for the code, it works.

Can you please help me with the following scenario:
If I have one project with multiple modules (libraries) inside and I want to create a jar for each module.
I mean in eclipse each project that was marked as a 'library' automatically generates a .jar file when the project was built.
What should I do if I want to achieve the same behaviour? Should I need to put this code in each build.gradle file?

Thanks!

@Brunoclas
Copy link

Brunoclas commented Nov 8, 2019

I want to delete AndroidX from big jar, how can i do?

android.libraryVariants.all { variant ->
        def name = variant.buildType.name
//        println "CONFIGURATIONS: " + name
        if (name == 'debug') {
//            println 'Skiping debug builds.'
            return
        }
        def task = project.tasks.create "lib${name.capitalize()}", Jar
        task.dependsOn variant.javaCompiler

        //Include Java classes
        task.from variant.javaCompiler.destinationDir

        //Include dependent jars with some exceptions
//        variant.getCompileConfiguration().files.each { println it.absolutePath }
        task.from variant.getCompileConfiguration().files.findAll() {
            it.absolutePath.startsWith(rootProject.projectDir.absolutePath) &&
            it.getName() != 'junit:4.12' &&
            it.getName() != 'appcompat:1.1.0' &&
            it.getName() != 'espresso-core:3.2.0'
//            !it.getName().startsWith('runner:1.2.0')
        }.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        artifacts.add('archives', task)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment