32

Starting from the update to Android Studio 2 days ago, it doesn't let me create Java classes anymore. And the current classes now have a strange symbol.

I tried to export and import many times with different configuration but they never worked. Any advice?

Here are two screenshots: enter image description here enter image description here

Here is the structure of my Android Studio project. AndroidManifest is in the right place: enter image description here

Null
  • 1,950
  • 9
  • 30
  • 33
Stefano Munarini
  • 2,711
  • 2
  • 22
  • 26
  • If anyone sees this now, you can run "Sync Project with Gradle files" and see if that resolves the issue. I ran into this problem because I closed the project before the initial Gradle Sync had finished. – Sid Datta Feb 21 '20 at 00:41

4 Answers4

42

Right click on the src folder, Mark Directory As -> Source Root.

SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65
  • Oh thanks a lot. That worked for the Java classes, but now if i try to build project error comes out `Android Source Generator: [LibrettoProva] AndroidManifest.xml file not found`. WHat is it? Can you explain what changed with this Android Studio update? – Stefano Munarini Dec 14 '13 at 20:47
  • Not sure, but the problem now is that it's unable to find the AndroidManifest, as it says, can you see it on your project? – SoManyGoblins Dec 14 '13 at 20:48
  • I'd try rearranging your code in a more usual manner. In your project, you should have a folder named src, inside it a folder called main, and in there a java folder, which should be marked as sources root, and your com package in there. So, you should have the following structure instead of what you have "src/main/java(sources root)/com/...", and have your AndroidManifest.xml inside main. That's the way AndroidStudio builds your projects when you make a new one, so I assume it's the default structure. You can customize this structure from build.gradle as well. – SoManyGoblins Dec 14 '13 at 20:55
  • Thanks I'm gonna try. – Stefano Munarini Dec 14 '13 at 20:56
  • Can you suggest me a gradle guide? This is the first time for me using gradle – Stefano Munarini Dec 14 '13 at 20:58
  • 1
    Not really that versed into Gradle, fortunately AndroidStudio handles mostly everything in the Gradle file without requiring you to know too much of how it works, but I'd suggest the manual on their site, or maybe this: http://www.jayway.com/2013/02/26/using-gradle-for-building-android-applications/ – SoManyGoblins Dec 14 '13 at 21:01
  • 7
    Where is the option mark directory as - source root appears? I couldn't find this menu item when I right click the 'src' folder and I've got the same problem - can't add java classe files – GyRo Feb 09 '15 at 11:30
  • 1
    Note that my response is from 2013, well before the release of Android Studio 1.0 – SoManyGoblins Feb 09 '15 at 15:32
  • 1
    Any explanation or document? Why won't AS do this for developer? – Alston Oct 20 '19 at 12:30
11

Android Studio 1.0 Update:

In project view of Android Studio please check for “java” folder under src.

Usually there will be two folders automatically created by Android Studio under src -

  1. androidTest (This is used for writing test cases),
  2. main (This is used for writing your application source.)

Each of these should contain a "java" folder.

If java folder is not there, you can create it with right click on main (or androidTest) as shown below

Creating Java Folder

To add a new java class, please right click on the "java folder" and select "java class".

Adding new class to the project

Regards,

Paul

Paul
  • 506
  • 4
  • 10
  • 1
    I am used on Intellij Idea, so I selected New > Package, but I was unable to add java class. Folder > Java folder is the trick in Android Studio. – Leos Literak Feb 04 '15 at 05:52
6

I tried the solution above but couldn't find the menu item to set the 'src' as the Source Root. So I managed to solve it by making a similar setting in the app's build.gradle file. I added this:

android {
...
sourceSets {
        main.java.srcDirs += 'src/main/java'
    }

}
GyRo
  • 2,586
  • 5
  • 30
  • 38
3

A bit of an edge-case, but I found my way here because I couldn't create a java package.

The reason being my project has multiple build variants (Google / Amazon build variants with different source folders) and I was trying to create a package in a non-active build variant java source folder.

As of Android Studio 3.1, you can't create a new package if you're trying to do so in a build-variant that's not the current build variant you're working with.

Basically, you have to switch to that build variant before any type of compilation change can be made.

dell116
  • 5,835
  • 9
  • 52
  • 70