Frameworks/base: Wall Werror in libs/androidfw

Turn on -Wall -Werror in libs/androidfw. Fix warnings. Refactor
some code.

Change-Id: I66fe54ace433c15dee5de328b149ca142f74b2dd
diff --git a/libs/androidfw/Android.mk b/libs/androidfw/Android.mk
index 376b101..9c0a747 100644
--- a/libs/androidfw/Android.mk
+++ b/libs/androidfw/Android.mk
@@ -52,8 +52,10 @@
 
 LOCAL_CFLAGS += -DSTATIC_ANDROIDFW_FOR_TOOLS
 
+LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
+
 LOCAL_C_INCLUDES := \
-	external/zlib
+    external/zlib
 
 LOCAL_STATIC_LIBRARIES := liblog libziparchive-host libutils
 
@@ -69,11 +71,11 @@
 LOCAL_SRC_FILES:= $(deviceSources)
 
 LOCAL_SHARED_LIBRARIES := \
-	libbinder \
-	liblog \
-	libcutils \
-	libutils \
-	libz
+    libbinder \
+    liblog \
+    libcutils \
+    libutils \
+    libz
 
 LOCAL_STATIC_LIBRARIES := libziparchive
 
@@ -85,6 +87,8 @@
 
 LOCAL_MODULE_TAGS := optional
 
+LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
+
 include $(BUILD_SHARED_LIBRARY)
 
 
diff --git a/libs/androidfw/Asset.cpp b/libs/androidfw/Asset.cpp
index 589211f..4b3382e 100644
--- a/libs/androidfw/Asset.cpp
+++ b/libs/androidfw/Asset.cpp
@@ -45,6 +45,8 @@
 # define O_BINARY 0
 #endif
 
+static const bool kIsDebug = false;
+
 static Mutex gAssetLock;
 static int32_t gCount = 0;
 static Asset* gHead = NULL;
@@ -89,7 +91,9 @@
         gTail->mNext = this;
         gTail = this;
     }
-    //ALOGI("Creating Asset %p #%d\n", this, gCount);
+    if (kIsDebug) {
+        ALOGI("Creating Asset %p #%d\n", this, gCount);
+    }
 }
 
 Asset::~Asset(void)
@@ -109,7 +113,9 @@
         mPrev->mNext = mNext;
     }
     mNext = mPrev = NULL;
-    //ALOGI("Destroying Asset in %p #%d\n", this, gCount);
+    if (kIsDebug) {
+        ALOGI("Destroying Asset in %p #%d\n", this, gCount);
+    }
 }
 
 /*
diff --git a/libs/androidfw/AssetManager.cpp b/libs/androidfw/AssetManager.cpp
index de6a33cf..e5c5b10 100644
--- a/libs/androidfw/AssetManager.cpp
+++ b/libs/androidfw/AssetManager.cpp
@@ -64,6 +64,8 @@
 
 using namespace android;
 
+static const bool kIsDebug = false;
+
 /*
  * Names for default app, locale, and vendor.  We might want to change
  * these to be an actual locale, e.g. always use en-US as the default.
@@ -152,15 +154,19 @@
       mResources(NULL), mConfig(new ResTable_config),
       mCacheMode(cacheMode), mCacheValid(false)
 {
-    int count = android_atomic_inc(&gCount)+1;
-    //ALOGI("Creating AssetManager %p #%d\n", this, count);
+    int count = android_atomic_inc(&gCount) + 1;
+    if (kIsDebug) {
+        ALOGI("Creating AssetManager %p #%d\n", this, count);
+    }
     memset(mConfig, 0, sizeof(ResTable_config));
 }
 
 AssetManager::~AssetManager(void)
 {
     int count = android_atomic_dec(&gCount);
-    //ALOGI("Destroying AssetManager in %p #%d\n", this, count);
+    if (kIsDebug) {
+        ALOGI("Destroying AssetManager in %p #%d\n", this, count);
+    }
 
     delete mConfig;
     delete mResources;
@@ -1864,7 +1870,9 @@
     : mPath(path), mZipFile(NULL), mModWhen(modWhen),
       mResourceTableAsset(NULL), mResourceTable(NULL)
 {
-    //ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
+    if (kIsDebug) {
+        ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
+    }
     ALOGV("+++ opening zip '%s'\n", mPath.string());
     mZipFile = ZipFileRO::open(mPath.string());
     if (mZipFile == NULL) {
@@ -1958,7 +1966,9 @@
 
 AssetManager::SharedZip::~SharedZip()
 {
-    //ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
+    if (kIsDebug) {
+        ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
+    }
     if (mResourceTable != NULL) {
         delete mResourceTable;
     }
diff --git a/libs/androidfw/BackupData.cpp b/libs/androidfw/BackupData.cpp
index d16d5498..2b45a70 100644
--- a/libs/androidfw/BackupData.cpp
+++ b/libs/androidfw/BackupData.cpp
@@ -27,7 +27,7 @@
 
 namespace android {
 
-static const bool DEBUG = false;
+static const bool kIsDebug = false;
 
 /*
  * File Format (v1):
@@ -62,7 +62,7 @@
      m_entityCount(0)
 {
     m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
-    if (DEBUG) ALOGI("BackupDataWriter(%d) @ %ld", fd, (long)m_pos);
+    if (kIsDebug) ALOGI("BackupDataWriter(%d) @ %ld", fd, (long)m_pos);
 }
 
 BackupDataWriter::~BackupDataWriter()
@@ -79,7 +79,7 @@
     paddingSize = padding_extra(n);
     if (paddingSize > 0) {
         uint32_t padding = 0xbcbcbcbc;
-        if (DEBUG) ALOGI("writing %zd padding bytes for %d", paddingSize, n);
+        if (kIsDebug) ALOGI("writing %zd padding bytes for %d", paddingSize, n);
         amt = write(m_fd, &padding, paddingSize);
         if (amt != paddingSize) {
             m_status = errno;
@@ -112,7 +112,7 @@
     } else {
         k = key;
     }
-    if (DEBUG) {
+    if (kIsDebug) {
         ALOGD("Writing header: prefix='%s' key='%s' dataSize=%zu", m_keyPrefix.string(),
                 key.string(), dataSize);
     }
@@ -126,7 +126,7 @@
     header.keyLen = tolel(keyLen);
     header.dataSize = tolel(dataSize);
 
-    if (DEBUG) ALOGI("writing entity header, %zu bytes", sizeof(entity_header_v1));
+    if (kIsDebug) ALOGI("writing entity header, %zu bytes", sizeof(entity_header_v1));
     amt = write(m_fd, &header, sizeof(entity_header_v1));
     if (amt != sizeof(entity_header_v1)) {
         m_status = errno;
@@ -134,7 +134,7 @@
     }
     m_pos += amt;
 
-    if (DEBUG) ALOGI("writing entity header key, %zd bytes", keyLen+1);
+    if (kIsDebug) ALOGI("writing entity header key, %zd bytes", keyLen+1);
     amt = write(m_fd, k.string(), keyLen+1);
     if (amt != keyLen+1) {
         m_status = errno;
@@ -152,10 +152,10 @@
 status_t
 BackupDataWriter::WriteEntityData(const void* data, size_t size)
 {
-    if (DEBUG) ALOGD("Writing data: size=%lu", (unsigned long) size);
+    if (kIsDebug) ALOGD("Writing data: size=%lu", (unsigned long) size);
 
     if (m_status != NO_ERROR) {
-        if (DEBUG) {
+        if (kIsDebug) {
             ALOGD("Not writing data - stream in error state %d (%s)", m_status, strerror(m_status));
         }
         return m_status;
@@ -167,7 +167,7 @@
     ssize_t amt = write(m_fd, data, size);
     if (amt != (ssize_t)size) {
         m_status = errno;
-        if (DEBUG) ALOGD("write returned error %d (%s)", m_status, strerror(m_status));
+        if (kIsDebug) ALOGD("write returned error %d (%s)", m_status, strerror(m_status));
         return m_status;
     }
     m_pos += amt;
@@ -189,7 +189,7 @@
 {
     memset(&m_header, 0, sizeof(m_header));
     m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
-    if (DEBUG) ALOGI("BackupDataReader(%d) @ %ld", fd, (long)m_pos);
+    if (kIsDebug) ALOGI("BackupDataReader(%d) @ %ld", fd, (long)m_pos);
 }
 
 BackupDataReader::~BackupDataReader()
@@ -342,15 +342,19 @@
         return -1;
     }
     int remaining = m_dataEndPos - m_pos;
-    //ALOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
-    //        size, m_pos, m_dataEndPos, remaining);
+    if (kIsDebug) {
+        ALOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
+                size, m_pos, m_dataEndPos, remaining);
+    }
     if (remaining <= 0) {
         return 0;
     }
     if (((int)size) > remaining) {
         size = remaining;
     }
-    //ALOGD("   reading %d bytes", size);
+    if (kIsDebug) {
+        ALOGD("   reading %d bytes", size);
+    }
     int amt = read(m_fd, data, size);
     if (amt < 0) {
         m_status = errno;
diff --git a/libs/androidfw/BackupHelpers.cpp b/libs/androidfw/BackupHelpers.cpp
index 52dce9f..36a4cfe 100644
--- a/libs/androidfw/BackupHelpers.cpp
+++ b/libs/androidfw/BackupHelpers.cpp
@@ -68,14 +68,11 @@
 
 const static int CURRENT_METADATA_VERSION = 1;
 
-#if 1
-#define LOGP(f, x...)
-#else
+static const bool kIsDebug = false;
 #if TEST_BACKUP_HELPERS
-#define LOGP(f, x...) printf(f "\n", x)
+#define LOGP(f, x...) if (kIsDebug) printf(f "\n", x)
 #else
-#define LOGP(x...) ALOGD(x)
-#endif
+#define LOGP(x...) if (kIsDebug) ALOGD(x)
 #endif
 
 const static int ROUND_UP[4] = { 0, 3, 2, 1 };
@@ -205,13 +202,6 @@
 }
 
 static int
-write_delete_file(BackupDataWriter* dataStream, const String8& key)
-{
-    LOGP("write_delete_file %s\n", key.string());
-    return dataStream->WriteEntityHeader(key, -1);
-}
-
-static int
 write_update_file(BackupDataWriter* dataStream, int fd, int mode, const String8& key,
         char const* realFilename)
 {
@@ -225,8 +215,6 @@
     file_metadata_v1 metadata;
 
     char* buf = (char*)malloc(bufsize);
-    int crc = crc32(0L, Z_NULL, 0);
-
 
     fileSize = lseek(fd, 0, SEEK_END);
     lseek(fd, 0, SEEK_SET);
@@ -442,18 +430,6 @@
     return 0;
 }
 
-// Utility function, equivalent to stpcpy(): perform a strcpy, but instead of
-// returning the initial dest, return a pointer to the trailing NUL.
-static char* strcpy_ptr(char* dest, const char* str) {
-    if (dest && str) {
-        while ((*dest = *str) != 0) {
-            dest++;
-            str++;
-        }
-    }
-    return dest;
-}
-
 static void calc_tar_checksum(char* buf) {
     // [ 148 :   8 ] checksum -- to be calculated with this field as space chars
     memset(buf + 148, ' ', 8);
@@ -635,7 +611,6 @@
 
         // construct the pax extended header data block
         memset(paxData, 0, BUFSIZE - (paxData - buf));
-        int len;
 
         // size header -- calc len in digits by actually rendering the number
         // to a string - brute force but simple
@@ -1200,7 +1175,6 @@
     size_t bufSize = strlen(str)+1;
     char* buf = (char*)malloc(bufSize);
     String8 string;
-    int cookie = 0x11111111;
     size_t actualSize;
     bool done;
     int type;
@@ -1490,7 +1464,6 @@
 backup_helper_test_null_base()
 {
     int err;
-    int oldSnapshotFD;
     int dataStreamFD;
     int newSnapshotFD;
 
@@ -1539,7 +1512,6 @@
 backup_helper_test_missing_file()
 {
     int err;
-    int oldSnapshotFD;
     int dataStreamFD;
     int newSnapshotFD;
 
diff --git a/libs/androidfw/ObbFile.cpp b/libs/androidfw/ObbFile.cpp
index ec59f06..195fa9a 100644
--- a/libs/androidfw/ObbFile.cpp
+++ b/libs/androidfw/ObbFile.cpp
@@ -122,7 +122,7 @@
         if (fileLength < 0) {
             ALOGW("error seeking in ObbFile: %s\n", strerror(errno));
         } else {
-            ALOGW("file is only %lld (less than %d minimum)\n", fileLength, kFooterMinSize);
+            ALOGW("file is only %lld (less than %d minimum)\n", (long long int)fileLength, kFooterMinSize);
         }
         return false;
     }
@@ -150,8 +150,8 @@
         footerSize = get4LE((unsigned char*)footer);
         if (footerSize > (size_t)fileLength - kFooterTagSize
                 || footerSize > kMaxBufSize) {
-            ALOGW("claimed footer size is too large (0x%08zx; file size is 0x%08llx)\n",
-                    footerSize, fileLength);
+            ALOGW("claimed footer size is too large (0x%08zx; file size is 0x%08lld)\n",
+                    footerSize, (long long int)fileLength);
             return false;
         }
 
@@ -164,7 +164,7 @@
 
     off64_t fileOffset = fileLength - footerSize - kFooterTagSize;
     if (lseek64(fd, fileOffset, SEEK_SET) != fileOffset) {
-        ALOGW("seek %lld failed: %s\n", fileOffset, strerror(errno));
+        ALOGW("seek %lld failed: %s\n", (long long int)fileOffset, strerror(errno));
         return false;
     }
 
diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp
index 825071a6..e2263d2 100644
--- a/libs/androidfw/ResourceTypes.cpp
+++ b/libs/androidfw/ResourceTypes.cpp
@@ -27,6 +27,10 @@
 #include <utils/String16.h>
 #include <utils/String8.h>
 
+#ifdef HAVE_ANDROID_OS
+#include <binder/TextOutput.h>
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <memory.h>
@@ -38,15 +42,6 @@
 #define INT32_MAX ((int32_t)(2147483647))
 #endif
 
-#define STRING_POOL_NOISY(x) //x
-#define XML_NOISY(x) //x
-#define TABLE_NOISY(x) //x
-#define TABLE_GETENTRY(x) //x
-#define TABLE_SUPER_NOISY(x) //x
-#define LOAD_TABLE_NOISY(x) //x
-#define TABLE_THEME(x) //x
-#define LIB_NOISY(x) //x
-
 namespace android {
 
 #ifdef HAVE_WINSOCK
@@ -72,6 +67,19 @@
 #define APP_PACKAGE_ID      0x7f
 #define SYS_PACKAGE_ID      0x01
 
+static const bool kDebugStringPoolNoisy = false;
+static const bool kDebugXMLNoisy = false;
+static const bool kDebugTableNoisy = false;
+static const bool kDebugTableGetEntry = false;
+static const bool kDebugTableSuperNoisy = false;
+static const bool kDebugLoadTableNoisy = false;
+static const bool kDebugLoadTableSuperNoisy = false;
+static const bool kDebugTableTheme = false;
+static const bool kDebugResXMLTree = false;
+static const bool kDebugLibNoisy = false;
+
+// TODO: This code uses 0xFFFFFFFF converted to bag_set* as a sentinel value. This is bad practice.
+
 // Standard C isspace() is only required to look at the low byte of its input, so
 // produces incorrect results for UTF-16 characters.  For safety's sake, assume that
 // any high-byte UTF-16 code point is not whitespace.
@@ -719,8 +727,10 @@
 
                     if (mCache == NULL) {
 #ifndef HAVE_ANDROID_OS
-                        STRING_POOL_NOISY(ALOGI("CREATING STRING CACHE OF %d bytes",
-                                mHeader->stringCount*sizeof(char16_t**)));
+                        if (kDebugStringPoolNoisy) {
+                            ALOGI("CREATING STRING CACHE OF %zu bytes",
+                                    mHeader->stringCount*sizeof(char16_t**));
+                        }
 #else
                         // We do not want to be in this case when actually running Android.
                         ALOGW("CREATING STRING CACHE OF %d bytes",
@@ -753,7 +763,9 @@
                         return NULL;
                     }
 
-                    STRING_POOL_NOISY(ALOGI("Caching UTF8 string: %s", u8str));
+                    if (kDebugStringPoolNoisy) {
+                        ALOGI("Caching UTF8 string: %s", u8str);
+                    }
                     utf8_to_utf16(u8str, u8len, u16str);
                     mCache[idx] = u16str;
                     return u16str;
@@ -843,7 +855,9 @@
     size_t len;
 
     if ((mHeader->flags&ResStringPool_header::UTF8_FLAG) != 0) {
-        STRING_POOL_NOISY(ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string()));
+        if (kDebugStringPoolNoisy) {
+            ALOGI("indexOfString UTF-8: %s", String8(str, strLen).string());
+        }
 
         // The string pool contains UTF 8 strings; we don't want to cause
         // temporary UTF-16 strings to be created as we search.
@@ -869,10 +883,14 @@
                 } else {
                     c = -1;
                 }
-                STRING_POOL_NOISY(ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
-                             (const char*)s, c, (int)l, (int)mid, (int)h));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
+                            (const char*)s, c, (int)l, (int)mid, (int)h);
+                }
                 if (c == 0) {
-                    STRING_POOL_NOISY(ALOGI("MATCH!"));
+                    if (kDebugStringPoolNoisy) {
+                        ALOGI("MATCH!");
+                    }
                     free(convBuffer);
                     return mid;
                 } else if (c < 0) {
@@ -891,18 +909,22 @@
             const size_t str8Len = str8.size();
             for (int i=mHeader->stringCount-1; i>=0; i--) {
                 const char* s = string8At(i, &len);
-                STRING_POOL_NOISY(ALOGI("Looking at %s, i=%d\n",
-                             String8(s).string(),
-                             i));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
+                }
                 if (s && str8Len == len && memcmp(s, str8.string(), str8Len) == 0) {
-                    STRING_POOL_NOISY(ALOGI("MATCH!"));
+                    if (kDebugStringPoolNoisy) {
+                        ALOGI("MATCH!");
+                    }
                     return i;
                 }
             }
         }
 
     } else {
-        STRING_POOL_NOISY(ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string()));
+        if (kDebugStringPoolNoisy) {
+            ALOGI("indexOfString UTF-16: %s", String8(str, strLen).string());
+        }
 
         if (mHeader->flags&ResStringPool_header::SORTED_FLAG) {
             // Do a binary search for the string...
@@ -914,11 +936,14 @@
                 mid = l + (h - l)/2;
                 const char16_t* s = stringAt(mid, &len);
                 int c = s ? strzcmp16(s, len, str, strLen) : -1;
-                STRING_POOL_NOISY(ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
-                             String8(s).string(),
-                             c, (int)l, (int)mid, (int)h));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("Looking at %s, cmp=%d, l/mid/h=%d/%d/%d\n",
+                            String8(s).string(), c, (int)l, (int)mid, (int)h);
+                }
                 if (c == 0) {
-                    STRING_POOL_NOISY(ALOGI("MATCH!"));
+                    if (kDebugStringPoolNoisy) {
+                        ALOGI("MATCH!");
+                    }
                     return mid;
                 } else if (c < 0) {
                     l = mid + 1;
@@ -933,11 +958,13 @@
             // block, start searching at the back.
             for (int i=mHeader->stringCount-1; i>=0; i--) {
                 const char16_t* s = stringAt(i, &len);
-                STRING_POOL_NOISY(ALOGI("Looking at %s, i=%d\n",
-                             String8(s).string(),
-                             i));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("Looking at %s, i=%d\n", String8(s).string(), i);
+                }
                 if (s && strLen == len && strzcmp16(s, len, str, strLen) == 0) {
-                    STRING_POOL_NOISY(ALOGI("MATCH!"));
+                    if (kDebugStringPoolNoisy) {
+                        ALOGI("MATCH!");
+                    }
                     return i;
                 }
             }
@@ -1138,7 +1165,9 @@
 {
     int32_t id = getAttributeNamespaceID(idx);
     //printf("attribute namespace=%d  idx=%d  event=%p\n", id, idx, mEventCode);
-    //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id));
+    if (kDebugXMLNoisy) {
+        printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
+    }
     return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
 }
 
@@ -1146,7 +1175,9 @@
 {
     int32_t id = getAttributeNamespaceID(idx);
     //printf("attribute namespace=%d  idx=%d  event=%p\n", id, idx, mEventCode);
-    //XML_NOISY(printf("getAttributeNamespace 0x%x=0x%x\n", idx, id));
+    if (kDebugXMLNoisy) {
+        printf("getAttributeNamespace 0x%zx=0x%x\n", idx, id);
+    }
     return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
 }
 
@@ -1169,7 +1200,9 @@
 {
     int32_t id = getAttributeNameID(idx);
     //printf("attribute name=%d  idx=%d  event=%p\n", id, idx, mEventCode);
-    //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id));
+    if (kDebugXMLNoisy) {
+        printf("getAttributeName 0x%zx=0x%x\n", idx, id);
+    }
     return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
 }
 
@@ -1177,7 +1210,9 @@
 {
     int32_t id = getAttributeNameID(idx);
     //printf("attribute name=%d  idx=%d  event=%p\n", id, idx, mEventCode);
-    //XML_NOISY(printf("getAttributeName 0x%x=0x%x\n", idx, id));
+    if (kDebugXMLNoisy) {
+        printf("getAttributeName 0x%zx=0x%x\n", idx, id);
+    }
     return id >= 0 ? mTree.mStrings.string8At(id, outLen) : NULL;
 }
 
@@ -1208,7 +1243,9 @@
 const char16_t* ResXMLParser::getAttributeStringValue(size_t idx, size_t* outLen) const
 {
     int32_t id = getAttributeValueStringID(idx);
-    //XML_NOISY(printf("getAttributeValue 0x%x=0x%x\n", idx, id));
+    if (kDebugXMLNoisy) {
+        printf("getAttributeValue 0x%zx=0x%x\n", idx, id);
+    }
     return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
 }
 
@@ -1299,54 +1336,69 @@
                 ns8 = String8(ns, nsLen);
             }
             attr8 = String8(attr, attrLen);
-            STRING_POOL_NOISY(ALOGI("indexOfAttribute UTF8 %s (%d) / %s (%d)", ns8.string(), nsLen,
-                    attr8.string(), attrLen));
+            if (kDebugStringPoolNoisy) {
+                ALOGI("indexOfAttribute UTF8 %s (%zu) / %s (%zu)", ns8.string(), nsLen,
+                        attr8.string(), attrLen);
+            }
             for (size_t i=0; i<N; i++) {
                 size_t curNsLen = 0, curAttrLen = 0;
                 const char* curNs = getAttributeNamespace8(i, &curNsLen);
                 const char* curAttr = getAttributeName8(i, &curAttrLen);
-                STRING_POOL_NOISY(ALOGI("  curNs=%s (%d), curAttr=%s (%d)", curNs, curNsLen,
-                        curAttr, curAttrLen));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("  curNs=%s (%zu), curAttr=%s (%zu)", curNs, curNsLen, curAttr, curAttrLen);
+                }
                 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
                         && memcmp(attr8.string(), curAttr, attrLen) == 0) {
                     if (ns == NULL) {
                         if (curNs == NULL) {
-                            STRING_POOL_NOISY(ALOGI("  FOUND!"));
+                            if (kDebugStringPoolNoisy) {
+                                ALOGI("  FOUND!");
+                            }
                             return i;
                         }
                     } else if (curNs != NULL) {
                         //printf(" --> ns=%s, curNs=%s\n",
                         //       String8(ns).string(), String8(curNs).string());
                         if (memcmp(ns8.string(), curNs, nsLen) == 0) {
-                            STRING_POOL_NOISY(ALOGI("  FOUND!"));
+                            if (kDebugStringPoolNoisy) {
+                                ALOGI("  FOUND!");
+                            }
                             return i;
                         }
                     }
                 }
             }
         } else {
-            STRING_POOL_NOISY(ALOGI("indexOfAttribute UTF16 %s (%d) / %s (%d)",
-                    String8(ns, nsLen).string(), nsLen,
-                    String8(attr, attrLen).string(), attrLen));
+            if (kDebugStringPoolNoisy) {
+                ALOGI("indexOfAttribute UTF16 %s (%zu) / %s (%zu)",
+                        String8(ns, nsLen).string(), nsLen,
+                        String8(attr, attrLen).string(), attrLen);
+            }
             for (size_t i=0; i<N; i++) {
                 size_t curNsLen = 0, curAttrLen = 0;
                 const char16_t* curNs = getAttributeNamespace(i, &curNsLen);
                 const char16_t* curAttr = getAttributeName(i, &curAttrLen);
-                STRING_POOL_NOISY(ALOGI("  curNs=%s (%d), curAttr=%s (%d)",
-                        String8(curNs, curNsLen).string(), curNsLen,
-                        String8(curAttr, curAttrLen).string(), curAttrLen));
+                if (kDebugStringPoolNoisy) {
+                    ALOGI("  curNs=%s (%zu), curAttr=%s (%zu)",
+                            String8(curNs, curNsLen).string(), curNsLen,
+                            String8(curAttr, curAttrLen).string(), curAttrLen);
+                }
                 if (curAttr != NULL && curNsLen == nsLen && curAttrLen == attrLen
                         && (memcmp(attr, curAttr, attrLen*sizeof(char16_t)) == 0)) {
                     if (ns == NULL) {
                         if (curNs == NULL) {
-                            STRING_POOL_NOISY(ALOGI("  FOUND!"));
+                            if (kDebugStringPoolNoisy) {
+                                ALOGI("  FOUND!");
+                            }
                             return i;
                         }
                     } else if (curNs != NULL) {
                         //printf(" --> ns=%s, curNs=%s\n",
                         //       String8(ns).string(), String8(curNs).string());
                         if (memcmp(ns, curNs, nsLen*sizeof(char16_t)) == 0) {
-                            STRING_POOL_NOISY(ALOGI("  FOUND!"));
+                            if (kDebugStringPoolNoisy) {
+                                ALOGI("  FOUND!");
+                            }
                             return i;
                         }
                     }
@@ -1394,7 +1446,9 @@
     do {
         const ResXMLTree_node* next = (const ResXMLTree_node*)
             (((const uint8_t*)mCurNode) + dtohl(mCurNode->header.size));
-        //ALOGW("Next node: prev=%p, next=%p\n", mCurNode, next);
+        if (kDebugXMLNoisy) {
+            ALOGI("Next node: prev=%p, next=%p\n", mCurNode, next);
+        }
 
         if (((const uint8_t*)next) >= mTree.mDataEnd) {
             mCurNode = NULL;
@@ -1471,7 +1525,9 @@
     , mDynamicRefTable(dynamicRefTable)
     , mError(NO_INIT), mOwnedData(NULL)
 {
-    //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
+    if (kDebugResXMLTree) {
+        ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
+    }
     restart();
 }
 
@@ -1480,13 +1536,17 @@
     , mDynamicRefTable(NULL)
     , mError(NO_INIT), mOwnedData(NULL)
 {
-    //ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
+    if (kDebugResXMLTree) {
+        ALOGI("Creating ResXMLTree %p #%d\n", this, android_atomic_inc(&gCount)+1);
+    }
     restart();
 }
 
 ResXMLTree::~ResXMLTree()
 {
-    //ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
+    if (kDebugResXMLTree) {
+        ALOGI("Destroying ResXMLTree in %p #%d\n", this, android_atomic_dec(&gCount)-1);
+    }
     uninit();
 }
 
@@ -1539,8 +1599,10 @@
         }
         const uint16_t type = dtohs(chunk->type);
         const size_t size = dtohl(chunk->size);
-        XML_NOISY(printf("Scanning @ %p: type=0x%x, size=0x%x\n",
-                     (void*)(((uint32_t)chunk)-((uint32_t)mHeader)), type, size));
+        if (kDebugXMLNoisy) {
+            printf("Scanning @ %p: type=0x%x, size=0x%zx\n",
+                    (void*)(((uintptr_t)chunk)-((uintptr_t)mHeader)), type, size);
+        }
         if (type == RES_STRING_POOL_TYPE) {
             mStrings.setTo(chunk, size);
         } else if (type == RES_XML_RESOURCE_MAP_TYPE) {
@@ -1563,7 +1625,9 @@
             mRootCode = mEventCode;
             break;
         } else {
-            XML_NOISY(printf("Skipping unknown chunk!\n"));
+            if (kDebugXMLNoisy) {
+                printf("Skipping unknown chunk!\n");
+            }
         }
         lastChunk = chunk;
         chunk = (const ResChunk_header*)
@@ -2147,9 +2211,11 @@
                 myDelta += requested->screenHeightDp - screenHeightDp;
                 otherDelta += requested->screenHeightDp - o.screenHeightDp;
             }
-            //ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
-            //    screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
-            //    requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
+            if (kDebugTableSuperNoisy) {
+                ALOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
+                        screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
+                        requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
+            }
             if (myDelta != otherDelta) {
                 return myDelta < otherDelta;
             }
@@ -2404,11 +2470,17 @@
     }
     if (screenSizeDp != 0) {
         if (screenWidthDp != 0 && screenWidthDp > settings.screenWidthDp) {
-            //ALOGI("Filtering out width %d in requested %d", screenWidthDp, settings.screenWidthDp);
+            if (kDebugTableSuperNoisy) {
+                ALOGI("Filtering out width %d in requested %d", screenWidthDp,
+                        settings.screenWidthDp);
+            }
             return false;
         }
         if (screenHeightDp != 0 && screenHeightDp > settings.screenHeightDp) {
-            //ALOGI("Filtering out height %d in requested %d", screenHeightDp, settings.screenHeightDp);
+            if (kDebugTableSuperNoisy) {
+                ALOGI("Filtering out height %d in requested %d", screenHeightDp,
+                        settings.screenHeightDp);
+            }
             return false;
         }
     }
@@ -2428,9 +2500,13 @@
             // For compatibility, we count a request for KEYSHIDDEN_NO as also
             // matching the more recent KEYSHIDDEN_SOFT.  Basically
             // KEYSHIDDEN_NO means there is some kind of keyboard available.
-            //ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
+            if (kDebugTableSuperNoisy) {
+                ALOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
+            }
             if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
-                //ALOGI("No match!");
+                if (kDebugTableSuperNoisy) {
+                    ALOGI("No match!");
+                }
                 return false;
             }
         }
@@ -2935,17 +3011,25 @@
 
     void clearBagCache() {
         if (bags) {
-            TABLE_NOISY(printf("bags=%p\n", bags));
+            if (kDebugTableNoisy) {
+                printf("bags=%p\n", bags);
+            }
             for (size_t i = 0; i < bags->size(); i++) {
-                TABLE_NOISY(printf("type=%d\n", i));
+                if (kDebugTableNoisy) {
+                    printf("type=%zu\n", i);
+                }
                 const TypeList& typeList = types[i];
                 if (!typeList.isEmpty()) {
                     bag_set** typeBags = bags->get(i);
-                    TABLE_NOISY(printf("typeBags=%p\n", typeBags));
+                    if (kDebugTableNoisy) {
+                        printf("typeBags=%p\n", typeBags);
+                    }
                     if (typeBags) {
                         const size_t N = typeList[0]->entryCount;
-                        TABLE_NOISY(printf("type->entryCount=%x\n", N));
-                        for (size_t j=0; j<N; j++) {
+                        if (kDebugTableNoisy) {
+                            printf("type->entryCount=%zu\n", N);
+                        }
+                        for (size_t j = 0; j < N; j++) {
                             if (typeBags[j] && typeBags[j] != (bag_set*)0xFFFFFFFF)
                                 free(typeBags[j]);
                         }
@@ -3053,7 +3137,9 @@
     uint32_t bagTypeSpecFlags = 0;
     mTable.lock();
     const ssize_t N = mTable.getBagLocked(resID, &bag, &bagTypeSpecFlags);
-    TABLE_NOISY(ALOGV("Applying style 0x%08x to theme %p, count=%d", resID, this, N));
+    if (kDebugTableNoisy) {
+        ALOGV("Applying style 0x%08x to theme %p, count=%zu", resID, this, N);
+    }
     if (N < 0) {
         mTable.unlock();
         return N;
@@ -3084,7 +3170,6 @@
             curPackageIndex = pidx;
             curPI = mPackages[pidx];
             if (curPI == NULL) {
-                PackageGroup* const grp = mTable.mPackageGroups[pidx];
                 curPI = (package_info*)malloc(sizeof(package_info));
                 memset(curPI, 0, sizeof(*curPI));
                 mPackages[pidx] = curPI;
@@ -3116,9 +3201,11 @@
             continue;
         }
         theme_entry* curEntry = curEntries + e;
-        TABLE_NOISY(ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x",
-                attrRes, bag->map.value.dataType, bag->map.value.data,
-                curEntry->value.dataType));
+        if (kDebugTableNoisy) {
+            ALOGV("Attr 0x%08x: type=0x%x, data=0x%08x; curType=0x%x",
+                    attrRes, bag->map.value.dataType, bag->map.value.data,
+                    curEntry->value.dataType);
+        }
         if (force || curEntry->value.dataType == Res_value::TYPE_NULL) {
             curEntry->stringBlock = bag->stringBlock;
             curEntry->typeSpecFlags |= bagTypeSpecFlags;
@@ -3130,17 +3217,21 @@
 
     mTable.unlock();
 
-    //ALOGI("Applying style 0x%08x (force=%d)  theme %p...\n", resID, force, this);
-    //dumpToLog();
+    if (kDebugTableTheme) {
+        ALOGI("Applying style 0x%08x (force=%d)  theme %p...\n", resID, force, this);
+        dumpToLog();
+    }
 
     return NO_ERROR;
 }
 
 status_t ResTable::Theme::setTo(const Theme& other)
 {
-    //ALOGI("Setting theme %p from theme %p...\n", this, &other);
-    //dumpToLog();
-    //other.dumpToLog();
+    if (kDebugTableTheme) {
+        ALOGI("Setting theme %p from theme %p...\n", this, &other);
+        dumpToLog();
+        other.dumpToLog();
+    }
 
     if (&mTable == &other.mTable) {
         for (size_t i=0; i<Res_MAXPACKAGE; i++) {
@@ -3169,8 +3260,10 @@
         }
     }
 
-    //ALOGI("Final theme:");
-    //dumpToLog();
+    if (kDebugTableTheme) {
+        ALOGI("Final theme:");
+        dumpToLog();
+    }
 
     return NO_ERROR;
 }
@@ -3187,23 +3280,33 @@
         const uint32_t t = Res_GETTYPE(resID);
         const uint32_t e = Res_GETENTRY(resID);
 
-        TABLE_THEME(ALOGI("Looking up attr 0x%08x in theme %p", resID, this));
+        if (kDebugTableTheme) {
+            ALOGI("Looking up attr 0x%08x in theme %p", resID, this);
+        }
 
         if (p >= 0) {
             const package_info* const pi = mPackages[p];
-            TABLE_THEME(ALOGI("Found package: %p", pi));
+            if (kDebugTableTheme) {
+                ALOGI("Found package: %p", pi);
+            }
             if (pi != NULL) {
-                TABLE_THEME(ALOGI("Desired type index is %ld in avail %d", t, Res_MAXTYPE + 1));
+                if (kDebugTableTheme) {
+                    ALOGI("Desired type index is %zd in avail %zu", t, Res_MAXTYPE + 1);
+                }
                 if (t <= Res_MAXTYPE) {
                     const type_info& ti = pi->types[t];
-                    TABLE_THEME(ALOGI("Desired entry index is %ld in avail %d", e, ti.numEntries));
+                    if (kDebugTableTheme) {
+                        ALOGI("Desired entry index is %u in avail %zu", e, ti.numEntries);
+                    }
                     if (e < ti.numEntries) {
                         const theme_entry& te = ti.entries[e];
                         if (outTypeSpecFlags != NULL) {
                             *outTypeSpecFlags |= te.typeSpecFlags;
                         }
-                        TABLE_THEME(ALOGI("Theme value: type=0x%x, data=0x%08x",
-                                te.value.dataType, te.value.data));
+                        if (kDebugTableTheme) {
+                            ALOGI("Theme value: type=0x%x, data=0x%08x",
+                                    te.value.dataType, te.value.data);
+                        }
                         const uint8_t type = te.value.dataType;
                         if (type == Res_value::TYPE_ATTRIBUTE) {
                             if (cnt > 0) {
@@ -3237,8 +3340,10 @@
     if (inOutValue->dataType == Res_value::TYPE_ATTRIBUTE) {
         uint32_t newTypeSpecFlags;
         blockIndex = getAttribute(inOutValue->data, inOutValue, &newTypeSpecFlags);
-        TABLE_THEME(ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=%p\n",
-             (int)blockIndex, (int)inOutValue->dataType, (void*)inOutValue->data));
+        if (kDebugTableTheme) {
+            ALOGI("Resolving attr reference: blockIndex=%d, type=0x%x, data=0x%x\n",
+                    (int)blockIndex, (int)inOutValue->dataType, inOutValue->data);
+        }
         if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newTypeSpecFlags;
         //printf("Retrieved attribute new type=0x%x\n", inOutValue->dataType);
         if (blockIndex < 0) {
@@ -3277,7 +3382,9 @@
 {
     memset(&mParams, 0, sizeof(mParams));
     memset(mPackageMap, 0, sizeof(mPackageMap));
-    //ALOGI("Creating ResTable %p\n", this);
+    if (kDebugTableSuperNoisy) {
+        ALOGI("Creating ResTable %p\n", this);
+    }
 }
 
 ResTable::ResTable(const void* data, size_t size, const int32_t cookie, bool copyData)
@@ -3287,12 +3394,16 @@
     memset(mPackageMap, 0, sizeof(mPackageMap));
     addInternal(data, size, NULL, 0, cookie, copyData);
     LOG_FATAL_IF(mError != NO_ERROR, "Error parsing resource table");
-    //ALOGI("Creating ResTable %p\n", this);
+    if (kDebugTableSuperNoisy) {
+        ALOGI("Creating ResTable %p\n", this);
+    }
 }
 
 ResTable::~ResTable()
 {
-    //ALOGI("Destroying ResTable in %p\n", this);
+    if (kDebugTableSuperNoisy) {
+        ALOGI("Destroying ResTable in %p\n", this);
+    }
     uninit();
 }
 
@@ -3421,9 +3532,10 @@
 
     const bool notDeviceEndian = htods(0xf0) != 0xf0;
 
-    LOAD_TABLE_NOISY(
-        ALOGV("Adding resources to ResTable: data=%p, size=0x%x, cookie=%d, copy=%d "
-             "idmap=%p\n", data, dataSize, cookie, copyData, idmap));
+    if (kDebugLoadTableNoisy) {
+        ALOGV("Adding resources to ResTable: data=%p, size=%zu, cookie=%d, copy=%d "
+                "idmap=%p\n", data, dataSize, cookie, copyData, idmapData);
+    }
 
     if (copyData || notDeviceEndian) {
         header->ownedData = malloc(dataSize);
@@ -3436,9 +3548,13 @@
 
     header->header = (const ResTable_header*)data;
     header->size = dtohl(header->header->header.size);
-    //ALOGI("Got size 0x%x, again size 0x%x, raw size 0x%x\n", header->size,
-    //     dtohl(header->header->header.size), header->header->header.size);
-    LOAD_TABLE_NOISY(ALOGV("Loading ResTable @%p:\n", header->header));
+    if (kDebugLoadTableSuperNoisy) {
+        ALOGI("Got size %zu, again size 0x%x, raw size 0x%x\n", header->size,
+                dtohl(header->header->header.size), header->header->header.size);
+    }
+    if (kDebugLoadTableNoisy) {
+        ALOGV("Loading ResTable @%p:\n", header->header);
+    }
     if (dtohs(header->header->header.headerSize) > header->size
             || header->size > dataSize) {
         ALOGW("Bad resource table: header size 0x%x or total size 0x%x is larger than data size 0x%x\n",
@@ -3466,9 +3582,11 @@
         if (err != NO_ERROR) {
             return (mError=err);
         }
-        TABLE_NOISY(ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
-                     dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
-                     (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))));
+        if (kDebugTableNoisy) {
+            ALOGV("Chunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
+                    dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
+                    (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
+        }
         const size_t csize = dtohl(chunk->size);
         const uint16_t ctype = dtohs(chunk->type);
         if (ctype == RES_STRING_POOL_TYPE) {
@@ -3512,7 +3630,9 @@
         ALOGW("No string values found in resource table!");
     }
 
-    TABLE_NOISY(ALOGV("Returning from add with mError=%d\n", mError));
+    if (kDebugTableNoisy) {
+        ALOGV("Returning from add with mError=%d\n", mError);
+    }
     return mError;
 }
 
@@ -3674,15 +3794,16 @@
         return BAD_VALUE;
     }
 
-    TABLE_NOISY(size_t len;
-          printf("Found value: pkg=%d, type=%d, str=%s, int=%d\n",
-                 entry.package->header->index,
-                 outValue->dataType,
-                 outValue->dataType == Res_value::TYPE_STRING
-                 ? String8(entry.package->header->values.stringAt(
-                     outValue->data, &len)).string()
-                 : "",
-                 outValue->data));
+    if (kDebugTableNoisy) {
+        size_t len;
+        printf("Found value: pkg=%zu, type=%d, str=%s, int=%d\n",
+                entry.package->header->index,
+                outValue->dataType,
+                outValue->dataType == Res_value::TYPE_STRING ?
+                    String8(entry.package->header->values.stringAt(outValue->data, &len)).string() :
+                    "",
+                outValue->data);
+    }
 
     if (outSpecFlags != NULL) {
         *outSpecFlags = entry.specFlags;
@@ -3703,15 +3824,16 @@
     while (blockIndex >= 0 && value->dataType == Res_value::TYPE_REFERENCE
             && value->data != 0 && count < 20) {
         if (outLastRef) *outLastRef = value->data;
-        uint32_t lastRef = value->data;
         uint32_t newFlags = 0;
         const ssize_t newIndex = getResource(value->data, value, true, 0, &newFlags,
                 outConfig);
         if (newIndex == BAD_INDEX) {
             return BAD_INDEX;
         }
-        TABLE_THEME(ALOGI("Resolving reference %p: newIndex=%d, type=0x%x, data=%p\n",
-             (void*)lastRef, (int)newIndex, (int)value->dataType, (void*)value->data));
+        if (kDebugTableTheme) {
+            ALOGI("Resolving reference 0x%x: newIndex=%d, type=0x%x, data=0x%x\n",
+                    value->data, (int)newIndex, (int)value->dataType, value->data);
+        }
         //printf("Getting reference 0x%08x: newIndex=%d\n", value->data, newIndex);
         if (inoutTypeSpecFlags != NULL) *inoutTypeSpecFlags |= newFlags;
         if (newIndex < 0) {
@@ -3818,7 +3940,9 @@
                         *outTypeSpecFlags = set->typeSpecFlags;
                     }
                     *outBag = (bag_entry*)(set+1);
-                    //ALOGI("Found existing bag for: %p\n", (void*)resID);
+                    if (kDebugTableSuperNoisy) {
+                        ALOGI("Found existing bag for: 0x%x\n", resID);
+                    }
                     return set->numAttrs;
                 }
                 ALOGW("Attempt to retrieve bag 0x%08x which is invalid or in a cycle.",
@@ -3844,7 +3968,9 @@
     // Mark that we are currently working on this one.
     typeSet[e] = (bag_set*)0xFFFFFFFF;
 
-    TABLE_NOISY(ALOGI("Building bag: %p\n", (void*)resID));
+    if (kDebugTableNoisy) {
+        ALOGI("Building bag: %x\n", resID);
+    }
 
     // Now collect all bag attributes
     Entry entry;
@@ -3861,13 +3987,13 @@
 
     size_t N = count;
 
-    TABLE_NOISY(ALOGI("Found map: size=%p parent=%p count=%d\n",
-                     entrySize, parent, count));
+    if (kDebugTableNoisy) {
+        ALOGI("Found map: size=%x parent=%x count=%d\n", entrySize, parent, count);
 
     // If this map inherits from another, we need to start
     // with its parent's values.  Otherwise start out empty.
-    TABLE_NOISY(printf("Creating new bag, entrySize=0x%08x, parent=0x%08x\n",
-                       entrySize, parent));
+        ALOGI("Creating new bag, entrySize=0x%08x, parent=0x%08x\n", entrySize, parent);
+    }
 
     // This is what we are building.
     bag_set* set = NULL;
@@ -3896,9 +4022,13 @@
         if (NP > 0) {
             memcpy(set+1, parentBag, NP*sizeof(bag_entry));
             set->numAttrs = NP;
-            TABLE_NOISY(ALOGI("Initialized new bag with %d inherited attributes.\n", NP));
+            if (kDebugTableNoisy) {
+                ALOGI("Initialized new bag with %zd inherited attributes.\n", NP);
+            }
         } else {
-            TABLE_NOISY(ALOGI("Initialized new bag with no inherited attributes.\n"));
+            if (kDebugTableNoisy) {
+                ALOGI("Initialized new bag with no inherited attributes.\n");
+            }
             set->numAttrs = 0;
         }
         set->availAttrs = NT;
@@ -3922,10 +4052,13 @@
     bag_entry* entries = (bag_entry*)(set+1);
     size_t curEntry = 0;
     uint32_t pos = 0;
-    TABLE_NOISY(ALOGI("Starting with set %p, entries=%p, avail=%d\n",
-                 set, entries, set->availAttrs));
+    if (kDebugTableNoisy) {
+        ALOGI("Starting with set %p, entries=%p, avail=%zu\n", set, entries, set->availAttrs);
+    }
     while (pos < count) {
-        TABLE_NOISY(printf("Now at %p\n", (void*)curOff));
+        if (kDebugTableNoisy) {
+            ALOGI("Now at %p\n", (void*)curOff);
+        }
 
         if (curOff > (dtohl(entry.type->header.size)-sizeof(ResTable_map))) {
             ALOGW("ResTable_map at %d is beyond type chunk data %d",
@@ -3950,8 +4083,10 @@
         uint32_t oldName = 0;
         while ((isInside=(curEntry < set->numAttrs))
                 && (oldName=entries[curEntry].map.name.ident) < newName) {
-            TABLE_NOISY(printf("#%d: Keeping existing attribute: 0x%08x\n",
-                         curEntry, entries[curEntry].map.name.ident));
+            if (kDebugTableNoisy) {
+                ALOGI("#%zu: Keeping existing attribute: 0x%08x\n",
+                        curEntry, entries[curEntry].map.name.ident);
+            }
             curEntry++;
         }
 
@@ -3968,8 +4103,10 @@
                 }
                 set->availAttrs = newAvail;
                 entries = (bag_entry*)(set+1);
-                TABLE_NOISY(printf("Reallocated set %p, entries=%p, avail=%d\n",
-                             set, entries, set->availAttrs));
+                if (kDebugTableNoisy) {
+                    ALOGI("Reallocated set %p, entries=%p, avail=%zu\n",
+                            set, entries, set->availAttrs);
+                }
             }
             if (isInside) {
                 // Going in the middle, need to make space.
@@ -3977,11 +4114,13 @@
                         sizeof(bag_entry)*(set->numAttrs-curEntry));
                 set->numAttrs++;
             }
-            TABLE_NOISY(printf("#%d: Inserting new attribute: 0x%08x\n",
-                         curEntry, newName));
+            if (kDebugTableNoisy) {
+                ALOGI("#%zu: Inserting new attribute: 0x%08x\n", curEntry, newName);
+            }
         } else {
-            TABLE_NOISY(printf("#%d: Replacing existing attribute: 0x%08x\n",
-                         curEntry, oldName));
+            if (kDebugTableNoisy) {
+                ALOGI("#%zu: Replacing existing attribute: 0x%08x\n", curEntry, oldName);
+            }
         }
 
         bag_entry* cur = entries+curEntry;
@@ -3995,9 +4134,11 @@
             return UNKNOWN_ERROR;
         }
 
-        TABLE_NOISY(printf("Setting entry #%d %p: block=%d, name=0x%08x, type=%d, data=0x%08x\n",
-                     curEntry, cur, cur->stringBlock, cur->map.name.ident,
-                     cur->map.value.dataType, cur->map.value.data));
+        if (kDebugTableNoisy) {
+            ALOGI("Setting entry #%zu %p: block=%zd, name=0x%08d, type=%d, data=0x%08x\n",
+                    curEntry, cur, cur->stringBlock, cur->map.name.ident,
+                    cur->map.value.dataType, cur->map.value.data);
+        }
 
         // On to the next!
         curEntry++;
@@ -4017,7 +4158,9 @@
             *outTypeSpecFlags = set->typeSpecFlags;
         }
         *outBag = (bag_entry*)(set+1);
-        TABLE_NOISY(ALOGI("Returning %d attrs\n", set->numAttrs));
+        if (kDebugTableNoisy) {
+            ALOGI("Returning %zu attrs\n", set->numAttrs);
+        }
         return set->numAttrs;
     }
     return BAD_INDEX;
@@ -4026,10 +4169,14 @@
 void ResTable::setParameters(const ResTable_config* params)
 {
     mLock.lock();
-    TABLE_GETENTRY(ALOGI("Setting parameters: %s\n", params->toString().string()));
+    if (kDebugTableGetEntry) {
+        ALOGI("Setting parameters: %s\n", params->toString().string());
+    }
     mParams = *params;
     for (size_t i=0; i<mPackageGroups.size(); i++) {
-        TABLE_NOISY(ALOGI("CLEARING BAGS FOR GROUP %d!", i));
+        if (kDebugTableNoisy) {
+            ALOGI("CLEARING BAGS FOR GROUP %zu!", i);
+        }
         mPackageGroups[i]->clearBagCache();
     }
     mLock.unlock();
@@ -4067,7 +4214,9 @@
                                      size_t packageLen,
                                      uint32_t* outTypeSpecFlags) const
 {
-    TABLE_SUPER_NOISY(printf("Identifier for name: error=%d\n", mError));
+    if (kDebugTableSuperNoisy) {
+        printf("Identifier for name: error=%d\n", mError);
+    }
 
     // Check for internal resource identifier as the very first thing, so
     // that we will always find them even when there are no resources.
@@ -4160,10 +4309,12 @@
     }
     nameLen = nameEnd-name;
 
-    TABLE_NOISY(printf("Looking for identifier: type=%s, name=%s, package=%s\n",
-                 String8(type, typeLen).string(),
-                 String8(name, nameLen).string(),
-                 String8(package, packageLen).string()));
+    if (kDebugTableNoisy) {
+        printf("Looking for identifier: type=%s, name=%s, package=%s\n",
+                String8(type, typeLen).string(),
+                String8(name, nameLen).string(),
+                String8(package, packageLen).string());
+    }
 
     const size_t NG = mPackageGroups.size();
     for (size_t ig=0; ig<NG; ig++) {
@@ -4171,7 +4322,9 @@
 
         if (strzcmp16(package, packageLen,
                       group->name.string(), group->name.size())) {
-            TABLE_NOISY(printf("Skipping package group: %s\n", String8(group->name).string()));
+            if (kDebugTableNoisy) {
+                printf("Skipping package group: %s\n", String8(group->name).string());
+            }
             continue;
         }
 
@@ -4186,8 +4339,10 @@
 
             const TypeList& typeList = group->types[ti];
             if (typeList.isEmpty()) {
-                TABLE_NOISY(printf("Expected type structure not found in package %s for index %d\n",
-                                   String8(group->name).string(), ti));
+                if (kDebugTableNoisy) {
+                    ALOGI("Expected type structure not found in package %s for index %zd\n",
+                            String8(group->name).string(), ti);
+                }
                 continue;
             }
 
@@ -4686,9 +4841,11 @@
                     rid = Res_MAKEID(
                         accessor->getRemappedPackage(Res_GETPACKAGE(rid)),
                         Res_GETTYPE(rid), Res_GETENTRY(rid));
-                    TABLE_NOISY(printf("Incl %s:%s/%s: 0x%08x\n",
-                           String8(package).string(), String8(type).string(),
-                           String8(name).string(), rid));
+                    if (kDebugTableNoisy) {
+                        ALOGI("Incl %s:%s/%s: 0x%08x\n",
+                                String8(package).string(), String8(type).string(),
+                                String8(name).string(), rid);
+                    }
                 }
 
                 uint32_t packageId = Res_GETPACKAGE(rid) + 1;
@@ -4703,9 +4860,11 @@
                 uint32_t rid = accessor->getCustomResourceWithCreation(package, type, name,
                                                                        createIfNotFound);
                 if (rid != 0) {
-                    TABLE_NOISY(printf("Pckg %s:%s/%s: 0x%08x\n",
-                           String8(package).string(), String8(type).string(),
-                           String8(name).string(), rid));
+                    if (kDebugTableNoisy) {
+                        ALOGI("Pckg %s:%s/%s: 0x%08x\n",
+                                String8(package).string(), String8(type).string(),
+                                String8(name).string(), rid);
+                    }
                     uint32_t packageId = Res_GETPACKAGE(rid) + 1;
                     if (packageId == 0x00) {
                         outValue->data = rid;
@@ -5508,8 +5667,6 @@
             }
 
             // Check if there is the desired entry in this type.
-            const uint8_t* const end = reinterpret_cast<const uint8_t*>(thisType)
-                    + dtohl(thisType->header.size);
             const uint32_t* const eindex = reinterpret_cast<const uint32_t*>(
                     reinterpret_cast<const uint8_t*>(thisType) + dtohs(thisType->header.headerSize));
 
@@ -5698,9 +5855,11 @@
     const uint8_t* endPos = ((const uint8_t*)pkg) + dtohs(pkg->header.size);
     while (((const uint8_t*)chunk) <= (endPos-sizeof(ResChunk_header)) &&
            ((const uint8_t*)chunk) <= (endPos-dtohl(chunk->size))) {
-        TABLE_NOISY(ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
-                         dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
-                         (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header))));
+        if (kDebugTableNoisy) {
+            ALOGV("PackageChunk: type=0x%x, headerSize=0x%x, size=0x%x, pos=%p\n",
+                    dtohs(chunk->type), dtohs(chunk->headerSize), dtohl(chunk->size),
+                    (void*)(((const uint8_t*)chunk) - ((const uint8_t*)header->header)));
+        }
         const size_t csize = dtohl(chunk->size);
         const uint16_t ctype = dtohs(chunk->type);
         if (ctype == RES_TABLE_TYPE_SPEC_TYPE) {
@@ -5714,11 +5873,13 @@
             const size_t typeSpecSize = dtohl(typeSpec->header.size);
             const size_t newEntryCount = dtohl(typeSpec->entryCount);
 
-            LOAD_TABLE_NOISY(printf("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
-                                    (void*)(base-(const uint8_t*)chunk),
-                                    dtohs(typeSpec->header.type),
-                                    dtohs(typeSpec->header.headerSize),
-                                    (void*)typeSpecSize));
+            if (kDebugLoadTableNoisy) {
+                ALOGI("TypeSpec off %p: type=0x%x, headerSize=0x%x, size=%p\n",
+                        (void*)(base-(const uint8_t*)chunk),
+                        dtohs(typeSpec->header.type),
+                        dtohs(typeSpec->header.headerSize),
+                        (void*)typeSpecSize);
+            }
             // look for block overrun or int overflow when multiplying by 4
             if ((dtohl(typeSpec->entryCount) > (INT32_MAX/sizeof(uint32_t))
                     || dtohs(typeSpec->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
@@ -5776,13 +5937,14 @@
             const uint32_t typeSize = dtohl(type->header.size);
             const size_t newEntryCount = dtohl(type->entryCount);
 
-            LOAD_TABLE_NOISY(printf("Type off %p: type=0x%x, headerSize=0x%x, size=%p\n",
-                                    (void*)(base-(const uint8_t*)chunk),
-                                    dtohs(type->header.type),
-                                    dtohs(type->header.headerSize),
-                                    (void*)typeSize));
-            if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount)
-                    > typeSize) {
+            if (kDebugLoadTableNoisy) {
+                printf("Type off %p: type=0x%x, headerSize=0x%x, size=%u\n",
+                        (void*)(base-(const uint8_t*)chunk),
+                        dtohs(type->header.type),
+                        dtohs(type->header.headerSize),
+                        typeSize);
+            }
+            if (dtohs(type->header.headerSize)+(sizeof(uint32_t)*newEntryCount) > typeSize) {
                 ALOGW("ResTable_type entry index to %p extends beyond chunk end 0x%x.",
                         (void*)(dtohs(type->header.headerSize) + (sizeof(uint32_t)*newEntryCount)),
                         typeSize);
@@ -5828,11 +5990,12 @@
 
                 t->configs.add(type);
 
-                TABLE_GETENTRY(
+                if (kDebugTableGetEntry) {
                     ResTable_config thisConfig;
                     thisConfig.copyFromDtoH(type->config);
-                    ALOGI("Adding config to type %d: %s\n",
-                          type->id, thisConfig.toString().string()));
+                    ALOGI("Adding config to type %d: %s\n", type->id,
+                            thisConfig.toString().string());
+                }
             } else {
                 ALOGV("Skipping empty ResTable_type for type %d", type->id);
             }
@@ -5893,8 +6056,10 @@
         uint32_t packageId = dtohl(entry->packageId);
         char16_t tmpName[sizeof(entry->packageName) / sizeof(char16_t)];
         strcpy16_dtoh(tmpName, entry->packageName, sizeof(entry->packageName) / sizeof(char16_t));
-        LIB_NOISY(ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
-                dtohl(entry->packageId)));
+        if (kDebugLibNoisy) {
+            ALOGV("Found lib entry %s with id %d\n", String8(tmpName).string(),
+                    dtohl(entry->packageId));
+        }
         if (packageId >= 256) {
             ALOGE("Bad package id 0x%08x", packageId);
             return UNKNOWN_ERROR;
@@ -6074,7 +6239,7 @@
 
             if (Res_GETTYPE(overlayResID) + 1 != static_cast<size_t>(typeMap.overlayTypeId)) {
                 ALOGE("idmap: can't mix type ids in entry map. Resource 0x%08x maps to 0x%08x"
-                        " but entries should map to resources of type %02x",
+                        " but entries should map to resources of type %02zx",
                         resID, overlayResID, typeMap.overlayTypeId);
                 return BAD_TYPE;
             }
@@ -6401,9 +6566,6 @@
                     continue;
                 }
                 for (size_t entryIndex=0; entryIndex<entryCount; entryIndex++) {
-
-                    const uint8_t* const end = ((const uint8_t*)type)
-                        + dtohl(type->header.size);
                     const uint32_t* const eindex = (const uint32_t*)
                         (((const uint8_t*)type) + dtohs(type->header.headerSize));
 
diff --git a/libs/androidfw/StreamingZipInflater.cpp b/libs/androidfw/StreamingZipInflater.cpp
index 1dfec23..b39b5f0 100644
--- a/libs/androidfw/StreamingZipInflater.cpp
+++ b/libs/androidfw/StreamingZipInflater.cpp
@@ -41,6 +41,8 @@
     _rc; })
 #endif
 
+static const bool kIsDebug = false;
+
 static inline size_t min_of(size_t a, size_t b) { return (a < b) ? a : b; }
 
 using namespace android;
@@ -209,7 +211,9 @@
         size_t toRead = min_of(mInBufSize, mInTotalSize - mInNextChunkOffset);
         if (toRead > 0) {
             ssize_t didRead = TEMP_FAILURE_RETRY(::read(mFd, mInBuf, toRead));
-            //ALOGV("Reading input chunk, size %08x didread %08x", toRead, didRead);
+            if (kIsDebug) {
+                ALOGV("Reading input chunk, size %08zx didread %08zx", toRead, didRead);
+            }
             if (didRead < 0) {
                 ALOGE("Error reading asset data: %s", strerror(errno));
                 return didRead;
diff --git a/libs/androidfw/ZipFileRO.cpp b/libs/androidfw/ZipFileRO.cpp
index 5f6e831..ef0d072 100644
--- a/libs/androidfw/ZipFileRO.cpp
+++ b/libs/androidfw/ZipFileRO.cpp
@@ -34,14 +34,6 @@
 #include <assert.h>
 #include <unistd.h>
 
-/*
- * We must open binary files using open(path, ... | O_BINARY) under Windows.
- * Otherwise strange read errors will happen.
- */
-#ifndef O_BINARY
-#  define O_BINARY  0
-#endif
-
 using namespace android;
 
 class _ZipEntryRO {
diff --git a/libs/androidfw/tests/Android.mk b/libs/androidfw/tests/Android.mk
index 597250a..8aaa887 100644
--- a/libs/androidfw/tests/Android.mk
+++ b/libs/androidfw/tests/Android.mk
@@ -37,12 +37,16 @@
 
 LOCAL_MODULE := libandroidfw_tests
 
+LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
+# gtest is broken.
+LOCAL_CFLAGS += -Wno-unnamed-type-template-args
+
 LOCAL_SRC_FILES := $(testFiles)
 LOCAL_STATIC_LIBRARIES := \
     libandroidfw \
     libutils \
     libcutils \
-	liblog
+    liblog
 
 include $(BUILD_HOST_NATIVE_TEST)
 
@@ -54,6 +58,10 @@
 
 LOCAL_MODULE := libandroidfw_tests
 
+LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
+# gtest is broken.
+LOCAL_CFLAGS += -Wno-unnamed-type-template-args
+
 LOCAL_SRC_FILES := $(testFiles) \
     BackupData_test.cpp \
     ObbFile_test.cpp