0

I tried adding .jar files into my android project as a dependency but it didnt work, so now im trying to add just the java files I need. Lets call the file I want cookies.java, and then within the original package structure there is a util folder with 2 files cookies.java needs to execute lets call them cookieDough and cookieRecipe and the pacakage is located int net.sourceforge.cooking. So it it would end up being 3 files I need:

net.sourceforge.cooking.cookies.java
net.sourceforge.cooking.util.cookieDough.java
net.sourceforge.cooking.util.cookieRecipe.java

what I am trying to do is add a cooking folder with the cookies.java class and inside the cooking folder a util folder with the cookieDough and cookieRecipe classes. so:

cooking/cookies.java
cooking/util/cookieDough.java
cooking/util/cookieRecipe.java

My main program is under

src/main/java/packagename/

I tried creating a java folder withing src, src/cooking but im having trouble getting both the cookies.java to find the /util/cookieDough.java and also for my classes withing src/main/java/packagename/ to find the cookies.java or the other files.

The files im adding already have package and import declarations but since im making them local I know I have to change these, I dont know if I need them to be a package all together.

Questions:

1- where is the right location to add the cooking folder within my project structure.

2- how do I get the files im adding to recognize each other, meaning how do i get cookies.java to find util/cookieRecipe.java?, do I have to declare them as a package

3- How do I get my java classes in src/main/java/packagename/ to find the classes in the cooking directory, an import statement??

It all works if I just add the 3 java files into my main package and remove all import and package statements from them, but I would like to figure out how to do it the right way (best practices) and i am assuming I should have those files separate from mine.

BTW the files I'm adding are opensource.

Thank You

Jack
  • 491
  • 7
  • 27

1 Answers1

0

I tried adding .jar files into my android project as a dependency but it didnt work

What happened led you to know that it "didn't work"? This can be a more important question leave to be solved, and may be the "best practise" you mentioned in the end of your question.

But for the other question about putting that .java file directly, the original question seems a little mess. You just remember to put every .java file in the directory indicated by that file's package statement relative to src/main/java.

Take your question as an example, you seems to add net.sourceforge.cooking.cookies.java (which means, a file under directory net/sourceforge/cooking, names cookies.java, with a package net.sourceforge.cooking; statement inside it), then you can create a package under src/main/java to put that file, which indicates that your file can be src/main/java/net/sourceforge/cooking/cookies.java. (Yes, this directory ignores the original directory of your main source in src/main/java/packagename.) Hope this is an answer to question 1.

For question 2, what you need seems just an import statement,

import net.sourceforge.cooking.util.cookieRecipe

in what directory it stays will indicate what package name it has. So you have to declare the package name, but just a package statement in Java as common, no need for other declarations, no other configurations.

And question 3 is the same, you don't need to care that you are dealing with two package trees with no common package, one is net.sourceforge.cooking, and the other is packagename, (or in directory, one is src/main/java/net/sourceforge/cooking, the other is src/main/java/packagename)

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • Hi, thanks for the great answer, the reason why adding the package didnt work I made a post here: https://stackoverflow.com/questions/47514818/dex-error-when-i-include-jar-files-into-my-android-project – Jack Dec 02 '17 at 20:17
  • About question 1 I wanted to cut out the begining (net.sourceforge) in my project and just start it from the cooking folder, I was able to do it like this using the method you described, I dont know if it is ok to cut it out or if it is more proper to leave it with its original path(best practices question), I just felt it would make it clearer not to have the net.sourceforge which doesnt have any files. – Jack Dec 02 '17 at 20:17
  • Also I dont know if I should open a new post about this but, when I add the cooking folder and util folder that goes within it I do it via new->package but I wasnt sure if to use the new->folder->java folder, but then I get a configure component window with a change folder location check box and I dont really understand what its asking. Did I do it the right way? what is the java folder option for? Thanks! – Jack Dec 02 '17 at 20:24
  • @Jack If you want to cut out the beginning, you just simply remove that beginning of the `package` statement of every file you mentioned, then move those file to the new folder (for example, a file `src/main/java/cooking/cookies.java`, with its `package` statement `package cooking`). – Geno Chen Dec 03 '17 at 03:20
  • Yeah I did just that worked great, was just wondering if it was best practice to leave the full path or if it doesnt really matter :D, thanks for the help! – Jack Dec 03 '17 at 10:49