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

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.

Related

how to create a jar file from android studio

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.

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

Using subproject Artifacts in Android Build with gradle

im still quite new to gradle. I decided to transfer some code i oft use into an subproject. This subproject is a pure java project, so im using only the apply plugin: 'java' there.
I can build this project and in the build folder is see a jar which contains the compiled classes after the gradle assemble command was invoked.
What really bugs me at the moment is, how can i use the classes in my Android project using Android Studio ?
I tried to use the compile project command:
compile project(':PureJavaSubProject')
And it compiles the Project as expected. But Android Studio is not able to see the artifacts created by the Project ?
I read a bit in the Gradle Docs about Artifacts management but the Doc is not clear for me.
Anyone can point me how i need to declare or setup the gradle build to make it work ?
Are you including the subproject from your root project in your settings.gradle?
include 'PureJavaSubProject'
I can't verify from Android Studio I'm afraid. There is also more doc on multi project builds here:
http://www.gradle.org/docs/current/userguide/multi_project_builds.html
I had this same issue, and it turned out I had the directory structure for the Java subproject slightly wrong (I had copied it from another existing Eclipse project).
I had:
java-subproject/src/com/example/MyJavaClass.java
...when it should have been:
java-subproject/src/main/java/com/example/MyJavaClass.java
When I changed it to the correct structure, the main Android project then picked up the classes in the Java sub-project fine.
A fully working example of an Android Studio project with a pure Java sub-project can be see here on Github:
https://github.com/barbeau/JavaSubprojectDemo

How to build android project using ant, when i referenced some jre libs

I used eclipse to develop my android app and used JRE System Library in the project. and i can build the project using Eclipse. but i could not build it using ant. it could not found the classes in the JRE System Library. How can i add the JRE System Library to the ant build path?
Eclipse doesn't create all the files you need to be able to run the project from the command-line. Might I suggest http://www.vogella.com/articles/AndroidBuildAnt/article.html as a reference that will guide you through the steps to correctly configure Ant and use it from the command-line.

NoClassDefFoundError on external library project for Android

I use eclipse for Google Android development.
I've created a library project ([x] Is Library in the Android-settings), which includes an external jar-file (Referenced Libraries). This library project are referenced in another Project (the actual project which will use the library project). This is done by add the project under the Android-settings.
the source compiles but if I want to execute it on the device, I get the NoClassDefFoundError for a class which is inside the jar-file which is included in the library project.
Edit: The jar-file ist added to the exported entries ([x] my.jar on the Order and Export-Tab from the library project)
Is there a clean way to get this working?
It has been clearly stated in offcial API here:
A library project can include a JAR library
You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file
The jar lib must be manually added to the dependent application project's build path, not only the library project build path itself.
Update from SDK r17:
This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:
Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)
For those who followed the steps(even check the projects in "Order and Export") and still have the java.lang.ClassNotFoundException in the API 17, the final step is to check that your compiler does not run with Java 1.7. If is 1.7 then you should change it to 1.6 for all your projects. After that it will ask to rebuild all the projects and successfully ran on my phone :)
To change the java compile version in eclipse, this is located in: Project properties > Java Compiler > Compiler Compliance level: 1.6
Go to project properties -> build path-> libraies
If you see your jar files like this
snmp4j.jar - e:\software\jars
Its may your problem
Add libs folder in your project and copy jar file in that folder. Right click jar file and go build path -> add to build path. Then you can see your jar as
snmp4j.jar - project_name/libs
Its worked for me.
I had two projects using the same library: one working, the other one crashing with java.lang.NoClassDefFoundError.
After nothing else helped me, I looked into the file project.properties in the root directory of my project.
The working project had the android.library.reference line (the last line below), the crashing one did not:
# Project target.
target=android-17
android.library.reference.1=../my-library-project
I manually added the line and it started working!
(Yes, I did try both (project) properties -- java build path -- projects and (project) properties -- java build path -- order and exports -- nothing helped.)
PS By the way, "project properties" has also the "project references" tab. No idea if it would work.
I had a minor issue when I upgraded to ADT17 where my libs weren't being imported properly. Turns out this is because my librarys were being linked as dependancies from my lib folder not libs!
Seems librarys have to be in the libs folder from now
I had a similar problem and non of the solutions out here fixed it.
Short version: the JAR was compiled in Java 1.7 and could not be found by Dalvik.
We build an internal JAR that we share across backend and mobile clients with Maven. The problem ended up being that the JAR was compiled with Java 1.7. Androids dalvik only supports Java 1.5 and 1.6. This is why everything works fine in Eclipse as it's not compiled to dalvik at this point.
We even specified the target and source version in the Maven compiler plugin to compile with Java 1.6. This did not work because only JDK 1.7 was installed on the build machine. A small note at the bottom of the Maven page gave us the hint: Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version.
To see if you have this problem as well, rename your *.jar file to *.zip unpack it, and look in the MANIFEST.MF file for the Build-Jdk: parameter to see what Java version actually compiled your JAR.
Another thing to pay attention to is library package names.
If you are using ADT21 and you happen to have libraries that have the same package name, there will be error during compile but it will still compile and run in Eclipse, giving you an APK that is missing some of the resource classes. Hence the crash when you run the app.
If you compile it with ANT then you can see the compile error that says two or more libraries use the same package name.
To fix this, rename your library project by using Android Tools -> Rename Application Package. Then everything will go back to normal.
It took me almost entire day to figure this out...

Categories