Can Android Studio be used to run standard Java projects?

200,376

Solution 1

Tested on 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.

  1. Open your Android project in Android Studio. If you do not have one, create one.
  2. Click File > New Module. Select Java Library and click Next.
  3. Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
  4. Add your code to the Java module you've just created.
  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.

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.

My usage case: My Android app relies on some precomputed files to function. These precomputed files are generated by some Java code. 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

If you want to enable Kotlin inside your standalone project, do the following.

  1. 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
        }
    }
    ...
    
  2. 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)
    }
    ...
    
  3. Bonus step: Convert your main function to Kotlin! Simply change your main class to:

    object Main {
        ...
        @JvmStatic
        fun main(args: Array<String>) {
            // do something
        }
        ...
    }
    

Solution 2

EDIT: many moon after this question was asked, yes, now apparently you can.

No, but it's based on 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.

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.

Basically it works like this

Android Studio is just Android the Android Stuff from IDEA 13 Community...

...which will be free, and is anything from IDEA 13 Ultimate...

...that doesn't require a database or app server.

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. There's a lot of good new Android stuff in it, that's going into 13.

Solution 3

Easy way to run a java program in Android Studio would be,

  1. Create a java Class says "Test.java" in Android Studio.

  2. Write your code eg, a Hello World program to test.

  3. Right-click on the Java class and:

  • select the option Run 'Test.main()'

or

  • press CTRL + SHIFT + F10 (on windows) or control + R (on Mac)

There you have your Java code running below.

Solution 4

With Android Studio 0.6.1+ (and possibly earlier) you can easily develop standard Java (non-Android) apps.

This method has been tested on 0.8.2:

Start by creating a vanilla Android Phone app, using File > New Project. Then add a Java Library module to hold your Java Application code. (Choose 'Java Library' even if you're building an application). You'll find you can build and run Java apps with main() methods, Swing apps etc.

You'll want to delete the auto-generated Android "app" module, which you're not using. 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. You can go ahead and delete the "app" module from the disk.

In 0.6.1 you could avoid creating the android module in the first place:

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.

Once the project is created, go to File -> Project Structure -> Project, and set your JDK as the "Project SDK". Add a Java Library module to hold your application code as above.

Solution 5

Here's exactly what the setup looks like.

enter image description here

Edit Configurations > '+' > Application: enter image description here

Share:
200,376
mgibson
Author by

mgibson

Interested in making the world a better place through technology and software. Experienced full stack developer with deep experience across the Node / Vue / Typescript stack

Updated on July 08, 2022

Comments

  • mgibson
    mgibson almost 2 years

    For those times when you want to isolate the Java and give it a quick test..

    Can you run non-Android Java projects in Android studio as in Eclipse?