Why Android Studio doesn't allow me to create Java Classes?

ⅰ亾dé卋堺 提交于 2019-11-28 11:53:31

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

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

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

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!