Mir-Ismaili
Mir-Ismaili

Reputation: 16948

Define a resource under another package (except the main package)

I have a Gradle Android project (in Android Studio) that includes two packages (both in app module). You know then there are two sets of folders under src/main/java directory:

src/main/java/the/first/pkg/...{The Files Under First Package}...
src/main/java/the/second/pkg/...{The Files Under Second Package}...

Note: Both first and second packages are in a folder that named main. But I mean, from the main package, my first package. In other words you can see this in main/AndroidManifest.xml file:

<manifest ... package="the.first.pkg">


But what about src/main/res directory?! All resources have been located in the root of this path (not categorized by package name)!

Now how can I define a resource (like a layout) NOT under the first package, but under the second package? (So I would be able to access it in my java code through the.second.pkg.R.layout.resource_name.)

Upvotes: 1

Views: 472

Answers (1)

Denysole
Denysole

Reputation: 4051

You should change your scheme a little bit. By default, Android Studio creates main package and put all your sources in it, like this:

enter image description here

If you want to get different res folders, for different packages you should define another package inside src package:

enter image description here

Also, Android Studio can generate it for you:

  1. Open the Project pane and select the Project view from the drop-down menu at the top of the pane.
  2. Navigate to MyProject/app/src/.
  3. Right-click the src directory and select New > Folder > Java Folder.
  4. From the drop-down menu next to Target Source Set, select debug.
  5. Click Finish.

It's called source sets more detail you can get from this link.

Upvotes: 1

Related Questions