0

i've been searching all the web for a definitive solution for this simple problem of adding an external directory with .java files and .jar libraries to my Android Studio project.

Here's what I want to do:

Create a default project in Android Studio, and import from git the library I will work on, which is a bunch of .java files and a .jar (jsoup). These java files need this jsoup. The java files are not for Android, just simple java files.

I need it to be from github because I'm working on this lib and I need AndroidStudio to automatically update it to its newest version.

This is how I compile my project:

javac -cp ".:jsoup-1.8.3.jar" Main.java Class1.java Class2.java; 

Then I run:

java -cp ".:jsoup-1.8.3.jar" Main

Well, I already loaded this library from github using Android Studio, so now I have:

Project/app/src/main/java/com.myname.myapp

Project/MyLibraryFromGithub

Of course I seen answers like this and I followed everything, but there are some problems. For example, it kept searching for a AndroidManifest.xml file which I don't know how to write because it's a simple java project, not a Android Project. Also, I think this answer is outdated, and won't work properly in AndroidStudio 2.0.

I've read some tutorials on Gradle but it won't say anything about external folders and so. Also, when I make it happen, how do I import my library? For example, in

Project/app/src/main/java/com.myname.myapp/Main.java

How do I import the library? import myLibrary? Or I just need to create new objects with the classe's names? Like Class1 myObject = new...?

This is so congusing! I whish I could compile everything by hand, Gradle is hiding too much from me and I can't find a good tutorial.

Community
  • 1
  • 1
Gatonito
  • 1,662
  • 5
  • 26
  • 55

2 Answers2

2

To add an external jar to your project you should do the following steps:

  1. Select File >> New >> New Module.
  2. Select Import JAR/AAR Package.
  3. Give the file name in the File name text box and click on Finish.
  4. Select File >> Project Structure.
  5. Select app under the modules section on the left hand side.
  6. Choose Dependencies tab at the top.
  7. Click on the + on the right hand side.
  8. Select Module Dependency
  9. Select the JAR file that you have just imported.
  10. Close both the open windows by clicking OK
Aswin P J
  • 546
  • 4
  • 13
  • but it's not only jar, it's a lot of source files too!! – Gatonito Apr 23 '16 at 03:00
  • I would say that you compile all the dependent files into a single jar and then proceed. See this stack Overflow answer. http://stackoverflow.com/questions/9941296/how-do-i-make-a-jar-from-a-java – Aswin P J Apr 23 '16 at 03:04
  • but I wanted to use gradle because its purpose is to build everything with one click :c – Gatonito Apr 23 '16 at 03:07
  • :) You may use gradle. But I am a beginner and this is the approach that I have found to be very easy and error free. All the best :) – Aswin P J Apr 23 '16 at 03:09
  • By the way.. All the above step are one time. There is no need of doing it again and again with every compilation. So I also dont see the point is using Gradle :\ – Aswin P J Apr 23 '16 at 03:10
0

Have you tried this?

  1. Place X.jar into the libs folder

  2. Right click on X.jar and 'Add as Library'

  3. (In build.gradle) Place this in dependencies: compile files('libs/X.jar')

Vanilla
  • 51
  • 1
  • 3
  • 9
josedlujan
  • 5,357
  • 2
  • 27
  • 49