Why Android Studio doesn't allow me to create

2019-01-19 10:24发布

问题:

Starting from the 2 days ago update to Android Studio, it doesnt let me create Java classes anymore. And the current classes are now with a strange symbol.

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

Here are two screenshot

UPDATE 1:

Here is the structure of my Android Studio project. AndroidManifest is in the right place.

回答1:

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



回答2:

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

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

Regards,

Paul



回答3:

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'
    }

}


回答4:

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.