eclipse 是否可以在 Android Studio 中编译一个简单的 Java 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32857374/
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
Is it possible to compile a simple Java code in Android Studio?
提问by Seung Joo Noh
I already have Android Studio, but not Eclipse.
我已经有了 Android Studio,但没有 Eclipse。
Do I have to install Eclipse in order to test some simple Java code? Or is it possible using Android Studio?
我是否必须安装 Eclipse 才能测试一些简单的 Java 代码?或者是否可以使用 Android Studio?
What is the difference between both?
两者有什么区别?
回答by dev.bmax
The cool thing about Android Studio is that it delegates the build process to Gradle. And with Gradle, building a plain Java application is a matter of adding the necessary plugin to the build.gradle file.
Android Studio 很酷的一点是它将构建过程委托给 Gradle。使用 Gradle,构建一个普通的 Java 应用程序只需将必要的插件添加到 build.gradle 文件中。
apply plugin: 'application'
mainClassName = "com.your.package.MainClassName"
And you don't need to go through the new project wizard. Just make a project directory, create a build.gradle file in it with the script above. Put your java source code is in 'src/main/java'.
而且您无需通过新建项目向导。只需创建一个项目目录,使用上面的脚本在其中创建一个 build.gradle 文件。把你的java源代码放在'src/main/java'中。
In Android Studio click 'Import Project (Eclipse, Gradle, etc.)', open the project directory and select the build.gradle file. Choose OK to configure Gradle wrapper. When the import is finished press RUN and let the plugin do the rest.
在Android Studio中点击'Import Project (Eclipse, Gradle, etc.)',打开项目目录,选择build.gradle文件。选择确定以配置 Gradle 包装器。导入完成后,按 RUN 并让插件完成剩下的工作。
Note, that the 'application' plugin also implicitly applies the Java plugin.
请注意,“应用程序”插件也隐式地应用了 Java 插件。
More about these plugins here: https://docs.gradle.org/current/userguide/application_plugin.htmland here: https://docs.gradle.org/current/userguide/java_plugin.html
更多关于这些插件的信息:https: //docs.gradle.org/current/userguide/application_plugin.html和这里:https: //docs.gradle.org/current/userguide/java_plugin.html
回答by Anirudh Sharma
I haven't tested it yet.But this is the first thing i found when i googled it:
我还没有测试过。但这是我在谷歌搜索时发现的第一件事:
Tested on Android Studio 0.8.6 - 1.2.2
在 Android Studio 0.8.6 - 1.2.2 上测试
Using this method you can have Java modules and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.
使用这种方法,您可以在同一个项目中拥有 Java 模块和 Android 模块,还可以将 Java 模块作为独立的 Java 项目进行编译和运行。
Open your Android project in Android Studio. If you do not have one, create one.
在 Android Studio 中打开您的 Android 项目。如果您没有,请创建一个。
1.Click File > New Module. Select Java Library and click Next.
1. 单击文件 > 新建模块。选择 Java 库并单击下一步。
2.Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
2.填写包名等,点击完成。您现在应该会在您的 Android 项目中看到一个 Java 模块。
3.Add your code to the Java module you've just created.
3. 将您的代码添加到您刚刚创建的 Java 模块中。
4.click on the drop down to the left of the run button. Click Edit Configurations...
4.单击运行按钮左侧的下拉菜单。单击编辑配置...
5.In the new window, click on the plus sign at the top left of the window and select Application
5.在新窗口中,点击窗口左上角的加号并选择应用程序
6.A new application configuration should appear, enter in the details such as your main class and classpath of your module. Click OK.
6.应该会出现一个新的应用程序配置,输入详细信息,例如您的主类和模块的类路径。单击确定。
7.Now if you click run, this should compile and run your Java module.
7.现在,如果您单击运行,这将编译并运行您的 Java 模块。
and you might wanna check This Thread.
你可能想检查这个线程。
回答by Chris
What is the difference?
有什么不同?
Android Studio is built by intellij for googles Android framework. It is specifically made for making App's (APK is made out of it). Now intellij also has their own IDE (Integrated development environment) that looks a lot similar but is designed to build things other than apps like JAR's and website pages.
Android Studio 是由 Intellij 为谷歌 Android 框架构建的。它是专门为制作应用程序而制作的(APK 是由它制作的)。现在,intellij 也有自己的 IDE(集成开发环境),它看起来很相似,但旨在构建 JAR 和网站页面等应用程序以外的东西。
Eclipse is also an IDE and will essentially do what intellij's IDE does. Eclipse also can build APK's (although google recommends and only supports Android Studio now). In terms of difference for IDE's it is between its generally up to users preference for coding in.
Eclipse 也是一个 IDE,并且本质上会执行 Intellij 的 IDE 所做的事情。Eclipse 也可以构建 APK(虽然 google 推荐并且现在只支持 Android Studio)。就 IDE 的差异而言,它通常取决于用户对编码的偏好。
From my own experience; I would only use Android Studio for writing app's and another IDE for any other type of program.
根据我自己的经验;我只会使用 Android Studio 为任何其他类型的程序编写应用程序和另一个 IDE。
回答by DroidAks
Please open the below link. It shows how to run an normal java code in android studio. https://github.com/libgdx/libgdx/wiki/Gradle-and-Intellij-IDEA#running-your-project
请打开以下链接。它展示了如何在 android studio 中运行普通的 java 代码。 https://github.com/libgdx/libgdx/wiki/Gradle-and-Intellij-IDEA#running-your-project
Referenced from the link:Running Java application in Android Studio
回答by Yasir Ali
I have not tried android studio to build java project but technically it should be possible why? because android studio uses gradle as build system to build projects and you can build stand alone java project with gradle. it is same like maven etc.
我还没有尝试过 android studio 来构建 java 项目,但从技术上讲应该是可能的,为什么?因为 android studio 使用 gradle 作为构建系统来构建项目,您可以使用 gradle 构建独立的 java 项目。它与 maven 等相同。
Now your question to Android studio VS Eclipse
现在你对 Android studio VS Eclipse 的问题
as i mentioned earlier Android studio uses gradle as build system while Eclipse uses Eclipse-ADT.
正如我之前提到的,Android Studio 使用 gradle 作为构建系统,而 Eclipse 使用 Eclipse-ADT。
Now it depends if android studio allow you to open simple java project if it does then there should not be any problem to build.
现在这取决于android studio是否允许您打开简单的java项目,如果它允许,那么构建应该没有任何问题。
In android studio there is separate tab for gradle where it shows all gradle tasks you can run any task from there.
在 android studio 中有一个单独的 gradle 选项卡,它显示了所有 gradle 任务,您可以从那里运行任何任务。
回答by MOHAMMAD S HUSSAIN
From this way you can check Java modules in Android studio.This is easy way to compile and run Java Code in Android studio.
通过这种方式,您可以在 Android Studio 中检查 Java 模块。这是在 Android Studio 中编译和运行 Java 代码的简单方法。
First of all Open your Android project in Android Studio. If you do not have any then create one project.
首先在 Android Studio 中打开你的 Android 项目。如果您没有,则创建一个项目。
Click File > New Module.Select Java Library and click Next.
单击文件 > 新建模块。选择 Java 库并单击下一步。
Write your package name, etc and click Finish. You should now able to see a Java module inside your Android project.
写下您的包名称等,然后单击完成。您现在应该能够在您的 Android 项目中看到一个 Java 模块。
Add your code or any program in java language to the Java module you've just created.
将您的代码或任何 Java 语言程序添加到您刚刚创建的 Java 模块中。
Click on the drop down to the left of the run button. Click Edit Configurations...
单击运行按钮左侧的下拉菜单。单击编辑配置...
In the new window,You'll see a + sign,click on the plus sign at the top left of the window and select Application
在新窗口中,您将看到一个 + 号,单击窗口左上角的加号并选择应用程序
A new application configuration must be appear,enter in the details such as your main class and java classpath of your module.
必须出现一个新的应用程序配置,输入详细信息,例如您的主类和模块的 java 类路径。
Click on OK button. Now if you click run, this will compile and run your Java code which you written.
单击确定按钮。现在,如果您单击运行,这将编译并运行您编写的 Java 代码。