24

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

3 Answers 3

47

IntelliJIdea mixes packages and directories.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Can u please suggest something here? stackoverflow.com/questions/39542522/…
15

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

  1. First step you have to put project mode:

enter image description here

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

enter image description here

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

enter image description here

  1. The solution is this:

enter image description here

1 Comment

Instead of adding a folder, right click on the current package (com.example.coolapp) and select New → Package. Type the name for the subfolder, and a new package subfolder will be created beneath com.example.coolapp. In Java, each subfolder is considered a new package. Doing it this way will make sure that Java classes created in the subfolder or moved between folders will function correctly (Android Studio will auto-update import statements and package names so they function correctly).
1

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

    ...

}

3 Comments

It would appear that due to my unfamiliarity with AndroidStudio what you propose is essentially what I've got. src/main/java/companyName.AppName/model/user.Java which doesn't help, unless I put user.java directly into the companyName.AppName folder. I'm curious if it's even supposed to be possible to put java files into sub folders for android projects? Sorry about the long delay, Christmas and all that...
About 3 weeks ago, I thought that you want to import ADT project to AS project. So I answered like that. But now, I read again your question, I have found misunderstood your question. sorry, my fault. I think new->folder-java was the problem. In my guess, your project's build.gradle file has something like this: sourceSets { main { java.srcDirs = ['src/main/java', 'src/main/[MyCompany]/[myAppName]'] } }. check it out, then ease them and run 'sync with gradle'. In general case, you don't have to create new folder-java. just drag-and drop the folder at your project navigate view.
But he wants to change the dir structure. That is the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.