How to Use Android Studio to Write Pure Java

For those looking to sharpen skills using Data Structures and building Algorithms you’re most likely reading Cracking The Code Interview as well doing practice problems on a number of the sites out there (KhanAcademy, HackerRank, InterviewCake, coderbyte, <code_eval>, GeeksforGeeks, etc…)

I prefer using one compiler to do everything, and since I’m developing apps in Android Studio I’d like to also construct my coding challenge here because…

  1. Some coding challenge sites / classes pick a specific language and I want to use Java or I’m doing questions out of a book.
  2. I can automatically save my solution for referring back in a Google Drive folder as well as on GitHub through the Android Studio UI.

Before I show you how, if you want to get running right away you can download or fork this example Repo and you won’t need to read further. If you plan to upload it to a type of Version Control just make sure to remove the origin and replace it with your Repo using this command in the terminal: git remote set-url origin git@github.com:yourprofilename-/RepoName.git

  1. Open a new Project and choose your Destination path and initial settings: Hit Next on Form Factors (doesn’t matter) > Choose No Activity and Finish
  2. In the dropdown view Select Project

3. Remove app Directory and in Settings.gradle delete include ‘:app’ > ignore the Sync Now prompt at the top of the screen in Android Studio

4. Replace build.gradle code with the following > Then click Sync Now prompt

apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}

5. Configure src and Child Directories main, java, foo and .java Class > Add in main class to construct code and print results

public static void main(String[] arg) {
String hi = "Hello World";
System.out.println(hi);
}

6. Configure Android Studio to run your Java Class properly: Edit Configurations > ‘+’ > Application

7. Get Runin’!

Thanks to this thread in StackOverflow as well as my friend Gurmeet Singh who helped me figure this out.

DeFi Punk

The (retired) Pub(lication) for Android & Tech, focused on Development