对于那些时候,当你想要隔离Java并给它一个快速测试。

你能像在Eclipse中一样在Android studio中运行非Android Java项目吗?


当前回答

在Android Studio 0.8.14中测试: 我能够以这种方式以最少的步骤运行一个标准项目: 在打开的Android Studio项目中,单击File > New Module。 单击More Modules > Java Library > Next,然后填写您喜欢的名称。 一个新的模块将作为一个文件夹出现在项目结构中与“app”文件夹相同的级别。打开它并打开新的Java类文件。

然后您可以添加您的代码,并选择构建>运行'YourClassName'。很快,你的代码就在没有Android设备的情况下运行了!

其他回答

使用Android Studio 0.6.1+(或更早),你可以轻松开发标准Java(非Android)应用。

此方法已在0.8.2上测试:

首先创建一个普通的Android手机应用程序,使用文件>新项目。然后添加一个Java库模块来保存Java应用程序代码。(即使您正在构建应用程序,也要选择“Java Library”)。你会发现你可以用main()方法构建和运行Java应用程序,Swing应用程序等等。

你会想要删除自动生成的Android“app”模块,你不使用它。进入文件->项目结构,删除它(选择左边框中的“app”模块,点击框上方的“减”图标)。现在,当你重新打开文件->项目结构->项目,你会看到选择项目SDK和语言级别的选项,加上一堆其他选项,以前是隐藏的。你可以从磁盘中删除“app”模块。

在0.6.1中,你可以避免在第一时间创建android模块:

进入文件>新项目。填写您的应用程序名称。在“form factors”选择页面上,在这里您可以声明您的最小Android SDK,取消选择Mobile复选框,并继续创建您的项目。

一旦项目创建,转到文件->项目结构->项目,并设置您的JDK为“项目SDK”。如上所述,添加一个Java Library模块来保存应用程序代码。

在Android Studio 0.8.14中测试: 我能够以这种方式以最少的步骤运行一个标准项目: 在打开的Android Studio项目中,单击File > New Module。 单击More Modules > Java Library > Next,然后填写您喜欢的名称。 一个新的模块将作为一个文件夹出现在项目结构中与“app”文件夹相同的级别。打开它并打开新的Java类文件。

然后您可以添加您的代码,并选择构建>运行'YourClassName'。很快,你的代码就在没有Android设备的情况下运行了!

我从http://www.jetbrains.com/idea/download/安装了IntelliJ IDEA社区版

I tried opening my project that I started in Android studio but it failed when at gradle build. I instead opened both android studio and intellij at same time and placed one screen next to the other and simply drag and dropped my java files, xml layouts, drawables, and manifest into the project hiearchy of a new project started in IntelliJ. It worked around the gradle build issues and now I can start a new project in IntelliJ and design either an android app or a basic Java app. Thankfully this worked because I hated having so many IDEs on my pc.

在Android Studio 0.8.6 - 3.5上测试

使用这种方法,你可以在同一个项目中拥有Java模块和Android模块,还可以将Java模块作为独立的Java项目编译和运行。

Open your Android project in Android Studio. If you do not have one, create one. Click File > New Module. Select Java Library and click Next. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project. Add your code to the Java module you've just created. Click on the drop down to the left of the run button. Click Edit Configurations... In the new window, click on the plus sign at the top left of the window and select Application A new application configuration should appear, enter in the details such as your main class and classpath of your module. Click OK.

现在,如果单击run,应该会编译并运行Java模块。

如果你得到错误错误:无法找到或加载主类…,只需再次输入你的主类(就像你在步骤7中所做的那样),即使字段已经填写。单击Apply,然后单击Ok。

我的用例: 我的Android应用程序依赖于一些预先计算好的文件来运行。这些预计算文件是由一些Java代码生成的。由于这两件事是相辅相成的,所以在同一个项目中同时拥有这两个模块是最有意义的。

新-如何在你的独立项目中启用Kotlin

如果希望在独立项目中启用Kotlin,请执行以下操作。

Continuing from the last step above, add the following code to your project level build.gradle (lines to add are denoted by >>>): buildscript { >>> ext.kotlin_version = '1.2.51' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' >>> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } ... Add the following code to your module level build.gradle (lines to add are denoted by >>>): apply plugin: 'java-library' >>> apply plugin: 'kotlin' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) >>> implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" >>> runtimeClasspath files(compileKotlin.destinationDir) } ... Bonus step: Convert your main function to Kotlin! Simply change your main class to: object Main { ... @JvmStatic fun main(args: Array<String>) { // do something } ... }

如果你做File>Open…然后选择pom.xml文件。确保将侧边栏左上角的“Android”下拉框改为“Project”,以查看所有文件。此外,我认为如果你的pom.xml文件在一个名为“app/”的文件夹中,这将会有所帮助。

免责声明:我的java项目是由谷歌应用程序引擎生成的。