Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 602
Guilherme Avatar asked Jul 26 '26 00:07

Guilherme


1 Answers

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.

like image 148
Scott Barta Avatar answered Jul 28 '26 11:07

Scott Barta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!