可以使用Android Studio运行标准Java项目吗?

本文翻译自:Can Android Studio be used to run standard Java projects?

For those times when you want to isolate the Java and give it a quick test.. 对于那些想要隔离Java并对其进行快速测试的时代。

Can you run non-Android Java projects in Android studio as in Eclipse? 您可以像Eclipse一样在Android Studio中运行非Android Java项目吗?


#1楼

参考:https://stackoom.com/question/17lOM/可以使用Android-Studio运行标准Java项目吗


#2楼

EDIT: many moon after this question was asked, yes, now apparently you can. 编辑:这个问题问了很多月之后,是的,现在显然可以了。

No, but it's based on Intellij IDEA. 否,但是它基于Intellij IDEA。

The community version of that is free for download but it doesn't support most things requiring an external database or application server. 该社区版本可免费下载,但不支持大多数需要外部数据库或应用程序服务器的功能。 The line for Java is pretty much that JavaSE code can happily use Community. Java的代码行几乎等于JavaSE代码可以愉快地使用Community。

If you want that (or are using JavaEE) then you either need the Ultimate version, which isn't free, or the EAP of the next version which is usually good for a month until they release another. 如果需要(或正在使用JavaEE),则需要非免费的Ultimate版本,或者通常需要一个月才能使用的下一版本的EAP,直到他们发布另一个。

Basically it works like this 基本上是这样的

Android Studio is just Android the Android Stuff from IDEA 13 Community... Android Studio只是Android,是IDEA 13社区的Android Stuff。

...which will be free, and is anything from IDEA 13 Ultimate... ...将是免费的,并且是IDEA 13 Ultimate的任何内容...

...that doesn't require a database or app server. ...不需要数据库或应用服务器。

http://www.jetbrains.com/idea/ http://www.jetbrains.com/idea/

IDEA 12 Community doesn't have the ability to import the gradilized project and it won't, so while you can do Android development in it now (I do), don't expect it to have the same features as Android Studio. IDEA 12社区没有能力导入已分级的项目,因此也无法导入,因此,尽管您现在可以在其中进行Android开发(我知道),但是不要期望它具有与Android Studio相同的功能。 There's a lot of good new Android stuff in it, that's going into 13. 里面有很多不错的Android新东西,即将推出13个。


#3楼

I found a somewhat hacky, annoying and not-completely-sure-it-always-works solution to this. 我发现了一个有点笨拙,烦人且不能完全确保始终有效的解决方案。 I wanted to share in case someone else finds it useful. 我想分享一下,以防其他人觉得有用。

In Android Studio, you can right-click a class with a main method and select "Run .main()". 在Android Studio中,您可以右键单击带有main方法的类,然后选择“ Run .main()”。 This will create a new Run configuration for YourClass, although it won't quite work: it will be missing some classpath entries. 这将为YourClass创建一个新的Run配置,尽管它不会完全起作用:它将缺少一些类路径条目。

In order to fix the missing classpath entries, go into the Project Structure and manually add the output folder location for your module and any other module dependencies that you need, like so: 为了修复丢失的类路径条目,请进入“项目结构”,然后手动为模块以及所需的任何其他模块依赖项添加输出文件夹位置,如下所示:

  • File -> Project Structure ... 文件->项目结构...
  • Select "Modules" in the Project Settings panel on the left-column panel 在左列面板的“项目设置”面板中选择“模块”
  • Select your module on the list of modules in the middle-column panel 在中间列面板的模块列表中选择您的模块
  • Select the "Dependencies" tab on the right-column panel 选择右列面板上的“ Dependencies”选项卡

And then for the module where you have your Java application as well as for each of the module dependencies you need: - Click "+" -> "Jars or directories" on the far right of the right-column panel - Navigate to the output folder of the module (eg: my_module/build/classes/main/java) and click "OK" - On the new entry to the Dependencies list, on the far right, change the select box from "Compile" to "Runtime" 然后,对于拥有Java应用程序的模块以及所需的每个模块依赖项:-单击右列面板最右侧的“ +”->“ Jars或目录”-导航至输出模块的文件夹(例如:my_module / build / classes / main / java),然后单击“确定”-在“依赖关系”列表的新条目(最右边)上,将选择框从“编译”更改为“运行时”

After this, you should be able to execute the Run configuration you just created to run the simple Java application. 之后,您应该能够执行刚刚创建的运行配置,以运行简单的Java应用程序。

One thing to note is that, for my particular [quite involved] Android Studio project set-up, I have to manually build the project with gradle, from outside Android Studio in order to get my simple Java Application classes to build, before I run the application - I think this is because the Run configuration of type "Application" is not triggering the corresponding Gradle build. 需要注意的一件事是,对于我特定的[相当多的] Android Studio项目设置,我必须从Android Studio外部使用gradle手动构建项目,以便在运行之前构建简单的Java Application类。应用程序-我认为这是因为类型为“应用程序”的运行配置未触发相应的Gradle构建。

Finally, this was done on Android Studio 0.4.0. 最后,这是在Android Studio 0.4.0上完成的。

I hope others find it useful. 我希望其他人觉得它有用。 I also hope Google comes around to supporting this functionality soon. 我也希望Google能够尽快支持此功能。


#4楼

With Android Studio 0.6.1+ (and possibly earlier) you can easily develop standard Java (non-Android) apps. 使用Android Studio 0.6.1+(可能更早),您可以轻松开发标准Java(非Android)应用。

This method has been tested on 0.8.2: 此方法已在0.8.2上进行了测试:

Start by creating a vanilla Android Phone app, using File > New Project. 首先使用“文件”>“新建项目”创建一个普通的Android Phone应用。 Then add a Java Library module to hold your Java Application code. 然后添加一个Java库模块来保存您的Java应用程序代码。 (Choose 'Java Library' even if you're building an application). (即使您正在构建应用程序,也请选择“ Java库”)。 You'll find you can build and run Java apps with main() methods, Swing apps etc. 您会发现可以使用main()方法,Swing应用程序等来构建和运行Java应用程序。

You'll want to delete the auto-generated Android "app" module, which you're not using. 您将要删除不使用的自动生成的Android“应用”模块。 Go to File -> Project Structure, and delete it (select the "app" module in the box on the left, and click the 'minus' icon above the box). 转到文件->项目结构,并将其删除(在左侧框中选择“应用”模块,然后单击框上方的“减号”图标)。 Now when you reopen File -> Project Structure -> Project, you'll see options for selecting the project SDK and language level, plus a bunch of other options that were previously hidden. 现在,当您重新打开文件->项目结构->项目时,您将看到用于选择项目SDK和语言级别的选项,以及许多其他先前隐藏的选项。 You can go ahead and delete the "app" module from the disk. 您可以继续从磁盘中删除“ app”模块。

In 0.6.1 you could avoid creating the android module in the first place: 在0.6.1中,您可以避免首先创建android模块:

Go to File > New Project. 转到文件>新建项目。 Fill in your application name. 填写您的应用程序名称。 On the "form factors" selection page, where you state your minimum Android SDK, deselect the Mobile checkbox, and proceed with creating your project. 在“外形尺寸”选择页面上,声明最低的Android SDK, 取消选中 “移动”复选框,然后继续创建项目。

Once the project is created, go to File -> Project Structure -> Project, and set your JDK as the "Project SDK". 创建项目后,转到文件->项目结构->项目,并将JDK设置为“项目SDK”。 Add a Java Library module to hold your application code as above. 添加Java库模块来保存您的应用程序代码(如上所述)。


#5楼

I installed IntelliJ IDEA community version from http://www.jetbrains.com/idea/download/ 我从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. 我尝试打开在Android Studio中启动的项目,但在构建gradle时失败。 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. 相反,我同时打开了android studio和intellij,并将一个屏幕紧挨着打开,然后将我的java文件,xml布局,可绘制对象拖放到以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. 它解决了gradle构建问题,现在我可以在IntelliJ中启动一个新项目,并设计一个android应用程序或一个基本Java应用程序。 Thankfully this worked because I hated having so many IDEs on my pc. 值得庆幸的是,这样做很有效,因为我讨厌PC上有这么多IDE。


#6楼

Tested on Android Studio 0.8.6 - 3.5 在Android Studio 0.8.6-3.5上测试

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模块。

  1. Open your Android project in Android Studio. 在Android Studio中打开您的Android项目。 If you do not have one, create one. 如果您没有,请创建一个。
  2. Click File > New Module . 单击文件>新建模块 Select Java Library and click Next . 选择Java库 ,然后单击下一步
  3. Fill in the package name, etc and click Finish . 填写软件包名称,等等,然后单击完成 You should now see a Java module inside your Android project. 现在,您应该在Android项目中看到一个Java模块。
  4. Add your code to the Java module you've just created. 将代码添加到刚创建的Java模块中。
  5. Click on the drop down to the left of the run button. 单击运行按钮左侧的下拉菜单。 Click Edit Configurations... 单击编辑配置...
  6. In the new window, click on the plus sign at the top left of the window and select Application 在新窗口中,单击窗口左上方的加号,然后选择“ 应用程序”
  7. A new application configuration should appear, enter in the details such as your main class and classpath of your module. 应出现一个新的应用程序配置,输入详细信息,例如您的主类和模块的类路径。
  8. Click OK . 单击确定

Now if you click run, this should compile and run your Java module. 现在,如果您单击运行,这将编译并运行您的Java模块。

If you get the error Error: Could not find or load main class... , just enter your main class (as you've done in step 7) again even if the field is already filled in. Click Apply and then click Ok . 如果出现Error: Could not find or load main class... ,即使该字段已经填写,也只需再次输入主类(如在步骤7中所做的那样)。单击应用 ,然后单击确定

My usage case: My Android app relies on some precomputed files to function. 我的使用案例:我的Android应用程序依靠一些预先计算的文件才能运行。 These precomputed files are generated by some Java code. 这些预先计算的文件是由某些Java代码生成的。 Since these two things go hand in hand, it makes the most sense to have both of these modules in the same project. 由于这两件事是并行的,因此将这两个模块都放在同一个项目中是最有意义的。

NEW - How to enable Kotlin in your standalone project 新增功能-如何在独立项目中启用Kotlin

If you want to enable Kotlin inside your standalone project, do the following. 如果要在独立项目中启用Kotlin,请执行以下操作。

  1. Continuing from the last step above, add the following code to your project level build.gradle (lines to add are denoted by >>> ): 从上面的最后一步继续,向您的项目级别build.gradle添加以下代码(要添加的行由>>>表示):

     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 } } ... 
  2. Add the following code to your module level build.gradle (lines to add are denoted by >>> ): 将以下代码添加到模块级别build.gradle (要添加的行用>>>表示):

     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) } ... 
  3. Bonus step: Convert your main function to Kotlin! 奖励步骤:将您的主要功能转换为Kotlin! Simply change your main class to: 只需将您的主要课程更改为:

     object Main { ... @JvmStatic fun main(args: Array<String>) { // do something } ... } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值