Issue
Resources.getIdentifier(String,String,String) requires the resource package. Unfortunately, if the APK was built with aapt
's --rename-manifest-package
option, the resource package won't be the same as the package returned from Context.getPackageName(). For instance, if AndroidManifest.xml
has the following:
<manifest package="foo.bar" ...>
...
</manifest>
but aapt
is run with --rename-manifest-package foo.bar.baz
, then the APK's resources will have the package name foo.bar
while Context.getPackageName()
will return foo.bar.baz
. Thus, calls to Resources.getIdentifier(String,String,String)
will return 0
whenever the package name from Context.getPackageName()
is used.
How can I get a list of the available resource packages in the APK so I can search them for my resource identifier?
Solution
See https://groups.google.com/forum/?fromgroups=#!topic/adt-dev/cqdjG2TuM-I
from Xavier Ducrohet:
They make the assumption that the package name of the app is the same as the R class. This really is not correct. While the previous build system somewhat implied this, aapt always had the feature to generate the R class in a different package.
Thinking about it there really is no current way to query the app for the package of the R class. It'd have to be provided to the library.
Basically, there is no way to do it. The package name would have to be passed to the library explicitly.
You can automate passing the package name by using a dummy R file:
It's easy to fix though, by retrieving the package name of another resource with Resources.getResourcePackageName().
Let's create a resource id dedicated to that purpose, for example in res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="used_for_package_name_retrieval" type="id"/>
</resources>
And now we get the right package:
Resources res = context.getResources();
String packageName = res.getResourcePackageName(R.id.used_for_package_name_retrieval);
int id = res.getIdentifier("some_drawable", "drawable", packageName);
Answered By - Heath Borders
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.