Guilherme
Guilherme

Reputation: 7949

Android Studio - Add subfolder without new package

I have a package com.myapp.api which includes a couple of helper classes and lots of model classes. For the sake of organization, I wish I could place all the models in a subfolder called Models.

When I do that, Android Studio will mark an error on every model file, saying that their packages should be named com.myapp.api.Models instead.

Renaming the package wouldn't be a big deal, but I need those models to access some package-visible methods and variable present in the helper classes. If I rename, I must make those methods and variables public, and I don't want to.

Is there any way to force Android Studio not to try to create a new package based on the files structure?

Upvotes: 3

Views: 4580

Answers (2)

Scott Barta
Scott Barta

Reputation: 80010

The simple answer is no. This isn't an Android Studio thing, it's a Java thing -- directories on disk correspond to packages.

If you're really motivated to work around it, you could set up multiple source folders, e.g.

  • src/main/java/com/myapp/api
  • src/main/java-models/com/myapp/api

and manipulate srcDirs in your build.gradle to add the new source directories:

sourceSets {
    main.java.srcDirs = ['src/main/java', 'src/main/java-models']
}

but that's kind of a confusing way to organize your source, and it's a pattern you rarely see.

Upvotes: 5

jclehner
jclehner

Reputation: 1460

I dont think that this is possible. I'm pretty sure that this is mandated by the Java Language Specification, although I couldn't tell you which section exactly.

Upvotes: 2

Related Questions