how to create a jar file from android studio - java

I have a fairly latest version of android studio, I have created a module under a project which is basically supposed to be a library, when I build it, it creates an ".aar" file , what I want is .jar file as this library is supposed to be used with eclipse as well.
The library contains activity as well, is there any way by which I can create a .jar file which I can use on Eclipse as well Android Studio ?
I have already created this module and tried building , as a result it generated .aar file and not a .jar file.

I found a way to achieve this, plain simple, using Gradle 2.2.1:
task jar(type: Jar, dependsOn: 'assembleRelease') {
from fileTree(dir: 'build/intermediates/classes/release')
}
Place this in your library module.
It will compile it as release and produce a JAR file in: build/libs.
bash gradlew jar or use your IDE to target that jar Gradle task.

aar is not related to AS or eclipse but is an AndroidARchive for Android applications like JavaARchives are for java applications.
Because Android is java based, jars can be used. But to take android specialities into account, like resource bundles, an aar is the right thing to use.

Currently gradle based builds doesnt seems to be allowing to create jar with android studio, I decided to go for Intelij it has necessary option and does what i want , i can use community edition.
***Update****
Even the jar produced by Intelij with resources don't work , obviously its not easy to have a jar with resource , so decided to opt for .aar and hoping that Eclipse also gets support for .aar when gradle supports lands there, as that was my main concern.

Add this in your library's gradle file:
task deleteJar(type: Delete) {
delete 'libs/traylib.jar'
}
task createJar(type: Copy) {
from('build/intermediates/packaged-classes/release/')
into('libs/')
include('classes.jar')
rename('classes.jar', 'traylib.jar')
}
createJar.dependsOn(deleteJar, build)
You need to check path of your generated classes.jar: build/intermediates/packaged-classes/release/
Create lib folder in your lib's root folder if it is not there
Go to gradle build: Android Studio --> View --> Toll Windows --> Gradle
Select your module(:library) --> Tasks --> other --> Double click on createJar task

You can just simply unzip the aar file to get the separated pieces of files.

Related

Add latest google_play_services_lib for ant build

My android project is using ant build, And I cant upgrade to gradle build since im using gameclosure, a 3rd party game development tool.
Now I need to upgrade my google play services library.
I found that the required modules are:
play-services-base
play-services-basement
play-services-plus
play-services-play
from $SDK-PATH/extras/google/m2repository/com/google/android/gms
So I created individual android lib projects from above modules and added all as library project of main project.
Issue is that, play-services-base libs project whose package name is "com.google.android.gms.base", some of the class is referencing R.class file from package "com.google.android.gms". But correct value is present inside package "com.google.android.gms.base".
How to change to project settings/configs so that this R.class is generated in proper package???
Try to follow the instructions in this related thread. It suggested to try changing the android library reference to a relative path (relative to the current directory) instead of an absolute path. You may also check on this blog.
The Play library has to be included in your project.properties file like android.library.reference.1=../google-play-services_lib
because it has resources that need to be included. However, Ant will fail your build because Google no longer provides a build.xml for the Play library. You can generate the build.xml in the Play library by running android update lib-project --path <playlib_path>.
Hope this helps!

Android Studio: How do I find where Gradle has been installed?

I have installed Android Studio but would like to use Gradle to build a simple "hello world" Java application that I have created outside of AS.
I have added the java plugin to my build.gradle file and ran `"gradle build" to try and build the .java file.
However, I am getting the error: "Gradle is not recognised as an internal or external command"
Do I need to install a separate version of Gradle, not bundled with AS to be able to build a java project?
Do I need to install a separate version of Gradle, not bundled with AS to be able to build a java project?
More or less.
There are two ways of using Gradle:
Download and install it, like you would most other development tools
Have a project set up for the Gradle Wrapper (gradlew and gradlew.bat files in the project root, plus the gradle/ directory containing the Wrapper JAR and properties file)
That latter approach will download a private copy of Gradle for this particular project, into the project's .gradle/ directory. This way, different projects can use different versions of Gradle without conflict.
Android Studio takes the latter approach; you can see the files that I mentioned in a newly-created Android Studio project, for example. For your own Java project, I'd do whatever is natural for your IDE that you plan on using for that project.

How to get JAR from Maven for Android

How can we get jar files/library files from Maven repo. Maven provides AAR file.
I know using Android Studio is best but I am facing some issues with my system, so for some time I have to use eclipse.
I want to get jar file/library project for this project
https://github.com/dmytrodanylyk/android-process-button/
Please suggest me some solution.
You can download the source from here and export in to your project and if you want you can create jar easily.
The code you're trying to get is an Android library, and requires some resource to work. You need to download the res folder too from here and you need to add it to your project res folder.

build jar from loopj android-async-http source

I'm trying to build a jar from the https://github.com/loopj/android-async-http source code but have been unsuccessful.
I also tried including the source as a library in my android project in android studio but was unable to.
I want to build from source because the jar's available in maven and in the github repo are too old and I want to use and test some of the new features available in the source.
How can I either build the jar and include the jar in my android studio project or simply properly include the source in my android studio project?
I was able to build the jar myself in ubuntu:
cd android-async-http/library
gradle build
That generated a build directory that contained a jar of the library.
Do you just need a Jar or you want to build the jar yourself. If you are just looking for the jar, they are already compiled and ready to download on this link here.
Looks like github download is not working. Use this link to download the latest android-async-http jar

Override target in Ant build script to copy a jar file to libs directory

I currently have a project in a directory named bbct. This contains subdirectories swing, with the Swing version, android with the Android version, and common with code that (possibly) is shared between the two. The swing and common subdirectories are organized as NetBeans projects with nbproject, src, build, and dist subdirectories. The android subdirectory is an Android SDK project with src, res, and gen subsdirectories.
Now I need to make sure that, when I build my Android project, the JAR file in common is built via an Ant script. I believe I can add the necessary information to my android project's Ant script to do this. (I haven't tried yet.) I also need to access the classes in the generated bbct-common.jar from my Android project and included these classes, or the JAR file itself, in the APK file generated for my Android app. How do I do this last part?
Update:
From Ant build scripts, antcall, dependencies, etc, I learned that I can use the <ant> task to make sure my bbct-common.jar file is up-to-date. I also learned from How do I reference external jar files in a common directory (not libs) to build android project using ant?, How to specify lib folder for JARs when using Android-generated ant build file?, and How to build an android app with external libraries using ant? that I need to copy the JAR file to bbct/android/libs. Now my question is, what target do I override in my bbct/android/build.xml file to do this? In particular, I would like to override a single target so that both the debug and release builds call it. Is there a common dependency to these two targets where it is common to do these kinds of tasks?

Categories