Reputation: 4544
I am facing problems getting the id of a resource by name when using a library project:
int resId = context.getResources().getIdentifier("myresname", "drawable", "net.iberdroid.mygamecore");
It seems like this always returns 0 when using the project as a library, but it were working flawlessly while it was an standalone project.
net.iberdroid.mygamecore is where those resources resides, and in fact they are there.
That code is even executed within the library project package, not from the android project that links it.
Any ideas?
Upvotes: 0
Views: 86
Reputation: 4544
I found a solution, but it is a bit weird and since I use a library for not making repetitive changes on different versions of same game (tipically free and pay versions), also a bit annoying.
In this context, net.iberdroid.mygamecore is the Library Project shared by payment and free versions, and net.iberdroid.gamefree and net.iberdroid.gamepro are the Android Projects for free and pay versions of the game.
If I change the package name on the previous code by "net.iberdroid.gamefree" it works fine. But the problem is that that code is within the library, and will be used also by "net.iberdroid.gamepro", so putting the package name of the free version will broken the pay one and viceversa.
So if I want to still share that code within the library, I will need to get the package name of the project dinamically. So this will do the trick:
public static int getDrawableId(String dname) {
String pn = context.getPackageName();
int resId = context.getResources().getIdentifier(dname, "drawable", pn);
return resId;
}
Upvotes: 1