eclipse Android Studio:如何从项目外部导入java类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34841674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio: How to import java class from outside the project?
提问by Fran?ois Guthmann
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:
我正在开发一个需要服务器的安卓游戏。我们已经设置了一个 git,其中有三个文件夹:Client、Server 和 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.
客户端包含 Android-Studio 项目。服务器包含一个 Eclipse 项目,它是我们的服务器和数据库。Shared 包含我想在 Eclipse 和 Android-Studio 项目中使用的 java 类。
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.
在 Eclipse 中,我通过将文件作为“文件系统”导入,成功地从 Shared 加载了这些文件。(它创建了一个包并且一切正常)在 Android-Studio 中,我找不到不需要我在项目中复制粘贴类的解决方案。这里的目的是将这些文件放在一个位置,因为它们很容易发生变化,但我必须在两个项目中保持相同……所以这些解决方案不符合我的需求。
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)
我正在寻找一种从项目外部链接文件的方法。(我已经设法从项目内部链接库,但没有从外部链接)
回答by Erik K.
The simple solution is to copy the desired java files into your project structure. Of course, you may have to change package names manually.
简单的解决方案是将所需的 java 文件复制到您的项目结构中。当然,您可能必须手动更改包名称。
回答by Fran?ois Guthmann
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.
在 Android-Studio 中:创建一个类似于“CommonEclipseAndroid”的文件夹,路径类似于以下路径:project/Android/app/src/main/java/com/example/myapplication/CommonEclipseAndroid 粘贴您需要在两者中使用的代码该文件夹内的项目。
In Eclipse: Right click on your project: Properties > Java Build Path > Source > Link Source and choose the folder we've just created.
在 Eclipse 中:右键单击您的项目:Properties > Java Build Path > Source > Link Source 并选择我们刚刚创建的文件夹。
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 :).
希望我有所帮助:)。
回答by Avinash Joshi
You can import the shared folder as a module . go to file> new> import module Now use the same imported folder in eclipse as well.
您可以将共享文件夹作为模块导入。转到文件> 新建> 导入模块 现在在 Eclipse 中也使用相同的导入文件夹。