13

I've create a module at Android Studio. In module code, I want to show a dialog which uses a layout defined in the module. When I reference layout like net.gwtr.module.R.layout.my_dialog_layout, I get this exception;

java.lang.ClassNotFoundException: Didn't find class "net.gwtr.module.R$layout" on path: DexPathList[[zip file "/data/app/net.gwtr.moduletest-1.apk"],nativeLibraryDirectories=[/data/app-lib/net.gwtr.moduletest-1, /vendor/lib, /system/lib]] 

I think the reason is that resources are merged when you add a module to project. It did not create different resource ids for the module package name. So i cannot reach to resources from module package.

How can I reference resources of module in module code?

Edit1: I reference resource like this;

Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.my_dialog_layout); dialog.show(); 

Edit2: I found a way but i don't want to use this every time when I reference resource.

I can reach resources when I get resource id with this;

context.getResources().getIdentifier("my_dialog_layout", "layout", context.getPackageName()) 
4
  • How are you referencing R? Show your code please. Commented Aug 21, 2014 at 8:32
  • Reproduced you problem: buildToolsVersion '20.0.0' Gradle 0.12.2 Android Studio 0.8.6. Also tested it with other resources - R class not found. Commented Aug 21, 2014 at 14:31
  • @ferar1988 thanks for sharing your solution , did you find another more convenient way to do this ? Commented Apr 28, 2016 at 7:39
  • There is a long treatment of these questions here: stackoverflow.com/questions/62694206/… Commented Jul 18, 2020 at 8:48

5 Answers 5

4

In your gradle file from your module.

Are you put compile your other module?

compile project(':othermodule') 
Sign up to request clarification or add additional context in comments.

3 Comments

yes, i can reach any module classes. But module cannot reach its own resources.
Have you compiled it as apklib?
No, i just create a new module inside the project.
4

Yes, resources from libraries are merged into your application (so basically there's a global namespace under R; this is why you have to choose resource names in modules with a little care, e.g. use a prefix, to prevent accidental mix-up with resources from other modules).

Within the defining library module, and your application module by virtue of the resource merging, you should just be able to refer to R.layout.my_dialog_layout (android studio should auto-complete). I've a similar situation working fine in my project (@jimpanzer: I tried under same versions as you, with compileSdkVersion = 16 and 20).

Some suggestions:

  • make sure there are no import statements of R anywhere. As far as I can see you shouldn't need any, if you either address library resources from your application module, or from the same library module.

  • clean your project and rebuild, perhaps even android studio caches. In the old days, sometimes R.java would get screwed up.

Comments

2

I'm converting a legacy project to android studio and managed to get the package names mixed up in the module. Although the R file was generated it was in the wrong package.

In the module check the resource to id mapping file

build->generated/source->debug->(app package)->R

or

build->generated/source->release->(app package)->R

The package of this file should match the import of R e.g. in your activity

Comments

2

I had to add the package name for the resource, e.g. instead of R.drawable.icon, I needed com.example.brand.module_name.R.drawable.icon

Comments

1

import net.gwtr.module.R.* and access the layout via getResources() like you had before

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.