Hey I'm having a pretty basic problem importing classes into my Eclipse based Android app. I have included the class directory into the /src directory where I figured the classes would be easily included, but Eclipse is saying it "cannot resolve the classes".
One thing I noticed is that the project folder that I included and the subfolders are just regular folder and not package folder icons. Is that a problem? If so, how do I change them to packages? This is an external git repo, so I'd rather not do anything beyond just including them in my project.
Here are some pics of how I set up the project to help:
The Activity where the error is:
Here is the package structure in the project, and the package called "android-utils" where the files I'm trying to import live:
Here is how I'm importing the files:
Btw, these classes are just some utils and I'll be improving and adding to them during the development process.
Let me know if you need any in more info to help me get these files imported the right way. Thanks!
Did you try dragging the .java files from Windows Explorer directly onto the package in Eclipse you want to put the new classes into? It should then ask to copy or link to files.
As your folder contains only java files Add them as Jar File in your Buildpath project, and your problem will be solved.
Also your jar file you can put it under the /libs folder
see this link to lean how you create jar file
Related
I want to use the API from Maps.Me. I've
downloaded the repository from github
extracted the files
copied the lib folder
created a libs folder as ...AndroidStudioProjects[MyApp]\app\libs
pasted the lib folder into the libs folder
The instructions on github say I should then "add the library project to [my] project". My problem is that all the advice I can find here and elsewhere online assumes that I will have a jar file which I will now see in Android Studio. I don't have a jar file and I can't see a libs folder anywhere in the Project tab on Android Studio.
(The libs folder that I have created contains the folders and files that I would expect to see in an Android Studio project folder, namely res and src folders and AndroidManifest.xml, build.gradle, build.xml and project.properties files)
What do I do next?
One thought is that I should import the downloaded library as a new project into Android Studio and then create a jar file from it. Would this work? Perhaps there is an easier way?
In case it helps anyone coming to this thread in future, I'll describe how I have worked around this issue.
As can be seen from the comments, I failed to import the library into my app in Android Studio, despite all the helpful guidance from CommonsWare. If I were more familiar with Android Studio, and Gradle specifically, I hope that I would have been able to succeed. However, I have followed a route which, for me with my limited experience of Android Studio, is more straightforward.
For each class in the downloaded repository I added a new Java class to my app with the same name.
I did not include one of the classes which included a dialog and relied on a layout and other resources, since I knew that I would not need this class
I copied all the code, except in each case for the package name, from each class in the depository and pasted it into the new classes.
I removed from the code that I did copy the one reference to the missing class
the result was code that worked exactly (so far as I know) as if I had imported the library.
Obviously, the result is inelegant, but for an app that is solely for my personal use it is good enough. It works.
I copied the code onto my computer via
svn checkout http://desk-clock.googlecode.com/svn/trunk/ desk-clock-read-only
into the folder C:\Programming\desk-clock-read-only
I try to create the project via File/New/Other/Android/Android Project from Existing Source.
I select the C:\Programming\desk-clock-read-only path.
For some reason eclipse only wants to import C:\Programming\desk-clock-read-only\res.
The source files are in C:\Programming\desk-clock-read-only so they don't get imported.
There's just an empty src folder.
What do I have to do to import the project correctly?
I dont't know how the developer of this project managed it to run this but usually the java files belong inside the src folder and the manifest definitely not inside the res folder. If you change this Eclipse(or the IDE of your preference) might recognize it, but you'll maybe get some errors because just putting the java files inside the src folder shouldn't solve it. You actually need a package. Check your manifest for that.
I have an android library project (say A) in eclipse that is trying to reference another library project (say B). Both projects have res folders, and what I see is that A is unable to reference anything in B's res folder, basically R.java can't be resolved (no strings, no layouts etc).
Am I missing something crucial? Is this even possible?
Thanks!
You could just export one of your library projects as a jar file.
Then create a new folder name "libs" (name is important) and then import that jar file into the project. As long as you've included the right files when creating the jar file, you should be able to reference those files from the second project.
Now when it comes to manipulating files in the res folder, that might get trickier. If you are trying to do any UI stuff where the resources are in the library project, then I'm not sure if this is possible.
Hopefully this helps!
Cheers!
----------------------- Update ------------------------------
I'm not sure where you are going wrong. If you do what I suggested, make sure that you add the "gen" folder when creating the jar file. Then make sure you add the right import, and voila you can reference that file.
For example: say your library file has the package com.test.Test1
then in your gen folder you should have the package com.test.Test1 with your R.java file in it.
Export this project making sure to include that gen folder.
Then in your other project make sure you include "import com.test.Test1"
now in code you can just type R.Whatever. There might be some confusion if you are including another import for R.java.
Now if you want to access the "res" folder, I would imagine you could just change the res folder into a source folder (Build Path->Use as source folder) and then export the jar making sure to add the res folder.
Hopefully this works for you.
Cheers
You should use the full namespace before the R class. For example if the name space on project B is com.projectB.* then you can reference the R from project A by referencing R as
com.projectB.R.xxxx
In my project I have added some folders that I want included in my project for source files. But no matter what I do, the source never gets recognized. I tried refreshing, clean up, restarting but nothing works. Here is the project's source folder:
I want the folders integration/android added. This is how it is showing up in Eclipse:
And here is what my build configuration looks like:
I could be wrong, but it looks like you have added them as a folder, but not actually added the folder to your project.
The folder should show up in your src folder in the solution explorer window under your other package.
Try creating the package name in your project, then move all the src files into it.
And when its in the src folder, in its correct package, you dont need to include any extra build paths.
(You can have multiple packages in a single application)
Importing someone elses project that is over 2 years old is almost always problematic. I found the easiest solution just to create a fresh project and add the files manually from the old project.
I'm working on an Android project and i need to import an XML library called XOM. The only way i found was to right click on the project name -> Properties -> Libraries tab -> click on "Add external Jar". But that only references it from it's original path, what happens when i compile the app and move it to my phone? Will the Jar be in there?
Might be a stupid question, sorry if that's the case.
It's fine to do that but what I normally do is create a folder called liband then add Jars from there. I find that makes it easier to see what Jars I have in my application.
The main disadvantage of your original approach is that what if the path to that library externally accidentally changes? then your program will not compile. It's better to have everything in the one place to make things easier.
.apk is nothing but a collection or zipped version of a file. it contains all the resources and compiled java files.
So you need not have to worry about the inclusion of your library files. if you have set proper path to your libraries in eclipse then these will be included in your apk file.
Create a directory called "libs" in your under your root directory. You can do it by,
Right click on the project which is in the Package Explorer window in
Eclipse.
New -> Folder -> give "libs" as the Folder name
Copy your jar file and paste it to in the "libs" folder
Expand the "libs" folder
Right click on your copied jar file -> Build Path -> Add to build
path
That's it