R.drawable in Libraries

airblaster

Active Member
Licensed User
Longtime User
This is my first time working with Eclipse and Java, so please bear with me.
I'm trying to wrap a Android View Library as a B4A Library.
The progress so far is probably quite good (thanks to Library Generator by XverhelstX getting me started), but there is one problem: On a code line that is using R.Java I get a NoClassDefFoundError.
If I'm not mistaken, this is because the R File and the resources are not included in the library project of the library to be wrapped.
What do I need to do to fix this?
Is there a way to include the resources in an Android library project?
 

corwin42

Expert
Licensed User
Longtime User
You should have asked this in the libraries developers section.

You can not include Android resources accessed by the R class into a library. You have to add the resource files to your B4A project under the Objects\res folder. Then you need a little helper method to access the resources from within your library:

B4X:
   public static int getResourceId(String type, String resourceName) {
      return BA.applicationContext.getResources().getIdentifier(resourceName, type, BA.packageName);
   }

I normally put it in a class named RHelper.

Then everywhere in your library where you want to access the resource use the helper method:

B4X:
RHelper.getResourceId("drawable", "name_of_resource")
 
Upvote 0

airblaster

Active Member
Licensed User
Longtime User
Sounds good, thanks for the detailed reply! I'll try applying it on Monday.
I didn't notice that there is a special forum for library development until you mentioned it, I'll post there next time.
 
Upvote 0
Top