Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Java Project in Android application?

Tags:

java

android

xml

Can I use a Java Project in an Android Project even if some classes from the java project are not recognized in a normal android project? For example javax.xml package?

There are 2 possibilities as I see it:

  1. Either create a jar with that java project and import it in android
  2. Or reference the project in android(as i understood it's possible).

But in either way, will those classes that are found in java but not in android be ok in my Android Application? Will this work? Thank you. Printscreen from build path of my android app

Printscreen from build path of my android app

I made the JavaProject, and then imported it in my Android Project as a reference. I get an error i did not expect, it does not recognize my .classes from my Java Project.

like image 314
Gabi Radu Avatar asked Feb 03 '12 13:02

Gabi Radu


People also ask

How do I export and import an Android Studio project?

Solution: Export to Zip File… (The new way of exporting Android Studio project) Export to Zip File… is a new tool available for us in Android Studio 3.0 onwards. You can use it via menu: File | Export to Zip File…

How do I run a project in Android Studio?

In Android Studio, create an Android Virtual Device (AVD) that the emulator can use to install and run your app. In the toolbar, select your app from the run/debug configurations drop-down menu. From the target device drop-down menu, select the AVD that you want to run your app on. Click Run .

What is import Android Studio?

Import Samples from GitHub To import a code sample into Android Studio: In the Android Studio menu, select File > Import Sample to open the Import Sample wizard. Select a sample to import and click Next. Specify the application name and project location if different from the displayed settings. Click Finish.


1 Answers

If you are using Eclipse (with the ADT plugin) you can reference the Java project in the build path of your Android project. This will package those classes in your .apk output and allow you to use them in your app.

As you pointed out in your comments; referencing a Java project as an Android library will not work in your case, since the presence of the javax.* packages will result in compile time errors in Eclipse. In this case, you will need to opt for option 1 and build a Jar file from your Java project and include it as explained here. This way, the class files requiring javax.* should not produce any compile/runtime errors unless you try to use them. Either way, using the Java build path will not work as you expect - the class files are not bundled this way.

The Android platform provides some of javax.xml, but not the whole package (read this document for more detail on this). Often the easiest solution is to write an Android equivalent of your affected Java code that does not require the missing dependencies, and bridge the 2 implementations so the correct one is used for each project.

like image 127
seanhodges Avatar answered Sep 30 '22 19:09

seanhodges