Shared library resource support

Shared libraries can now export resources for applications
to use.

Exporting resources works the same way the framework exports
resources, by defining the public symbols in res/values/public.xml.

Building a shared library requires aapt to be invoked with the
--shared-lib option. Shared libraries will be assigned a package
ID of 0x00 at build-time. At runtime, all loaded shared libraries
will be assigned a new package ID.

Currently, shared libraries should not import other shared libraries,
as those dependencies will not be loaded at runtime.

At runtime, reflection is used to update the package ID of resource
symbols in the shared library's R class file. The package name of
the R class file is assumed to be the same as the shared library's
package name declared in its manifest. This will be customizable in
a future commit.

See /tests/SharedLibrary/ for examples of a shared library and its
client.

Bug:12724178
Change-Id: I60c0cb8ab87849f8f8a1a13431562fe8603020a7
diff --git a/tests/SharedLibrary/lib/Android.mk b/tests/SharedLibrary/lib/Android.mk
index c19e23a..b2fc369 100644
--- a/tests/SharedLibrary/lib/Android.mk
+++ b/tests/SharedLibrary/lib/Android.mk
@@ -3,8 +3,13 @@
 
 LOCAL_SRC_FILES := $(call all-subdir-java-files)
 
+LOCAL_AAPT_FLAGS := --shared-lib
 LOCAL_PACKAGE_NAME := SharedLibrary
 
-LOCAL_MODULE_TAGS := tests
+LOCAL_EXPORT_PACKAGE_RESOURCES := true
+LOCAL_PRIVILEGED_MODULE := true
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_PROGUARD_FLAG_FILES := proguard.proguard
 
 include $(BUILD_PACKAGE)
diff --git a/tests/SharedLibrary/lib/proguard.proguard b/tests/SharedLibrary/lib/proguard.proguard
new file mode 100644
index 0000000..e5dfbe1
--- /dev/null
+++ b/tests/SharedLibrary/lib/proguard.proguard
@@ -0,0 +1,7 @@
+-keepparameternames
+-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
+                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
+
+-keep public class * {
+    public protected *;
+}
diff --git a/tests/SharedLibrary/lib/res/layout/address.xml b/tests/SharedLibrary/lib/res/layout/address.xml
new file mode 100644
index 0000000..835f43e
--- /dev/null
+++ b/tests/SharedLibrary/lib/res/layout/address.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+    <TextView android:id="@+id/name"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    <TextView android:id="@+id/street"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    <TextView android:id="@+id/cityStateZip"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    <TextView android:id="@+id/country"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+</merge>
diff --git a/tests/SharedLibrary/lib/res/values/attrs.xml b/tests/SharedLibrary/lib/res/values/attrs.xml
new file mode 100644
index 0000000..8cefe92
--- /dev/null
+++ b/tests/SharedLibrary/lib/res/values/attrs.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<resources>
+    <declare-styleable name="AddressView">
+        <attr name="name" format="string" />
+        <attr name="streetNumber" format="integer" />
+        <attr name="streetName" format="string" />
+        <attr name="city" format="string" />
+        <attr name="state" format="string" />
+        <attr name="zip" format="string" />
+        <attr name="country" format="string" />
+    </declare-styleable>
+</resources>
diff --git a/tests/SharedLibrary/lib/res/values/public.xml b/tests/SharedLibrary/lib/res/values/public.xml
new file mode 100644
index 0000000..37b1ec9
--- /dev/null
+++ b/tests/SharedLibrary/lib/res/values/public.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<resources>
+    <public type="string" name="shared_string" id="0x00020003" />
+    <public type="style" name="CodeFont" id="0x00040000" />
+    <public type="style" name="Theme" id="0x00040001" />
+
+    <public type="attr" name="name"         id="0x00010000" />
+    <public type="attr" name="streetNumber" id="0x00010001" />
+    <public type="attr" name="streetName"   id="0x00010002" />
+    <public type="attr" name="city"         id="0x00010003" />
+    <public type="attr" name="state"        id="0x00010004" />
+    <public type="attr" name="zip"          id="0x00010005" />
+    <public type="attr" name="country"      id="0x00010006" />
+
+    <public type="array" name="animals"     id="0x02050000" />
+</resources>
diff --git a/tests/SharedLibrary/lib/res/values/strings.xml b/tests/SharedLibrary/lib/res/values/strings.xml
index bbfb0b4..6827f93 100644
--- a/tests/SharedLibrary/lib/res/values/strings.xml
+++ b/tests/SharedLibrary/lib/res/values/strings.xml
@@ -19,4 +19,13 @@
     <string name="upgrade_body"><xliff:g id="app">%1$s</xliff:g> requires a newer version
             of <xliff:g id="lib">%2$s</xliff:g> to run.</string>
     <string name="upgrade_button">Upgrade</string>
+    <string name="shared_string">Shared string!</string>
+
+    <string-array name="animals">
+        <item>@string/racoon</item>
+        <item>Rhino</item>
+        <item>Elephant</item>
+    </string-array>
+
+    <string name="racoon">Racoon</string>
 </resources>
diff --git a/tests/SharedLibrary/lib/res/values/themes.xml b/tests/SharedLibrary/lib/res/values/themes.xml
new file mode 100644
index 0000000..f1081ac
--- /dev/null
+++ b/tests/SharedLibrary/lib/res/values/themes.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<resources>
+    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
+        <item name="android:textColor">#00FF00</item>
+        <item name="android:typeface">monospace</item>
+    </style>
+
+    <style name="Theme" parent="android:Theme.Holo.Light">
+        <item name="android:actionBarStyle">@style/ActionBar</item>
+    </style>
+
+    <style name="ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
+        <item name="android:background">@color/orange</item>
+    </style>
+
+    <color name="orange">#f0ad4e</color>
+</resources>
diff --git a/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/AddressView.java b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/AddressView.java
new file mode 100644
index 0000000..dcaf68c
--- /dev/null
+++ b/tests/SharedLibrary/lib/src/com/google/android/test/shared_library/AddressView.java
@@ -0,0 +1,44 @@
+package com.google.android.test.shared_library;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+public class AddressView extends LinearLayout {
+    private TextView mNameView;
+    private TextView mStreetView;
+    private TextView mCityStateZipView;
+    private TextView mCountryView;
+
+    public AddressView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setOrientation(VERTICAL);
+
+        View view = LayoutInflater.from(context).inflate(R.layout.address, this);
+        mNameView = (TextView) view.findViewById(R.id.name);
+        mStreetView = (TextView) view.findViewById(R.id.street);
+        mCityStateZipView = (TextView) view.findViewById(R.id.cityStateZip);
+        mCountryView = (TextView) view.findViewById(R.id.country);
+
+        TypedArray a = context.getTheme().obtainStyledAttributes(
+                attrs,
+                R.styleable.AddressView,
+                0, 0);
+        try {
+            mNameView.setText(a.getString(R.styleable.AddressView_name));
+            int streetNumber = a.getInteger(R.styleable.AddressView_streetNumber, -1);
+            mStreetView.setText((streetNumber <= 0 ? "" : Integer.toString(streetNumber)) +
+                    " " + a.getString(R.styleable.AddressView_streetName));
+            mCityStateZipView.setText(a.getString(R.styleable.AddressView_city) + ", " +
+                    a.getString(R.styleable.AddressView_state) + " " +
+                    a.getString(R.styleable.AddressView_zip));
+            mCountryView.setText(a.getString(R.styleable.AddressView_country));
+        } finally {
+            a.recycle();
+        }
+    }
+}