CISC181 F2017 PlainJavaInAndroidStudio

From class_wiki
Revision as of 10:21, 28 August 2017 by Cer (talk | contribs) (Created page with "This is adapted from idunnololz's comment titled "Tested on Android Studio 0.8.6 - 1.0.2" [http://stackoverflow.com/questions/16626810/can-android-studio-be-used-to-run-standa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is adapted from idunnololz's comment titled "Tested on Android Studio 0.8.6 - 1.0.2" here.

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 > New Module. Select Java Library and click Next.
  3. Set the library name to be "java" (this is arbitrary)
  4. Click Edit and fill in the package name (which you just created here -- should be cisc181.mylab_n)
  5. Fill in a class name -- "MyClass" or whatever the assignment specifies -- and click Finish. You should now see a module called "java" inside your Android project.
  6. On the far left there should be a "project view" pane. If not, click sideways (vertical) tab that says "1: Project" or type "Alt+1" (hold down Alt key and press 1)
  7. Open the "java" module folder, double-click on "MyClass" (or whatever your class name is) and start coding! For example, paste this into MyClass (between the curly braces) and save with Control-S:
 public static void main(String[] args) 
 {
   System.out.println("Hello World!"); 
 }
  1. Before you forget, make sure to ADD YOUR NAME to the code as a comment.
  2. Click on the drop down menu to the left of the Run button (the green triangle next to the bug icon). Click Edit Configurations...
  3. In the new window, click on the green plus sign at the top left of the window and select Application
  4. A new application configuration should appear. Enter in the details such as name ("MyJavaProgram"), your full main class ("cisc181.mylab_n.MyClass"), and classpath of your module ("java"), and click OK. Pop-up menus should help with faster completion here.

Now if you click Run (or use Shift+F10 keyboard shortcut), this should compile and run your Java module. If it does not happen quickly (the first time may be quite slow), there is a progress bar at the bottom of the window.

You can tweak your editor's look and feel at File > Settings > Editor