43

It's possible to add pure Java module to existing Android project.

But is it possible to create pure Java project with no Android dependencies?

Alex Dmitriev
  • 3,664
  • 3
  • 29
  • 35

11 Answers11

71

Yes, it is possible. You have to manually create all necessary files.

Here are steps for Gradle based project:

  1. Remove include ':app' form settings.gradle
  2. Remove app directory
  3. Replace build.gradle with example from end of this post (IntelliJ creates similar)
  4. Create folder hierarchy for your Java code (src/main/java) enter image description here
  5. Select Edit Configuration from drop down menu where normally you start project

  6. Click Add new Configuration and select Application enter image description here

  7. In Main class point your Main class.

Android studio is more or less like IntelliJ Community Edition.

apply plugin: 'java'

sourceCompatibility = 1.8
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}
Aleksander Mielczarek
  • 2,787
  • 1
  • 24
  • 43
  • 2
    It works :) Note: after step 4, click "Refresh all Gradle projects", otherwise you won't be able to continue. – Hermit May 23 '15 at 11:01
  • 1
    And don't forget to create main(String [ ] args) method :) – Slampy Aug 26 '15 at 06:56
  • 1
    @Hermit How to do that ? – user5716019 Mar 01 '16 at 09:35
  • Click the round refresh button at the top left of the Gradle Projects view – Hermit Mar 01 '16 at 09:42
  • 1
    Not working for me. It is showing Red "J" on my java files. It cant find the Main method i have added in the class. – M. Usman Khan Dec 06 '17 at 04:10
  • Error: Could not find or load main class Foo – That's Enam Mar 28 '18 at 08:31
  • Using your method, I am forced to manually sync the project with gradle files to show the src folder at every project startup.Is it possible to avoid that? – Evolve_or_Die Oct 24 '18 at 07:04
  • Hello future readers: I had some issues so hopefully I can help if you're reading this. 1: Right click on java -> Mark Directory as -> Sources Root 2: You NEED to create a package inside src/main/java and refer to it in the config with the package name. I had my class rooted in /java/ and it didn't work. – Zeek Aran Dec 27 '19 at 21:57
  • 3: And as Slampy said, you need this method - public static void main(String[] args) { } – Zeek Aran Dec 30 '19 at 16:39
6

Not via the 'New project' wizard.

One alternative is to create a Java maven/gradle project outside Android Studio, then 'import' it into AS via File->Open.

Gradle has the init plugin to set up a java project scaffold:

gradle init --type java-library

https://docs.gradle.org/current/userguide/build_init_plugin.html

user3452758
  • 638
  • 1
  • 7
  • 15
2

I think this is possible to create a new module from following path (Using Androdi Stdio 1.1.0):

File> New Module> Choose from more module> java library

Hope it will work's for you.

2

No you cannot create a java project with Android Studio because AS's building system won't let you build your project once you are done writing your application. Android Studio's gradle building system only builds .apk file.

Android Studio won't support most things requiring an external database or application server.

Apurva
  • 7,871
  • 7
  • 40
  • 59
2

Easiest method is to temporarily disable the Android Support Plugin. Un-check Configuration > Plugin > Android Support Plugin After restarting close any current project and you will get the new project wizard with all of your non-android options. Once your new project is created you can re-enable the Android plugin.

JT.
  • 633
  • 1
  • 8
  • 9
  • Disabling the plugins _Android APK Support_, _Android Games_ and _Android NDK Support_ at _File_ > _Settings_ > _Plugins_ does not work as described with Android Studio 3.6. – iron9 Mar 23 '20 at 12:54
1

One thing that you might want to add here to help REALLY new folks out (it kept tripping me up) is that after creating the folder hierarchy for your Java code (src/main/java) you must right click the java folder and select Mark Directory As > Sources Root. Otherwise, you won't have the option of creating a new java class within the foo directory.

1

Another way from my perspective:

  1. Create a new android project
  2. Add java module to your project
  3. Close android studio project and go to project's directory, just take the java module folder and paste it to new directory
  4. In Android Studio open exthis this module, ide will generate gradle file for you.
ssmm
  • 171
  • 1
  • 1
  • 10
1

The answer by Aleksander worked for me. Thanks Aleksander. I further needed to create a package and add a new java class to it before Step 5. Then in Step 6, I used that java class as the MainClass.

wat er
  • 361
  • 2
  • 5
0

Easiest Way From Scratch

  1. Create a empty folder where you want to put the project
  2. At the 'Welcome to Android Studio' screen, choose "Open existing..." enter image description here
  3. Navigate to your folder and hit OK
  4. Your project is created, now add your java files enter image description here
  5. Choose the SDK and you're done! Happy coding
Scott Grodberg
  • 168
  • 1
  • 5
0

The easiest way to run a pure java program with input

Follow these simple steps:

  1. Create a new project or open an existing project
  2. Right-click your package name under java directory > New > Scratch File
  3. Choose java from the dialog

A class with name Scratch.java will open now you can write your code here...

  1. To run your program just right-click to Scratch.java and choose option -> Run 'Scratch.main()'

Now you can see you program output. :)

0

I think my answer is the simpliest one: (To clarify, I'm not using Gradle, but I'm working with native Java only)

Simply create a root folder in your workspace directory, below which create two new folders src and res.

1

Then, in Android Studio, go File > Open and select the root folder you just created. You may have to refresh to see the folder.



However, if you add a new Java file by (RightClickMenu) > New > File and name the file Main.java, you should see that it's not working (denoted with an orange dot):

2

What you'll need to do is right click on the src folder, then Mark Directory As > Source Root



Repeat this with res directory, this time using Mark Directory As > Resources Root.



You should now see that your Main.java file is now recognized as a class. You can then proceed to do the coding thing. You may add new .java files through Right Clicking src (or any package directory), then New > Java Class.

3

In fact, you could mark the src and res directories first before adding the Main.java file. It's just a matter of your preference.

For me, this method is simpler than having to delete a number of setting files (which may potentially cause errors), and it works for me everytime. I use this method quite often for I have just migrated to Android Studio from Eclipse several months ago, and I used to work with native Java code instead of Android projects.

Hope this helps. :)

K. H. Tam
  • 41
  • 7