How to include a Java project in an Android project? - java

I am using Eclipse to develop an Android board game.
I have developed the UI in an Android project.
On the other hand, I have developed the AI in a regular Java project, because I wanted to be able to test it without all the constraints of the Android emulator (I didn't find any other way to run the code using my Windows JVM).
Now comes the times when I want to 'join' both projects (which work fine independently), that is to use the AI Java classes from the UI.
This is what I have tried:
In my Android project Properties > Projects References, I ticked the Java project.
This allows me to build without error the Android project (which instantiates an AI object).
But it fails at runtime with this error:
Could not find class 'my.package.AI', referenced from method my.otherpackage.UI.onStart
What would be the correct way to include the AI Java Project into my Android Project ?
(NB: I still want to be able to develop and test the AI as a regular Java app, so I think using a jar or copying all the sources to the Android Project would not suit my needs)
EDIT:
I was hoping that the new ADT v17 would solve the problem, but it didn't. I have tried virtually every option available to include a project in my Android project:
Adding or linking a source folder
Adding a Project (Java Build Path > Projects tab)
Adding a Class Folder or a Library (Java Build Path > Library tab)
Ticking all the previously imported projets/libraries as Exported
The only way that allows compiling and running without error is adding the JAR of the Java Project to the Android project build path.

Projects References only add the dependent project source code (your AI project) as a soft (perhaps weak is a more accurate word) reference, your AI project is not added into you Android Project Build path. so the actual ai.jar is not exported into the final apk when Eclipse build your app.
You should add AI project into Android project build path:
Right-click on your android project, select Build Path - Configure Build Path, in Projects tab (Required projects on the build path), add your AI project here.
Then in Order and Export tab (Build class path order and exported entries), tick your newly added AI project appeared int the list.
Step 2 is probably optional, this should add AI project as a reference in Android project and export the ai.jar to final.apk when Eclipse build your Android project.
Update from ADT 17.0.0:
Android Dev Team just release SDK r17 with ADT 17.0.0, which claims to handle this use cases properly now:
Eclipse specific changes
The dynamic classpath container called “Library Projects” has been renamed to “Android Dependencies” as it now contains more than just Library Projects.
The container will now also be populated with Java-only projects that are referenced by Library Projects. If those Java projects also reference other Java projects and/or jar files they will be added automatically (jar files referenced through user libraries are supported as well).
Important: this only happens if the references are set to be exported in the referencing project. Note that this is not the default when adding a project or jar file to a project build path.
Library Projects (and the content of their libs/*.jar files) is always exported. This change only impacts Java-only projects and their own jar files.
Again, duplicates (both projects and jar files) are detected and removed.
More in this link.

Edit: After much testing, reading and found my solution. Problem is (as of this writing) that you can't reference another project from an Android project, although it works fine for a normal Java application project.
Workaround for me (Windows with NTFS filesystem):
In a prompt (admin rights) make a symbolic folder link using mklink /D command pointing to your source project folders, and refresh in Eclipse as needed.
The obvious limitation is you cant use the same package names in source project and target project, and then there is the problem with libs in the common project.
example:
cd \java\workspace\AndroidProject\src\your\package
mklink /D common c:\java\workspace\CommonProject\src\your\package\common
Clarification: You can reference another project from an Android project to make it compile, however when you run, the referenced classes from the included project are not put in the apk classes.dex (verified with dex decompiler).

Using ADT 21.0.0 64bit on Windows 7 64bit. I tried adding project to build path. Ticked in the Order and Export, played with order. I read the official solution, the detailed explanation of that solution, exported as JAR, added to libs. Did not work, while the source was without errors. Read in the comments that the solution didn't work for everyone.
Was fed up with it and tried a simple but somewhat messier solution: linked the Java project's source to the Android project. That did the trick. Hope that this will help others too.

Don't add it to the project references area, instead go into the Android pane (in the Project Properties), scroll down to the bottom, and add it under Libraries.
This sometimes works but sometimes produces bizarre Eclipse issues (not sure why), in which case copying the jar directly into the project seems to work best.

Related

Stuck with first time java for android

guys, with my friend started working on a school project - developing a java app for android. He started it, now it's my turn to do my job, so i got the code he already has, set up Android SDK for Eclipse and downloaded all the neccessary files and packages, but it still gives me errors and I can't even run the project.
One of the errors (the main one, I think) is that it can't import android.support.v7.app.ActionBarActivity
If anyone knows what can I do (I've tried all kinds of solutions from the internet, but none work) I would be very grateful.
Here is a screenshot of the code
http://s17.postimg.org/8aw952lha/Capture.jpg
See if you have imported the ActionBar project to your Eclipse workspace, also, right click on your project->properties->android and check that you have referenced/linked the ActionBar project there.
If you are using a repository without the proper ignore files it is possible that you have imported your partners configurations and then have the references to the auxiliary projects broken (you just need to update that).
Hope it helps.
android.support.v7.app.ActionBarActivity is a support library. This allows older versions of Android (before native support for an action bar was available) to use the action bar.
Since you are having difficulty importing it, I'd assume you do not have the support library installed.
Full instructions are available here:
http://developer.android.com/tools/support-library/setup.html
A snippet for adding support libraries to Eclipse:
Make sure you have downloaded the Android Support Library using the SDK Manager.
Create a libs/ directory in the root of your application project.
Copy the JAR file from your Android SDK installation directory (e.g., /extras/android/support/v4/android-support-v4.jar) into your application's project libs/ directory.
Right click the JAR file and select Build Path > Add to Build Path.

Eclipse ADT appcompat... what is it?

I've spent all afternoon getting absolutely nowhere with this.
I've downloaded Eclipse, downloaded the SDK, installed the updates, but every new Android project I create something's wrong.
Firstly, it would not generate the R.java file, at all, now it does but there's a separate project it's created automatically called appcompat_v7. I don't know what this is, but it's causing problems with any other new project.
This is the error a normal project produces:
The container 'Android Dependencies' references non existing library '/home/omar/workspace/appcompat_v7/bin/appcompat_v7.jar'
I have absolutely no idea how to fix this. What is causing this?
EDIT
It appear this is only with KitKat, every other API platform doesn't produce ANY source files at all.... any idea how to combat this?
It's a support library which presumably your project refers to. You will need to build it as a library project in your workspace. This process is described here Support Library Setup under Adding libraries with resources.
You will find the project you need to copy in your SDK in the folder:
\yourSDKlocation\tools\android-sdk-windows4.4\extras\android\support\v7\appcompat
(It's no use just copying a jar, you must build it as a library project.)
I had this problem when I moved a project to a different laptop. I solved it like this:
If appcompat_v7 is not available in Eclipse:
From the File menu, choose New then Project.
Next, choose Android and Android Project from existing Code, then click next
Browse to find your appcompat_v7 project folder
Make sure there’s a check mark next it in the ‘Projects to Import’ list
If the appcompat_v7 project folder is not currently in your workspace, select ‘Copy projects into workspace’, then click Finish
Now that the library project is available in your workspace, it can be added to a project:
Choose your project from the Project Explorer and open the project properties (on a mac it’s in the Project menu)
Click on Android in the left list
Next to the Library list, click the Add button
Choose appcompat_v7 from the list
You should be good to go now.

Java - Android - NoClassDefFoundError - "Could not find class" error in Logcat

First of all, I realise that similar questions have been posted a lot here. But I've been through all of the answers I can find to similar problems, and nothing has worked yet, so was wondering if someone could help me.
I'm trying to develop a simple chat app with an Android client and a server app sitting running on my laptop. This includes a regular Java "common" project that holds the class that is used to pass messages via the socket.
First I tried to reference the "common" project in the Android project, but after a bit of reading abandoned that idea.
Now I have exported the jar file for the common project, and imported it into my Android project by copying it into the "libs" folder, and then selecting Build Path > Add To Build Path. I have also gone to Properties > Java Build Path > Order and Export and checked the library and moved it to the top of the order.
I have also moved the "gen" project above the "src" one in that build order.
Android Private Libraries is also checked. Everything is checked.
When I debug as an Android Application to my Samsung S3, I see a "Could not find class error" in logcat when advancing to the Activity that contains a reference to a class in the "common" project. Stepping through to the point where a class in the "common" project is referenced then causes a ClassNotFoundException to be thrown.
If I try to put the name of the library in the Android Manifest, like:
<uses-library android:name="com.johndarv.chatproj.common" />
I get:
Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY
Please check logcat output for more details.
Launch canceled!
Upon trying to install.
I have the following versions:
Eclipse Standard SDK 2.0.0
Android SDK 22.3
Android project is compiled with Android 4.2.2.
Any help would be much appreciated! If I can give any more info that would be useful, please ask!
The uses-library tag means that your app expects the library to be available on the system. This is to prevent installation on devices which do not have some expected third-party software installed. Consider phone manufacturer who ships his devices with some specific api.
So this is not your case. Forget about this tag this time.
And as to your problem. The jar is not exported into the apk.
Ensure in Project Settings > Java Build Path > Order and Export that the library jar is checked to be exported.
If you put a jar into /libs dir of android project it is automatically added to the build path. You do not need to do this manually (as you did). Then you can see it (this jar) under Android Libraries entry in under Project Settings > Java Build Path > Libraries. This entry is also checked as exported (by default in android project) in Order and Export tab.
Maybe your problm occurs due to the fact that the jar is actually exported twice (once manually and once as private library). Just remove manually added entry from build path. You will have it added once - but to be honest I do not expect this caused the problem. But for sure the problem is in your project settings. Maybe paste a screenshot with Java Build Path > Order and Export and Java Build Path > Libraries.
I managed to solve this by deleting the common project and then creating a new Java project with "Use execution environment" set to Java SE 1.6. And rewriting (copy-pasting) the old classes.
There must have been something wrong with my previous project, and looks like it was the Java version used to compile. I had tried to set the compliance version to 1.6 on the old project. In any case, creating a whole new 1.6 project, pasting the classes in, re-exporting, and copying into my Android project did the trick.
Thanks to everyone who tried to answer this for your help. Hope someone finds the above useful one day!

android-support-v7-appcompat library project won't work

I am trying to add Support Library v7 to my clean android project as support library (with resources). I followed every instruction here: http://developer.android.com/tools/support-library/setup.html#download and android.support.v7.* package is not visible in my main project.
Here is library reference in main project:
Support library project tree:
Support library project build path:
And finally, my main project tree
I don't see any errors in Problems tab, app compiles and runs normally but i cannot import android.support.v7 package which apparently isn't in build path in main project. I went through instruction twice in clean projects/workspace. I cleaned project, restarted Eclipse and nothing... All resources from library project are unreachable too.
Thanks in advance :)
Copy the library project to the folder where your android project is.
Select File > Import.
Select Existing Android Code Into Workspace and click Next.
Browse and import the same to eclipse
Once the library project is imported you can refer the same in your android project.
This is similar to setting up google play services in eclipse described here. Check the 4th step
http://developer.android.com/google/play-services/setup.html
It looks like android-support-v7-appcompat.jar is missing from your libs folder.
When you download the Android Support Library through the Android SDK Manager it makes the support .jar files you need available under {SDK Location}\extra\android\support.
To use the v7 files, copy these to your project's /libs folder.
android-support-v4.jar
android-support-v7-appcompat.jar
android-support-v7-gridlayout.jar (*if needed)
android-support-v7-mediarouter.jar (*if needed)
Typical gotchas (this looks OK in your setup):
Make sure the min SDK is at least API 7
Make sure the project target build is at least API 17
For more information about using the support V7 package (including running the v7 samples) can be found at the RHM Guide to Android Support Lib.
The issue seems to be in Absolute path in case of Windows base machine. I faced the issue of referencing support libraries When I moved Eclipse and related libraries to different drive than where the project was.
Copy paste the libraries to folder on same derive in a way that it can pick by relative path.
..\androidCommonlib\appcompat
android.library

Eclipse Not Finding Classes added to the build Path (Java Dynamic Web Project)

I am taking over a web project from my school. I am trying to deploy the project in eclipse. The project uses a group of libraries (Namely javax.mail.* , com.sun.mail.*, org.joda.* and org.apache.*). The project can not compile because it is unable to locate these. I have them in the src folder and then added the three top level folders/packages to the build path. The import statements work. But getting the joda.time.CLASSXYZ does not work (Then I instantiate the class CLASSXYZ). (The error is "The type org.joda.time.base.BaseDateTime cannot be resolved. It is indirectly referenced from required .class files" the import was joda.time.* so the class should have been imported)
I've looked at the other threads and most of them just explain how to add folders to the build path. I have Apache tomcat set up with eclipse. It works with other projects that I have deployed.
This problem has gotten pretty frustrating as its preventing me from starting the project. Any help you be much appreciated.
Cheers
Unless you use maven you need to download additional libraries:
javax.mail: http://www.oracle.com/technetwork/java/javamail/index.html (also included in Java EE)
org.apache http://commons.apache.org/
org.joda http://joda-time.sourceforge.net/
I suggest to place them in a folder 'lib' and add the jar-files to your build class path. (context menu on your project).
The other threads you mentioned are pointing you in the right direction, and provide a good practice to get in to (especially once you start having more than one project running in your eclipse ide).
So right click project - Build Path - Configure Build Path... , then in the Libraries tab, click Add External JARs and add in the jar for org.joda from the link in the other answer here. This way you can have all your external jar libraries in one spot and reuse them across multiple projects. There's also about a dozen different ways to do the same thing in eclipse, but this is the most direct I think. I'm on version Helios (your's might be slightly different). I try not to use source files of external code libraries unless I want to play around, debugging their code - not a bad thing to do - but I never have the time to spare for that.
Also I'd double check that the other libraries you mentioned aren't being automatically linked in or included as part of the JRE system library. Especially if you are setup in a Dynamic Web Project, it would surprise me if org.apache wasn't already there... but I could easily be wrong there, have been many times before & eclipse was usually involved ;-)

Categories