Wenny
Wenny

Reputation: 1

How to get resources from child's ./res/ directory in Android App

I have a android app folder structure like:

phone
├── res
└── src
└── tablet 
     ├── res
     └── src

Now there is an activity.java in phone/src would like to use the resources (.xml file) from phone/tablet/res, how do I access the resources from child's directory? Thanks!

Tried import both .phone.R and phone.tablet.R but the latter cannot find.

Upvotes: 0

Views: 40

Answers (1)

Myroslav
Myroslav

Reputation: 1237

Looks like your folder structure is not the one, used in Android Studio by default. I would suggest to change it to be according to default structure, because in that case Gradle can merge all the resources automatically etc., otherwise you would probably need to add some custom config in your build.gradle. Below is only example with using modules/sub-modules. But maybe you need flavors instead, if you want to have separate code versions for different build types.

<module name> // i.e. phone
|--src
|----main
|------res
|--<sub-module name> // i.e. tablet
|----src
|------main
|--------res
|----build.gradle
|--build.gradle

Then in your module's build.gradle you add sub-module as dependency - this will allow to use sub-module's codebase from module level.

Alternatively, if you don't need separate modules, but just want to group tablet related resources in separate folder

phone
|--src
|----main
|------res
|--------tablet

Then in your build.gradle you need to specify res paths:

android {
    sourceSets {
        main.res.srcDirs = [
           'src/main/res',
           'src/main/res/tablet'
        ]

Upvotes: 1

Related Questions