可以使用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 } ... } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章