0

I have encountered the following problem:

1) I have a Library ("[x] Is Library" selected) created in Eclipse. It has 2 layouts (main.xml and another one) included.

2) It works very well when I use it within one Eclipse Workspace - I can easily include it in another project and use it without issues.

3) My workspace got huge recently and Eclipse even slower than earlier (and I am working on an Intel Atom "powered" netbook). So I decided to create new one.

4) I have copied the mylibrary.jar file from its bin folder, including the library.jar in new project worked well. Everything compiles, but at runtime an java.lang.NoClassDefFoundError: my.namespace.mylibrary.R$layout is raised at startup.

What I have done:

1) I tried to find solution here on stackoverflow but NoClassDefFoundError on external library project for Android is what I found but I already have the jar file added.

2) Then I have looked into the jar file (renamed to zip & unpacked) to see that the R$layout class is not included there.

How can I get layout classes included into the library.jar file?

Is this really the source of my issue or I am doing something else wrong?

Thanks for advice.

Edit: I have checked and removing xml layouts from the library (e.g. creating a dummy one in code) stops the NoClassDefFoundError. Yet looking at admob.jar file seems it is possible to include R&layout class into the jar file.

Community
  • 1
  • 1
Piotr
  • 1,597
  • 3
  • 18
  • 25

3 Answers3

3

I had the same problem. The problem was this line in my derived Activity:

setContentView(R.layout.my_layout);

Eclipse had imported the R from my library project. Problem solved by changing it to import the right R.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Kiros86
  • 31
  • 2
0

Seems the only answer to this one is to create layouts in .java file not in .xml. Tested this one and works well. No idea how admob get theirs layouts included in the .jar file.

Piotr
  • 1,597
  • 3
  • 18
  • 25
-1

To add a library project first make sure that your library project is actually marked as a library project. Right click on the project node --> properties --> Android --> Click the 'Is Library' checkbox --> OK.

Then in your main Android project: Right click the project node --> properties --> Android --> Add --> Select your library project --> OK --> OK

Damian
  • 8,062
  • 4
  • 42
  • 43
  • Thanks Damian, I do know how to mark library as such - exactly with the "[ ] is Library" piece - I have it marked as such with "[x] is Library". Also including the library in a project is not a mystery for me. The way you proposed works only within a single Eclipse Workspace. My itent is to create new one - Eclipse gets slower with more projects within the workspace, navigating through the workspace is easier with less projects. But I am unable to get this done. Meanwhile I have checked admob.jar and they managed to get R$layout.class into the jar file ... not clear how to do it though. – Piotr Jan 06 '12 at 10:40