Android Studio: How to import java class from outside the project?

14,535

Solution 1

Found a solution that might not be optimal but at least in my case it solved the problem.

In Android-Studio: Create a folder called something like "CommonEclipseAndroid" following a path similar to this one: project/Android/app/src/main/java/com/example/myapplication/CommonEclipseAndroid Paste the code you need to use in both project inside this folder.

In Eclipse: Right click on your project: Properties > Java Build Path > Source > Link Source and choose the folder we've just created.

Now your projects use the same code. You only need to modify it once for both projects and there's no risk of error.

Hope I helped :).

Solution 2

The simple solution is to copy the desired java files into your project structure. Of course, you may have to change package names manually.

Share:
14,535
François Guthmann
Author by

François Guthmann

Currently learning android.

Updated on June 15, 2022

Comments

  • François Guthmann
    François Guthmann almost 2 years

    I'm working on an android game that requires a server. We've set up a git in which there are three folders: Client, Server and Shared:

    Client contains the Android-Studio project. Server contains an Eclipse project which is our server and database. Shared contains java classes that I would like to use in both the Eclipse and Android-Studio project.

    In Eclipse I successfully loaded the files from Shared by importing them as "file system". (It created a package and everything works great) In Android-Studio, I can't find a solution that doesn't require me to copy-paste the classes in the project. The aim here is to have those file at only one location as they're susceptible to change but must me identical in both the projects... So those solutions don't fit my needs.

    I'm looking for something like a way to link files from outside the project. (I managed to link libraries from inside the project already but not from outside)