Adding java folders to android studio project

≯℡__Kan透↙ 提交于 2019-12-18 03:55:10

问题


Started working on my first project using Android Studio instead of Eclipse ADT. I would like to add sub folders to contain java.classes in order to structure my app but are unable to obtain references to them in my activity

structure is (with app name replaced)

src/main/[MyCompany]/[myAppName]/model/user.java

and

src/main/[MyCompany]/[myAppName]/helperClasses/SQLiteHelper.java

however I am unable to reference them either directly in code or by import, and are forced to put them in the appName folder with the activity to reference them.

Is this simply a limitation in Android or is there a way to organize your project with folders better. I used the new->folder-java folder option to create folders


回答1:


IntelliJIdea mixes packages and directories.

So, right click on the java folder, click new - 'Package', then add your directory.




回答2:


In my case I wanted a mock package with java folder:

  1. First step you have to put project mode:

  1. Click right button, new, Directory, in my case "mock".
  2. Click right button, new, Folder, Java Folder:

  1. Click in checkbock "change folder location" and put your path, in my case "src/mock/java":

  1. The solution is this:




回答3:


Try this path : src/main/java/[MyCompany]/[myAppName]/...
src/main/java/ is default source directory in Android Studio

OR

if you don't want to change your directory structure. you have to modify your build.gradleto change source directory.

something like: (this example is for ADT project)

android {

    ...

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

    ...

}


来源:https://stackoverflow.com/questions/27539702/adding-java-folders-to-android-studio-project

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