I encountered the following problem:
The app I implemented includes another project as a dependency (of type aar). Both projects share the same parent pom. The dependency includes resources, which the app is using. To access the resources within the library project, the resource id is fetched by calling context.getResources().getIdentifier(resourceKey, resourceType, packageName)
. I get the package name by calling getPackageName()
on the given context
object.
Before changing the package names of the projects by using
<renameManifestPackage>com.example.newpackagename</renameManifestPackage>
accessing the library resources worked fine. But after renaming the package name of the app I get a android.content.res.Resources$NotFoundException
because the getIdentifier()
call still expects the old package name of the app and calling getPackageName()
returns the new one (as expected).
Now I wonder if I'm missing something or if this a bug in the android maven plugin? https://github.com/simpligility/android-maven-plugin
The answer is: I missed something. I opened a ticket regarding this problem at the project site (https://github.com/simpligility/android-maven-plugin/issues/736). It turned out that what I've seen as a problem is expected behavior of the renameManifestPackage
configuration, since renaming not only the manifest package but also the resources is out of scope for this configuration.
There is, however, a not too ugly workaround for this:
Instead of retrieving the package name from the Context
object, it is possible to retrieve it from the Resources
object. Here comes the part where it gets a bit ugly: A resource with the single purpose of retrieving the package name needs to be added since other resources are usually subjects to change:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The only purpose of this resource is to retrieve the resource package name -->
<item name="used_for_package_name_retrieval" type="id"/>
</resources>
Now you can retrieve the resource package name as follows:
Resources resources = context.getResources();
String packageName = resources.getResourcePackageName(R.id.used_for_package_name_retrieval);
Credit goes to: http://www.piwai.info/renaming-android-manifest-package/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With