Generating .JAR Files From Android Library Projects In Android Studio 2-24-2015

6,810 views
Skip to first unread message

Ernest Holloway

unread,
Feb 24, 2015, 2:33:03 AM2/24/15
to adt...@googlegroups.com
Hi Everyone,

Is there an official Google supported way of being able to simply and quickly compile an Android library project into a JAR file? On Eclipse you could do this easily by building the project as long as your target API was >14. There were some provisions to be made such as removing switch statements, however, the process was pretty painless. 

Please let me know if there is an obvious way to do this with Android studio and if I am missing something. Most of the postings that I have found on Stack Overflow have been to either:

1) Generate an .aar file, which seems to only work best and be fully supported with Android studio. I am not sure if .aar files are fully supported with Eclipse since Google has officially stopped support of Eclipse.

2) Modify the gradle files somehow to get a .jar file to build instead of the .aar file. 

If anyone knows of a plugin or at least a good set of directions to point me in the right direction I would greatly appreciate it. I am developing a library and the .JAR file is a requirement so that folks using Android Studio or Eclipse can still utilize the library.

Thank you all for your help,

-Ernest

Mark Murphy

unread,
Feb 24, 2015, 12:08:29 PM2/24/15
to adt...@googlegroups.com
On Tue, Feb 24, 2015, at 02:33, Ernest Holloway wrote:
> Is there an official Google supported way of being able to simply and
> quickly compile an Android library project into a JAR file?

I don't know about "Google-supported", but I use this task (based on one
published by Jake Wharton):

// from http://stackoverflow.com/a/19484146/115145

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.baseName = "cwac-${PUBLISH_ARTIFACT_ID}"
task.version = PUBLISH_VERSION
task.exclude('com/commonsware/cwac/**/BuildConfig.**')
}

Obviously, substitute relevant stuff for the CommonsWare bits in there.
And, this task is only for library projects sans resources. For a
library project with resources, here's my 17-hour-old Stack Overflow
answer on that subject:

https://stackoverflow.com/a/28685923/115145

(TL;DR: use the Maven plugin to have Eclipse consume AARs, or convert
the AAR into an Android library project)

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 6.4: Your App,
on TV!

SUjH

unread,
Apr 5, 2015, 2:12:27 AM4/5/15
to adt...@googlegroups.com
You can create a jar file using Android Studio. 
Just add these tasks in your app's build.gradle file.

    //task to delete the old jar
    task deleteOldJar(type: Delete) {
        delete 'build/libs/AndroidPlugin.jar'
    }
     
    //task to export contents as jar
    task exportJar(type: Copy) {
        from('build/intermediates/bundles/release/')
        into('release/')
        include('classes.jar')
        ///Give whatever name you want to give
        rename('classes.jar', 'AndroidPlugin.jar')
    }
     
    exportJar.dependsOn(deleteOldJar, build)

Once you add the above lines you can export the jar using the task of the same name.
Reply all
Reply to author
Forward
0 new messages