How to solve Android Libraries custom attributes a

2019-02-05 04:23发布

问题:

Over time our Android project has expanded a great deal, and these days we are creating multiple branded APKs from the same source tree. This has become challenging due to the package naming requirements of Android.

We have all of our shared code in an Android Library project, which is included in the main application project. Plus we have Android library 'overlays' for branded resources that get applied for each brand. When we want to build a brand, we include a few extra properties for that brand that end up including the 'overlay' Android libraries before the main shared code Android Library. To illustrate:

AppProject
  Include: BrandALibrary or BrandBLibrary
  Include: SharedLibrary

SharedLibrary
-> src/..
-> res/..
BrandALibrary
-> res/..
BrandBLibrary
-> res/..

The 'BrandALibrary or BrandBLibrary' is switched at build time, by including .properties files at build time using command-line switches. Nothing too fancy there. The build script also must change the package name of the .APK at build time so that the two .APK files can coexist on a given device at the same time (mostly for QA purposes, since we don't expect customers to have both at the same time... though they could).

Everything was working very well until our build broke today after adding a class to our shared code library project. In looking around for a reason why this is ocurring I stumbled upon the following web page, where the fellow had encountered the same issue and had fixed his version of aapt to make this work:

http://devmaze.wordpress.com/2011/05/22/the-case-of-android-libraries-and-custom-xml-attributes-part-2/

During our build, the following errors are presented in the logs:

    -pre-build:

-code-gen:
     [echo] ----------
     [echo] Handling aidl files...
     [aidl] No aidl files to compile.
     [echo] ----------
     [echo] Handling RenderScript files...
[renderscript] No renderscript files to compile.
     [echo] ----------
     [echo] Handling Resources...
     [aapt] Found Deleted Target File
     [aapt] Generating resource IDs...
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: No resource identifier found for attribute 'state_add' in package 'com.myapp'
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: No resource identifier found for attribute 'state_list' in package 'com.myapp'
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\drawable\fancy_button_selector.xml:4: error: No resource identifier found for attribute 'state_add' in package 'com.myapp'
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\drawable\fancy_button_selector.xml:4: error: No resource
identifier found for attribute 'state_list' in package 'com.myapp'
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\drawable\fancy_button_selector.xml:5: error: No resource
identifier found for attribute 'state_add' in package 'com.myapp'
     [aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\drawable\fancy_button_selector_button_selector.xml:5: error: No resource identifier found for attribute 'state_list' in package 'com.myapp'

BUILD FAILED
C:\android-sdk\tools\ant\build.xml:539: The following error occurred while executing this line:
C:\android-sdk\tools\ant\build.xml:568: null returned: 1

My custom attributes are nothing spectacular, I've got a state_add, and state_list defined that I can use to set the default states for a control that shows a button with an icon that indicates which states should be shown. It's like a 'toggle' button in a way, and I might end up replacing this control with a toggle button instead.

I followed this tutorial when I created this button.

My question is, finally (this guy is long winded isn't he?), is there a way to ensure that aapt will 'remap' the package names used in custom attributes. If I even want to write more custom controls into our library, we will need appt to be able to do the following:

  • If a custom namespace is defined in the library project, remap that namespace to the namespace of the android application that is being built.

Am I missing something fundamental with the way custom attributes are mapped during an Ant build? Is there something simple I can do to fix my build issues?

回答1:

This was solved.

For example:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto" >
...