Skip to content

Commit 5069dd6

Browse files
author
Adam Lesinski
committed
Fix issue with using locally defined attrs in a shared lib
The attribute name resource IDs were never fixed up with the runtime package ID so we weren't finding attributes whenever the runtime package ID was different than the build time one, which happened to be when a shared lib referenced itself (0x00 vs 0x02). Bug:17666947 Change-Id: Icf3e874bcea0e27eebe42d60fbed626a34bf9266
1 parent 75c33d9 commit 5069dd6

5 files changed

Lines changed: 56 additions & 10 deletions

File tree

core/jni/android_util_AssetManager.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,21 @@ static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject cla
13241324
config.density = 0;
13251325

13261326
// Skip through XML attributes until the end or the next possible match.
1327-
while (ix < NX && curIdent > curXmlAttr) {
1327+
// We make two assumptions about the order of attributes:
1328+
// 1) Among attributes with the same package ID, the attributes are
1329+
// sorted by increasing resource ID.
1330+
// 2) Groups of attributes with the same package ID are in the same
1331+
// order.
1332+
// 3) The same sorting is applied to the input attributes as is
1333+
// to the attributes in the XML.
1334+
//
1335+
// ex: 02010000, 02010001, 010100f4, 010100f5
1336+
//
1337+
// The total order of attributes (including package ID) can not be linear
1338+
// as shared libraries get assigned dynamic package IDs at runtime, which
1339+
// may break the sort order established at build time.
1340+
while (ix < NX && (Res_GETPACKAGE(curIdent) != Res_GETPACKAGE(curXmlAttr) ||
1341+
curIdent > curXmlAttr)) {
13281342
ix++;
13291343
curXmlAttr = xmlParser->getAttributeNameResID(ix);
13301344
}
@@ -1339,7 +1353,9 @@ static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject cla
13391353
}
13401354

13411355
// Skip through the style values until the end or the next possible match.
1342-
while (styleEnt < endStyleEnt && curIdent > styleEnt->map.name.ident) {
1356+
while (styleEnt < endStyleEnt &&
1357+
(Res_GETPACKAGE(curIdent) != Res_GETPACKAGE(styleEnt->map.name.ident) ||
1358+
curIdent > styleEnt->map.name.ident)) {
13431359
styleEnt++;
13441360
}
13451361
// Retrieve the current style attribute if it matches, and step to next.
@@ -1355,7 +1371,9 @@ static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject cla
13551371
}
13561372

13571373
// Skip through the default style values until the end or the next possible match.
1358-
while (defStyleEnt < endDefStyleEnt && curIdent > defStyleEnt->map.name.ident) {
1374+
while (defStyleEnt < endDefStyleEnt &&
1375+
(Res_GETPACKAGE(curIdent) != Res_GETPACKAGE(defStyleEnt->map.name.ident) ||
1376+
curIdent > defStyleEnt->map.name.ident)) {
13591377
defStyleEnt++;
13601378
}
13611379
// Retrieve the current default style attribute if it matches, and step to next.
@@ -1517,7 +1535,8 @@ static jboolean android_content_AssetManager_retrieveAttributes(JNIEnv* env, job
15171535
config.density = 0;
15181536

15191537
// Skip through XML attributes until the end or the next possible match.
1520-
while (ix < NX && curIdent > curXmlAttr) {
1538+
while (ix < NX && (Res_GETPACKAGE(curIdent) != Res_GETPACKAGE(curXmlAttr) ||
1539+
curIdent > curXmlAttr)) {
15211540
ix++;
15221541
curXmlAttr = xmlParser->getAttributeNameResID(ix);
15231542
}

libs/androidfw/ResourceTypes.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,11 @@ uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
11851185
{
11861186
int32_t id = getAttributeNameID(idx);
11871187
if (id >= 0 && (size_t)id < mTree.mNumResIds) {
1188-
return dtohl(mTree.mResIds[id]);
1188+
uint32_t resId = dtohl(mTree.mResIds[id]);
1189+
if (mTree.mDynamicRefTable == NULL ||
1190+
mTree.mDynamicRefTable->lookupResourceId(&resId) == NO_ERROR) {
1191+
return resId;
1192+
}
11891193
}
11901194
return 0;
11911195
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical">
7+
8+
<TextView
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:text="@string/sample_layout"/>
12+
13+
<com.google.android.test.shared_library.AddressView xmlns:address="http://schemas.android.com/apk/res-auto"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:layout_weight="1"
17+
android:background="#03a9f4"
18+
address:name="Librarian L"
19+
address:streetNumber="21"
20+
address:streetName="Android Lane"
21+
address:city="AndroidVille"
22+
address:state="OS"
23+
address:zip="12345"
24+
address:country="Mobile"/>
25+
26+
</LinearLayout>

tests/SharedLibrary/lib/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
</string-array>
2929

3030
<string name="racoon">Racoon</string>
31+
<string name="sample_layout">This is an example of a layout this library provides.</string>
3132
</resources>

tests/SharedLibrary/lib/src/com/google/android/test/shared_library/ActivityMain.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818

1919
import android.app.Activity;
2020
import android.os.Bundle;
21-
import android.widget.TextView;
2221

2322
public class ActivityMain extends Activity {
2423
@Override
2524
protected void onCreate(Bundle savedInstanceState) {
2625
super.onCreate(savedInstanceState);
27-
28-
TextView content = new TextView(this);
29-
content.setText("Dummy main entry for this apk; not really needed...");
30-
setContentView(content);
26+
setContentView(R.layout.main);
3127
}
3228
}

0 commit comments

Comments
 (0)