Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android maven plugin: renameManifestPackage results in Resources$NotFoundException

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

like image 625
phil155 Avatar asked Oct 31 '22 00:10

phil155


1 Answers

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/

like image 59
phil155 Avatar answered Nov 11 '22 10:11

phil155