]> git.saurik.com Git - android/aapt.git/blame - Command.cpp
am fa888784: am 89fef418: Merge "Avoid pre-processing images when they won\'t be...
[android/aapt.git] / Command.cpp
CommitLineData
a534180c
TAOSP
1//
2// Copyright 2006 The Android Open Source Project
3//
4// Android Asset Packaging Tool main entry point.
5//
6#include "Main.h"
7#include "Bundle.h"
8#include "ResourceTable.h"
9#include "XMLNode.h"
10
79e5d372
MA
11#include <utils/Log.h>
12#include <utils/threads.h>
13#include <utils/List.h>
14#include <utils/Errors.h>
a534180c
TAOSP
15
16#include <fcntl.h>
17#include <errno.h>
18
19using namespace android;
20
21/*
22 * Show version info. All the cool kids do it.
23 */
24int doVersion(Bundle* bundle)
25{
26 if (bundle->getFileSpecCount() != 0)
27 printf("(ignoring extra arguments)\n");
28 printf("Android Asset Packaging Tool, v0.2\n");
29
30 return 0;
31}
32
33
34/*
35 * Open the file read only. The call fails if the file doesn't exist.
36 *
37 * Returns NULL on failure.
38 */
39ZipFile* openReadOnly(const char* fileName)
40{
41 ZipFile* zip;
42 status_t result;
43
44 zip = new ZipFile;
45 result = zip->open(fileName, ZipFile::kOpenReadOnly);
46 if (result != NO_ERROR) {
47 if (result == NAME_NOT_FOUND)
48 fprintf(stderr, "ERROR: '%s' not found\n", fileName);
49 else if (result == PERMISSION_DENIED)
50 fprintf(stderr, "ERROR: '%s' access denied\n", fileName);
51 else
52 fprintf(stderr, "ERROR: failed opening '%s' as Zip file\n",
53 fileName);
54 delete zip;
55 return NULL;
56 }
57
58 return zip;
59}
60
61/*
62 * Open the file read-write. The file will be created if it doesn't
63 * already exist and "okayToCreate" is set.
64 *
65 * Returns NULL on failure.
66 */
67ZipFile* openReadWrite(const char* fileName, bool okayToCreate)
68{
69 ZipFile* zip = NULL;
70 status_t result;
71 int flags;
72
73 flags = ZipFile::kOpenReadWrite;
74 if (okayToCreate)
75 flags |= ZipFile::kOpenCreate;
76
77 zip = new ZipFile;
78 result = zip->open(fileName, flags);
79 if (result != NO_ERROR) {
80 delete zip;
81 zip = NULL;
82 goto bail;
83 }
84
85bail:
86 return zip;
87}
88
89
90/*
91 * Return a short string describing the compression method.
92 */
93const char* compressionName(int method)
94{
95 if (method == ZipEntry::kCompressStored)
96 return "Stored";
97 else if (method == ZipEntry::kCompressDeflated)
98 return "Deflated";
99 else
100 return "Unknown";
101}
102
103/*
104 * Return the percent reduction in size (0% == no compression).
105 */
106int calcPercent(long uncompressedLen, long compressedLen)
107{
108 if (!uncompressedLen)
109 return 0;
110 else
111 return (int) (100.0 - (compressedLen * 100.0) / uncompressedLen + 0.5);
112}
113
114/*
115 * Handle the "list" command, which can be a simple file dump or
116 * a verbose listing.
117 *
118 * The verbose listing closely matches the output of the Info-ZIP "unzip"
119 * command.
120 */
121int doList(Bundle* bundle)
122{
123 int result = 1;
124 ZipFile* zip = NULL;
125 const ZipEntry* entry;
126 long totalUncLen, totalCompLen;
127 const char* zipFileName;
128
129 if (bundle->getFileSpecCount() != 1) {
130 fprintf(stderr, "ERROR: specify zip file name (only)\n");
131 goto bail;
132 }
133 zipFileName = bundle->getFileSpecEntry(0);
134
135 zip = openReadOnly(zipFileName);
136 if (zip == NULL)
137 goto bail;
138
139 int count, i;
140
141 if (bundle->getVerbose()) {
142 printf("Archive: %s\n", zipFileName);
143 printf(
4838f309 144 " Length Method Size Ratio Offset Date Time CRC-32 Name\n");
a534180c 145 printf(
4838f309 146 "-------- ------ ------- ----- ------- ---- ---- ------ ----\n");
a534180c
TAOSP
147 }
148
149 totalUncLen = totalCompLen = 0;
150
151 count = zip->getNumEntries();
152 for (i = 0; i < count; i++) {
153 entry = zip->getEntryByIndex(i);
154 if (bundle->getVerbose()) {
155 char dateBuf[32];
156 time_t when;
157
158 when = entry->getModWhen();
159 strftime(dateBuf, sizeof(dateBuf), "%m-%d-%y %H:%M",
160 localtime(&when));
161
4838f309 162 printf("%8ld %-7.7s %7ld %3d%% %8zd %s %08lx %s\n",
a534180c
TAOSP
163 (long) entry->getUncompressedLen(),
164 compressionName(entry->getCompressionMethod()),
165 (long) entry->getCompressedLen(),
166 calcPercent(entry->getUncompressedLen(),
167 entry->getCompressedLen()),
4838f309 168 (size_t) entry->getLFHOffset(),
a534180c
TAOSP
169 dateBuf,
170 entry->getCRC32(),
171 entry->getFileName());
172 } else {
173 printf("%s\n", entry->getFileName());
174 }
175
176 totalUncLen += entry->getUncompressedLen();
177 totalCompLen += entry->getCompressedLen();
178 }
179
180 if (bundle->getVerbose()) {
181 printf(
182 "-------- ------- --- -------\n");
183 printf("%8ld %7ld %2d%% %d files\n",
184 totalUncLen,
185 totalCompLen,
186 calcPercent(totalUncLen, totalCompLen),
187 zip->getNumEntries());
188 }
189
190 if (bundle->getAndroidList()) {
191 AssetManager assets;
192 if (!assets.addAssetPath(String8(zipFileName), NULL)) {
193 fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n");
194 goto bail;
195 }
36b80681 196
a534180c
TAOSP
197 const ResTable& res = assets.getResources(false);
198 if (&res == NULL) {
199 printf("\nNo resource table found.\n");
200 } else {
c70f9935 201#ifndef HAVE_ANDROID_OS
a534180c 202 printf("\nResource table:\n");
7751daa4 203 res.print(false);
c70f9935 204#endif
a534180c 205 }
36b80681 206
a534180c
TAOSP
207 Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
208 Asset::ACCESS_BUFFER);
209 if (manifestAsset == NULL) {
210 printf("\nNo AndroidManifest.xml found.\n");
211 } else {
212 printf("\nAndroid manifest:\n");
213 ResXMLTree tree;
214 tree.setTo(manifestAsset->getBuffer(true),
215 manifestAsset->getLength());
216 printXMLBlock(&tree);
217 }
218 delete manifestAsset;
219 }
36b80681 220
a534180c
TAOSP
221 result = 0;
222
223bail:
224 delete zip;
225 return result;
226}
227
228static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
229{
230 size_t N = tree.getAttributeCount();
231 for (size_t i=0; i<N; i++) {
232 if (tree.getAttributeNameResID(i) == attrRes) {
233 return (ssize_t)i;
234 }
235 }
236 return -1;
237}
238
6648ff78 239String8 getAttribute(const ResXMLTree& tree, const char* ns,
a534180c
TAOSP
240 const char* attr, String8* outError)
241{
242 ssize_t idx = tree.indexOfAttribute(ns, attr);
243 if (idx < 0) {
244 return String8();
245 }
246 Res_value value;
247 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
248 if (value.dataType != Res_value::TYPE_STRING) {
249 if (outError != NULL) *outError = "attribute is not a string value";
250 return String8();
251 }
252 }
253 size_t len;
254 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
255 return str ? String8(str, len) : String8();
256}
257
258static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
259{
260 ssize_t idx = indexOfAttribute(tree, attrRes);
261 if (idx < 0) {
262 return String8();
263 }
264 Res_value value;
265 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
266 if (value.dataType != Res_value::TYPE_STRING) {
267 if (outError != NULL) *outError = "attribute is not a string value";
268 return String8();
269 }
270 }
271 size_t len;
272 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
273 return str ? String8(str, len) : String8();
274}
275
59308677
DH
276static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
277 String8* outError, int32_t defValue = -1)
a534180c
TAOSP
278{
279 ssize_t idx = indexOfAttribute(tree, attrRes);
280 if (idx < 0) {
59308677 281 return defValue;
a534180c
TAOSP
282 }
283 Res_value value;
284 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
59308677
DH
285 if (value.dataType < Res_value::TYPE_FIRST_INT
286 || value.dataType > Res_value::TYPE_LAST_INT) {
a534180c 287 if (outError != NULL) *outError = "attribute is not an integer value";
59308677 288 return defValue;
a534180c
TAOSP
289 }
290 }
291 return value.data;
292}
293
294static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
295 uint32_t attrRes, String8* outError)
296{
297 ssize_t idx = indexOfAttribute(tree, attrRes);
298 if (idx < 0) {
299 return String8();
300 }
301 Res_value value;
302 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
303 if (value.dataType == Res_value::TYPE_STRING) {
304 size_t len;
305 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
306 return str ? String8(str, len) : String8();
307 }
308 resTable->resolveReference(&value, 0);
309 if (value.dataType != Res_value::TYPE_STRING) {
310 if (outError != NULL) *outError = "attribute is not a string value";
311 return String8();
312 }
313 }
314 size_t len;
315 const Res_value* value2 = &value;
316 const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
317 return str ? String8(str, len) : String8();
318}
319
320// These are attribute resource constants for the platform, as found
321// in android.R.attr
322enum {
323 NAME_ATTR = 0x01010003,
324 VERSION_CODE_ATTR = 0x0101021b,
325 VERSION_NAME_ATTR = 0x0101021c,
326 LABEL_ATTR = 0x01010001,
327 ICON_ATTR = 0x01010002,
59308677 328 MIN_SDK_VERSION_ATTR = 0x0101020c,
b6cda18d 329 MAX_SDK_VERSION_ATTR = 0x01010271,
59308677
DH
330 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
331 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
332 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
333 REQ_NAVIGATION_ATTR = 0x0101022a,
334 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
335 TARGET_SDK_VERSION_ATTR = 0x01010270,
336 TEST_ONLY_ATTR = 0x01010272,
337 DENSITY_ATTR = 0x0101026c,
7e029053 338 GL_ES_VERSION_ATTR = 0x01010281,
7ea6703c
DH
339 SMALL_SCREEN_ATTR = 0x01010284,
340 NORMAL_SCREEN_ATTR = 0x01010285,
341 LARGE_SCREEN_ATTR = 0x01010286,
74fa3103 342 XLARGE_SCREEN_ATTR = 0x010102bf,
7e029053 343 REQUIRED_ATTR = 0x0101028e,
a534180c
TAOSP
344};
345
36b80681
SA
346const char *getComponentName(String8 &pkgName, String8 &componentName) {
347 ssize_t idx = componentName.find(".");
348 String8 retStr(pkgName);
349 if (idx == 0) {
350 retStr += componentName;
351 } else if (idx < 0) {
352 retStr += ".";
353 retStr += componentName;
354 } else {
355 return componentName.string();
356 }
357 return retStr.string();
358}
359
a534180c
TAOSP
360/*
361 * Handle the "dump" command, to extract select data from an archive.
362 */
363int doDump(Bundle* bundle)
364{
365 status_t result = UNKNOWN_ERROR;
366 Asset* asset = NULL;
36b80681 367
a534180c
TAOSP
368 if (bundle->getFileSpecCount() < 1) {
369 fprintf(stderr, "ERROR: no dump option specified\n");
370 return 1;
371 }
36b80681 372
a534180c
TAOSP
373 if (bundle->getFileSpecCount() < 2) {
374 fprintf(stderr, "ERROR: no dump file specified\n");
375 return 1;
376 }
36b80681 377
a534180c
TAOSP
378 const char* option = bundle->getFileSpecEntry(0);
379 const char* filename = bundle->getFileSpecEntry(1);
36b80681 380
a534180c 381 AssetManager assets;
59308677
DH
382 void* assetsCookie;
383 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
a534180c
TAOSP
384 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
385 return 1;
386 }
36b80681 387
a534180c
TAOSP
388 const ResTable& res = assets.getResources(false);
389 if (&res == NULL) {
390 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
391 goto bail;
392 }
36b80681 393
a534180c 394 if (strcmp("resources", option) == 0) {
c70f9935 395#ifndef HAVE_ANDROID_OS
7751daa4 396 res.print(bundle->getValues());
c70f9935 397#endif
a534180c
TAOSP
398 } else if (strcmp("xmltree", option) == 0) {
399 if (bundle->getFileSpecCount() < 3) {
400 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
401 goto bail;
402 }
36b80681 403
a534180c
TAOSP
404 for (int i=2; i<bundle->getFileSpecCount(); i++) {
405 const char* resname = bundle->getFileSpecEntry(i);
406 ResXMLTree tree;
407 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
408 if (asset == NULL) {
7a97f12f 409 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
a534180c
TAOSP
410 goto bail;
411 }
36b80681 412
a534180c
TAOSP
413 if (tree.setTo(asset->getBuffer(true),
414 asset->getLength()) != NO_ERROR) {
415 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
416 goto bail;
417 }
418 tree.restart();
419 printXMLBlock(&tree);
15c62a5b 420 tree.uninit();
a534180c
TAOSP
421 delete asset;
422 asset = NULL;
423 }
36b80681 424
a534180c
TAOSP
425 } else if (strcmp("xmlstrings", option) == 0) {
426 if (bundle->getFileSpecCount() < 3) {
427 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
428 goto bail;
429 }
36b80681 430
a534180c
TAOSP
431 for (int i=2; i<bundle->getFileSpecCount(); i++) {
432 const char* resname = bundle->getFileSpecEntry(i);
433 ResXMLTree tree;
434 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
435 if (asset == NULL) {
7a97f12f 436 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
a534180c
TAOSP
437 goto bail;
438 }
36b80681 439
a534180c
TAOSP
440 if (tree.setTo(asset->getBuffer(true),
441 asset->getLength()) != NO_ERROR) {
442 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
443 goto bail;
444 }
445 printStringPool(&tree.getStrings());
446 delete asset;
447 asset = NULL;
448 }
36b80681 449
a534180c
TAOSP
450 } else {
451 ResXMLTree tree;
452 asset = assets.openNonAsset("AndroidManifest.xml",
453 Asset::ACCESS_BUFFER);
454 if (asset == NULL) {
455 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
456 goto bail;
457 }
36b80681 458
a534180c
TAOSP
459 if (tree.setTo(asset->getBuffer(true),
460 asset->getLength()) != NO_ERROR) {
461 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
462 goto bail;
463 }
464 tree.restart();
36b80681 465
a534180c
TAOSP
466 if (strcmp("permissions", option) == 0) {
467 size_t len;
468 ResXMLTree::event_code_t code;
469 int depth = 0;
470 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
471 if (code == ResXMLTree::END_TAG) {
472 depth--;
473 continue;
474 }
475 if (code != ResXMLTree::START_TAG) {
476 continue;
477 }
478 depth++;
479 String8 tag(tree.getElementName(&len));
480 //printf("Depth %d tag %s\n", depth, tag.string());
481 if (depth == 1) {
482 if (tag != "manifest") {
483 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
484 goto bail;
485 }
486 String8 pkg = getAttribute(tree, NULL, "package", NULL);
487 printf("package: %s\n", pkg.string());
488 } else if (depth == 2 && tag == "permission") {
489 String8 error;
490 String8 name = getAttribute(tree, NAME_ATTR, &error);
491 if (error != "") {
492 fprintf(stderr, "ERROR: %s\n", error.string());
493 goto bail;
494 }
495 printf("permission: %s\n", name.string());
496 } else if (depth == 2 && tag == "uses-permission") {
497 String8 error;
498 String8 name = getAttribute(tree, NAME_ATTR, &error);
499 if (error != "") {
500 fprintf(stderr, "ERROR: %s\n", error.string());
501 goto bail;
502 }
503 printf("uses-permission: %s\n", name.string());
504 }
505 }
506 } else if (strcmp("badging", option) == 0) {
507 size_t len;
508 ResXMLTree::event_code_t code;
509 int depth = 0;
510 String8 error;
511 bool withinActivity = false;
512 bool isMainActivity = false;
513 bool isLauncherActivity = false;
a3055f1b 514 bool isSearchable = false;
36b80681
SA
515 bool withinApplication = false;
516 bool withinReceiver = false;
a3055f1b
SA
517 bool withinService = false;
518 bool withinIntentFilter = false;
519 bool hasMainActivity = false;
520 bool hasOtherActivities = false;
521 bool hasOtherReceivers = false;
522 bool hasOtherServices = false;
523 bool hasWallpaperService = false;
524 bool hasImeService = false;
525 bool hasWidgetReceivers = false;
526 bool hasIntentFilter = false;
527 bool actMainActivity = false;
528 bool actWidgetReceivers = false;
529 bool actImeService = false;
530 bool actWallpaperService = false;
7276d4a4
DM
531
532 // This next group of variables is used to implement a group of
533 // backward-compatibility heuristics necessitated by the addition of
534 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
535 // heuristic is "if an app requests a permission but doesn't explicitly
536 // request the corresponding <uses-feature>, presume it's there anyway".
537 bool specCameraFeature = false; // camera-related
538 bool specCameraAutofocusFeature = false;
539 bool reqCameraAutofocusFeature = false;
540 bool reqCameraFlashFeature = false;
7e029053 541 bool hasCameraPermission = false;
7276d4a4
DM
542 bool specLocationFeature = false; // location-related
543 bool specNetworkLocFeature = false;
544 bool reqNetworkLocFeature = false;
af945cf3 545 bool specGpsFeature = false;
7276d4a4
DM
546 bool reqGpsFeature = false;
547 bool hasMockLocPermission = false;
548 bool hasCoarseLocPermission = false;
af945cf3 549 bool hasGpsPermission = false;
7276d4a4
DM
550 bool hasGeneralLocPermission = false;
551 bool specBluetoothFeature = false; // Bluetooth API-related
552 bool hasBluetoothPermission = false;
553 bool specMicrophoneFeature = false; // microphone-related
554 bool hasRecordAudioPermission = false;
555 bool specWiFiFeature = false;
556 bool hasWiFiPermission = false;
557 bool specTelephonyFeature = false; // telephony-related
558 bool reqTelephonySubFeature = false;
559 bool hasTelephonyPermission = false;
560 bool specTouchscreenFeature = false; // touchscreen-related
561 bool specMultitouchFeature = false;
562 bool reqDistinctMultitouchFeature = false;
563 // 2.2 also added some other features that apps can request, but that
564 // have no corresponding permission, so we cannot implement any
565 // back-compatibility heuristic for them. The below are thus unnecessary
566 // (but are retained here for documentary purposes.)
567 //bool specCompassFeature = false;
568 //bool specAccelerometerFeature = false;
569 //bool specProximityFeature = false;
570 //bool specAmbientLightFeature = false;
571 //bool specLiveWallpaperFeature = false;
572
7ea6703c
DH
573 int targetSdk = 0;
574 int smallScreen = 1;
575 int normalScreen = 1;
576 int largeScreen = 1;
74fa3103 577 int xlargeScreen = 1;
36b80681 578 String8 pkg;
a534180c
TAOSP
579 String8 activityName;
580 String8 activityLabel;
581 String8 activityIcon;
36b80681 582 String8 receiverName;
a3055f1b 583 String8 serviceName;
a534180c
TAOSP
584 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
585 if (code == ResXMLTree::END_TAG) {
586 depth--;
a3055f1b
SA
587 if (depth < 2) {
588 withinApplication = false;
589 } else if (depth < 3) {
590 if (withinActivity && isMainActivity && isLauncherActivity) {
591 const char *aName = getComponentName(pkg, activityName);
592 if (aName != NULL) {
593 printf("launchable activity name='%s'", aName);
594 }
595 printf("label='%s' icon='%s'\n",
596 activityLabel.string(),
597 activityIcon.string());
598 }
599 if (!hasIntentFilter) {
600 hasOtherActivities |= withinActivity;
601 hasOtherReceivers |= withinReceiver;
602 hasOtherServices |= withinService;
603 }
604 withinActivity = false;
605 withinService = false;
606 withinReceiver = false;
607 hasIntentFilter = false;
608 isMainActivity = isLauncherActivity = false;
609 } else if (depth < 4) {
610 if (withinIntentFilter) {
611 if (withinActivity) {
612 hasMainActivity |= actMainActivity;
613 hasOtherActivities |= !actMainActivity;
614 } else if (withinReceiver) {
615 hasWidgetReceivers |= actWidgetReceivers;
616 hasOtherReceivers |= !actWidgetReceivers;
617 } else if (withinService) {
618 hasImeService |= actImeService;
619 hasWallpaperService |= actWallpaperService;
620 hasOtherServices |= (!actImeService && !actWallpaperService);
621 }
622 }
623 withinIntentFilter = false;
624 }
a534180c
TAOSP
625 continue;
626 }
627 if (code != ResXMLTree::START_TAG) {
628 continue;
629 }
630 depth++;
631 String8 tag(tree.getElementName(&len));
a3055f1b 632 //printf("Depth %d, %s\n", depth, tag.string());
a534180c
TAOSP
633 if (depth == 1) {
634 if (tag != "manifest") {
635 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
636 goto bail;
637 }
36b80681 638 pkg = getAttribute(tree, NULL, "package", NULL);
a534180c
TAOSP
639 printf("package: name='%s' ", pkg.string());
640 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
641 if (error != "") {
642 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
643 goto bail;
644 }
645 if (versionCode > 0) {
646 printf("versionCode='%d' ", versionCode);
647 } else {
648 printf("versionCode='' ");
649 }
1423619a 650 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
a534180c
TAOSP
651 if (error != "") {
652 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
653 goto bail;
654 }
655 printf("versionName='%s'\n", versionName.string());
36b80681
SA
656 } else if (depth == 2) {
657 withinApplication = false;
658 if (tag == "application") {
659 withinApplication = true;
660 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
661 if (error != "") {
662 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
663 goto bail;
664 }
665 printf("application: label='%s' ", label.string());
666 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
667 if (error != "") {
668 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
669 goto bail;
670 }
671 printf("icon='%s'\n", icon.string());
59308677 672 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
36b80681 673 if (error != "") {
59308677 674 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
36b80681
SA
675 goto bail;
676 }
59308677
DH
677 if (testOnly != 0) {
678 printf("testOnly='%d'\n", testOnly);
679 }
680 } else if (tag == "uses-sdk") {
681 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
682 if (error != "") {
683 error = "";
684 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
685 if (error != "") {
686 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
687 error.string());
688 goto bail;
689 }
7ea6703c 690 if (name == "Donut") targetSdk = 4;
59308677
DH
691 printf("sdkVersion:'%s'\n", name.string());
692 } else if (code != -1) {
7ea6703c 693 targetSdk = code;
59308677
DH
694 printf("sdkVersion:'%d'\n", code);
695 }
b6cda18d
SA
696 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
697 if (code != -1) {
698 printf("maxSdkVersion:'%d'\n", code);
699 }
59308677
DH
700 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
701 if (error != "") {
702 error = "";
703 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
704 if (error != "") {
705 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
706 error.string());
707 goto bail;
708 }
7ea6703c 709 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
59308677
DH
710 printf("targetSdkVersion:'%s'\n", name.string());
711 } else if (code != -1) {
7ea6703c
DH
712 if (targetSdk < code) {
713 targetSdk = code;
714 }
59308677
DH
715 printf("targetSdkVersion:'%d'\n", code);
716 }
717 } else if (tag == "uses-configuration") {
718 int32_t reqTouchScreen = getIntegerAttribute(tree,
719 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
720 int32_t reqKeyboardType = getIntegerAttribute(tree,
721 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
722 int32_t reqHardKeyboard = getIntegerAttribute(tree,
723 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
724 int32_t reqNavigation = getIntegerAttribute(tree,
725 REQ_NAVIGATION_ATTR, NULL, 0);
726 int32_t reqFiveWayNav = getIntegerAttribute(tree,
727 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
9c0366d0 728 printf("uses-configuration:");
59308677
DH
729 if (reqTouchScreen != 0) {
730 printf(" reqTouchScreen='%d'", reqTouchScreen);
731 }
732 if (reqKeyboardType != 0) {
733 printf(" reqKeyboardType='%d'", reqKeyboardType);
734 }
735 if (reqHardKeyboard != 0) {
736 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
737 }
738 if (reqNavigation != 0) {
739 printf(" reqNavigation='%d'", reqNavigation);
36b80681 740 }
59308677
DH
741 if (reqFiveWayNav != 0) {
742 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
743 }
744 printf("\n");
745 } else if (tag == "supports-density") {
746 int32_t dens = getIntegerAttribute(tree, DENSITY_ATTR, &error);
747 if (error != "") {
748 fprintf(stderr, "ERROR getting 'android:density' attribute: %s\n",
749 error.string());
750 goto bail;
751 }
752 printf("supports-density:'%d'\n", dens);
7ea6703c
DH
753 } else if (tag == "supports-screens") {
754 smallScreen = getIntegerAttribute(tree,
755 SMALL_SCREEN_ATTR, NULL, 1);
756 normalScreen = getIntegerAttribute(tree,
757 NORMAL_SCREEN_ATTR, NULL, 1);
758 largeScreen = getIntegerAttribute(tree,
759 LARGE_SCREEN_ATTR, NULL, 1);
74fa3103
DH
760 xlargeScreen = getIntegerAttribute(tree,
761 XLARGE_SCREEN_ATTR, NULL, 1);
7e029053
DH
762 } else if (tag == "uses-feature") {
763 String8 name = getAttribute(tree, NAME_ATTR, &error);
774d89bf
SA
764
765 if (name != "" && error == "") {
7e029053
DH
766 int req = getIntegerAttribute(tree,
767 REQUIRED_ATTR, NULL, 1);
7276d4a4 768
7e029053
DH
769 if (name == "android.hardware.camera") {
770 specCameraFeature = true;
7276d4a4
DM
771 } else if (name == "android.hardware.camera.autofocus") {
772 // these have no corresponding permission to check for,
773 // but should imply the foundational camera permission
774 reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
775 specCameraAutofocusFeature = true;
776 } else if (req && (name == "android.hardware.camera.flash")) {
777 // these have no corresponding permission to check for,
778 // but should imply the foundational camera permission
779 reqCameraFlashFeature = true;
780 } else if (name == "android.hardware.location") {
781 specLocationFeature = true;
782 } else if (name == "android.hardware.location.network") {
783 specNetworkLocFeature = true;
784 reqNetworkLocFeature = reqNetworkLocFeature || req;
af945cf3
DH
785 } else if (name == "android.hardware.location.gps") {
786 specGpsFeature = true;
7276d4a4
DM
787 reqGpsFeature = reqGpsFeature || req;
788 } else if (name == "android.hardware.bluetooth") {
789 specBluetoothFeature = true;
790 } else if (name == "android.hardware.touchscreen") {
791 specTouchscreenFeature = true;
792 } else if (name == "android.hardware.touchscreen.multitouch") {
793 specMultitouchFeature = true;
794 } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
795 reqDistinctMultitouchFeature = reqDistinctMultitouchFeature || req;
796 } else if (name == "android.hardware.microphone") {
797 specMicrophoneFeature = true;
798 } else if (name == "android.hardware.wifi") {
799 specWiFiFeature = true;
800 } else if (name == "android.hardware.telephony") {
801 specTelephonyFeature = true;
802 } else if (req && (name == "android.hardware.telephony.gsm" ||
803 name == "android.hardware.telephony.cdma")) {
804 // these have no corresponding permission to check for,
805 // but should imply the foundational telephony permission
806 reqTelephonySubFeature = true;
7e029053
DH
807 }
808 printf("uses-feature%s:'%s'\n",
809 req ? "" : "-not-required", name.string());
810 } else {
811 int vers = getIntegerAttribute(tree,
812 GL_ES_VERSION_ATTR, &error);
813 if (error == "") {
814 printf("uses-gl-es:'0x%x'\n", vers);
815 }
816 }
817 } else if (tag == "uses-permission") {
818 String8 name = getAttribute(tree, NAME_ATTR, &error);
774d89bf 819 if (name != "" && error == "") {
7e029053
DH
820 if (name == "android.permission.CAMERA") {
821 hasCameraPermission = true;
af945cf3
DH
822 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
823 hasGpsPermission = true;
7276d4a4
DM
824 } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
825 hasMockLocPermission = true;
826 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
827 hasCoarseLocPermission = true;
828 } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
829 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
830 hasGeneralLocPermission = true;
831 } else if (name == "android.permission.BLUETOOTH" ||
832 name == "android.permission.BLUETOOTH_ADMIN") {
833 hasBluetoothPermission = true;
834 } else if (name == "android.permission.RECORD_AUDIO") {
835 hasRecordAudioPermission = true;
836 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
837 name == "android.permission.CHANGE_WIFI_STATE" ||
838 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
839 hasWiFiPermission = true;
840 } else if (name == "android.permission.CALL_PHONE" ||
841 name == "android.permission.CALL_PRIVILEGED" ||
842 name == "android.permission.MODIFY_PHONE_STATE" ||
843 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
844 name == "android.permission.READ_SMS" ||
845 name == "android.permission.RECEIVE_SMS" ||
846 name == "android.permission.RECEIVE_MMS" ||
847 name == "android.permission.RECEIVE_WAP_PUSH" ||
848 name == "android.permission.SEND_SMS" ||
849 name == "android.permission.WRITE_APN_SETTINGS" ||
850 name == "android.permission.WRITE_SMS") {
851 hasTelephonyPermission = true;
7e029053
DH
852 }
853 printf("uses-permission:'%s'\n", name.string());
854 } else {
855 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
856 error.string());
857 goto bail;
858 }
a0a01587
DH
859 } else if (tag == "uses-package") {
860 String8 name = getAttribute(tree, NAME_ATTR, &error);
861 if (name != "" && error == "") {
862 printf("uses-package:'%s'\n", name.string());
863 } else {
864 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
865 error.string());
866 goto bail;
867 }
2db8b02f
JH
868 } else if (tag == "original-package") {
869 String8 name = getAttribute(tree, NAME_ATTR, &error);
870 if (name != "" && error == "") {
871 printf("original-package:'%s'\n", name.string());
872 } else {
873 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
874 error.string());
875 goto bail;
876 }
a534180c 877 }
36b80681
SA
878 } else if (depth == 3 && withinApplication) {
879 withinActivity = false;
880 withinReceiver = false;
a3055f1b
SA
881 withinService = false;
882 hasIntentFilter = false;
36b80681
SA
883 if(tag == "activity") {
884 withinActivity = true;
885 activityName = getAttribute(tree, NAME_ATTR, &error);
886 if (error != "") {
887 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
888 goto bail;
889 }
a534180c 890
36b80681
SA
891 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
892 if (error != "") {
893 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
894 goto bail;
895 }
a534180c 896
36b80681 897 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
a534180c 898 if (error != "") {
36b80681 899 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
a534180c
TAOSP
900 goto bail;
901 }
36b80681
SA
902 } else if (tag == "uses-library") {
903 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
904 if (error != "") {
905 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
906 goto bail;
a534180c 907 }
826ff9a6
DH
908 int req = getIntegerAttribute(tree,
909 REQUIRED_ATTR, NULL, 1);
910 printf("uses-library%s:'%s'\n",
911 req ? "" : "-not-required", libraryName.string());
36b80681
SA
912 } else if (tag == "receiver") {
913 withinReceiver = true;
914 receiverName = getAttribute(tree, NAME_ATTR, &error);
915
a534180c 916 if (error != "") {
36b80681 917 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
a534180c
TAOSP
918 goto bail;
919 }
a3055f1b
SA
920 } else if (tag == "service") {
921 withinService = true;
922 serviceName = getAttribute(tree, NAME_ATTR, &error);
923
924 if (error != "") {
925 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
926 goto bail;
927 }
36b80681 928 }
a3055f1b
SA
929 } else if ((depth == 4) && (tag == "intent-filter")) {
930 hasIntentFilter = true;
931 withinIntentFilter = true;
932 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
933 } else if ((depth == 5) && withinIntentFilter){
934 String8 action;
935 if (tag == "action") {
936 action = getAttribute(tree, NAME_ATTR, &error);
937 if (error != "") {
938 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
939 goto bail;
940 }
941 if (withinActivity) {
59308677
DH
942 if (action == "android.intent.action.MAIN") {
943 isMainActivity = true;
a3055f1b 944 actMainActivity = true;
59308677 945 }
a3055f1b
SA
946 } else if (withinReceiver) {
947 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
948 actWidgetReceivers = true;
36b80681 949 }
a3055f1b
SA
950 } else if (withinService) {
951 if (action == "android.view.InputMethod") {
952 actImeService = true;
953 } else if (action == "android.service.wallpaper.WallpaperService") {
954 actWallpaperService = true;
36b80681
SA
955 }
956 }
a3055f1b
SA
957 if (action == "android.intent.action.SEARCH") {
958 isSearchable = true;
36b80681 959 }
a534180c 960 }
a534180c 961
a3055f1b
SA
962 if (tag == "category") {
963 String8 category = getAttribute(tree, NAME_ATTR, &error);
964 if (error != "") {
965 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
966 goto bail;
967 }
968 if (withinActivity) {
969 if (category == "android.intent.category.LAUNCHER") {
970 isLauncherActivity = true;
971 }
972 }
36b80681 973 }
a534180c
TAOSP
974 }
975 }
a3055f1b 976
7276d4a4
DM
977 /* The following blocks handle printing "inferred" uses-features, based
978 * on whether related features or permissions are used by the app.
979 * Note that the various spec*Feature variables denote whether the
980 * relevant tag was *present* in the AndroidManfest, not that it was
981 * present and set to true.
982 */
983 // Camera-related back-compatibility logic
984 if (!specCameraFeature) {
985 if (reqCameraFlashFeature || reqCameraAutofocusFeature) {
986 // if app requested a sub-feature (autofocus or flash) and didn't
987 // request the base camera feature, we infer that it meant to
988 printf("uses-feature:'android.hardware.camera'\n");
989 } else if (hasCameraPermission) {
990 // if app wants to use camera but didn't request the feature, we infer
991 // that it meant to, and further that it wants autofocus
992 // (which was the 1.0 - 1.5 behavior)
993 printf("uses-feature:'android.hardware.camera'\n");
994 if (!specCameraAutofocusFeature) {
995 printf("uses-feature:'android.hardware.camera.autofocus'\n");
996 }
997 }
7e029053 998 }
1e8883fc 999
7276d4a4
DM
1000 // Location-related back-compatibility logic
1001 if (!specLocationFeature &&
1002 (hasMockLocPermission || hasCoarseLocPermission || hasGpsPermission ||
1003 hasGeneralLocPermission || reqNetworkLocFeature || reqGpsFeature)) {
1004 // if app either takes a location-related permission or requests one of the
1005 // sub-features, we infer that it also meant to request the base location feature
1006 printf("uses-feature:'android.hardware.location'\n");
1007 }
af945cf3 1008 if (!specGpsFeature && hasGpsPermission) {
7276d4a4
DM
1009 // if app takes GPS (FINE location) perm but does not request the GPS
1010 // feature, we infer that it meant to
af945cf3
DH
1011 printf("uses-feature:'android.hardware.location.gps'\n");
1012 }
7276d4a4
DM
1013 if (!specNetworkLocFeature && hasCoarseLocPermission) {
1014 // if app takes Network location (COARSE location) perm but does not request the
1015 // network location feature, we infer that it meant to
1016 printf("uses-feature:'android.hardware.location.network'\n");
1017 }
1018
1019 // Bluetooth-related compatibility logic
b1b45912 1020 if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
7276d4a4
DM
1021 // if app takes a Bluetooth permission but does not request the Bluetooth
1022 // feature, we infer that it meant to
1023 printf("uses-feature:'android.hardware.bluetooth'\n");
1024 }
1025
1026 // Microphone-related compatibility logic
1027 if (!specMicrophoneFeature && hasRecordAudioPermission) {
1028 // if app takes the record-audio permission but does not request the microphone
1029 // feature, we infer that it meant to
1030 printf("uses-feature:'android.hardware.microphone'\n");
1031 }
1032
1033 // WiFi-related compatibility logic
1034 if (!specWiFiFeature && hasWiFiPermission) {
1035 // if app takes one of the WiFi permissions but does not request the WiFi
1036 // feature, we infer that it meant to
1037 printf("uses-feature:'android.hardware.wifi'\n");
1038 }
1039
1040 // Telephony-related compatibility logic
1041 if (!specTelephonyFeature && (hasTelephonyPermission || reqTelephonySubFeature)) {
1042 // if app takes one of the telephony permissions or requests a sub-feature but
1043 // does not request the base telephony feature, we infer that it meant to
1044 printf("uses-feature:'android.hardware.telephony'\n");
1045 }
1046
1047 // Touchscreen-related back-compatibility logic
1048 if (!specTouchscreenFeature) { // not a typo!
1049 // all apps are presumed to require a touchscreen, unless they explicitly say
1050 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1051 // Note that specTouchscreenFeature is true if the tag is present, regardless
1052 // of whether its value is true or false, so this is safe
1053 printf("uses-feature:'android.hardware.touchscreen'\n");
1054 }
1055 if (!specMultitouchFeature && reqDistinctMultitouchFeature) {
1056 // if app takes one of the telephony permissions or requests a sub-feature but
1057 // does not request the base telephony feature, we infer that it meant to
1058 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1059 }
af945cf3 1060
a3055f1b
SA
1061 if (hasMainActivity) {
1062 printf("main\n");
1063 }
1064 if (hasWidgetReceivers) {
1065 printf("app-widget\n");
1066 }
1067 if (hasImeService) {
1068 printf("ime\n");
1069 }
1070 if (hasWallpaperService) {
1071 printf("wallpaper\n");
1072 }
1073 if (hasOtherActivities) {
1074 printf("other-activities\n");
1075 }
1076 if (isSearchable) {
1077 printf("search\n");
1078 }
1079 if (hasOtherReceivers) {
1080 printf("other-receivers\n");
1081 }
1082 if (hasOtherServices) {
1083 printf("other-services\n");
1084 }
1085
7ea6703c
DH
1086 // Determine default values for any unspecified screen sizes,
1087 // based on the target SDK of the package. As of 4 (donut)
1088 // the screen size support was introduced, so all default to
1089 // enabled.
1090 if (smallScreen > 0) {
1091 smallScreen = targetSdk >= 4 ? -1 : 0;
1092 }
1093 if (normalScreen > 0) {
1094 normalScreen = -1;
1095 }
1096 if (largeScreen > 0) {
1097 largeScreen = targetSdk >= 4 ? -1 : 0;
1098 }
74fa3103
DH
1099 if (xlargeScreen > 0) {
1100 // Introduced in Honeycomb.
1101 xlargeScreen = targetSdk >= 10 ? -1 : 0;
1102 }
7ea6703c
DH
1103 printf("supports-screens:");
1104 if (smallScreen != 0) printf(" 'small'");
1105 if (normalScreen != 0) printf(" 'normal'");
1106 if (largeScreen != 0) printf(" 'large'");
74fa3103 1107 if (xlargeScreen != 0) printf(" 'xlarge'");
7ea6703c 1108 printf("\n");
a3055f1b 1109
a534180c
TAOSP
1110 printf("locales:");
1111 Vector<String8> locales;
1112 res.getLocales(&locales);
7751daa4
DH
1113 const size_t NL = locales.size();
1114 for (size_t i=0; i<NL; i++) {
a534180c
TAOSP
1115 const char* localeStr = locales[i].string();
1116 if (localeStr == NULL || strlen(localeStr) == 0) {
1117 localeStr = "--_--";
1118 }
1119 printf(" '%s'", localeStr);
1120 }
1121 printf("\n");
a3055f1b 1122
7751daa4
DH
1123 Vector<ResTable_config> configs;
1124 res.getConfigurations(&configs);
1125 SortedVector<int> densities;
1126 const size_t NC = configs.size();
1127 for (size_t i=0; i<NC; i++) {
1128 int dens = configs[i].density;
1129 if (dens == 0) dens = 160;
1130 densities.add(dens);
1131 }
a3055f1b 1132
7751daa4
DH
1133 printf("densities:");
1134 const size_t ND = densities.size();
1135 for (size_t i=0; i<ND; i++) {
1136 printf(" '%d'", densities[i]);
1137 }
1138 printf("\n");
a3055f1b 1139
59308677
DH
1140 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
1141 if (dir != NULL) {
1142 if (dir->getFileCount() > 0) {
1143 printf("native-code:");
1144 for (size_t i=0; i<dir->getFileCount(); i++) {
1145 printf(" '%s'", dir->getFileName(i).string());
1146 }
1147 printf("\n");
1148 }
1149 delete dir;
1150 }
a534180c
TAOSP
1151 } else if (strcmp("configurations", option) == 0) {
1152 Vector<ResTable_config> configs;
1153 res.getConfigurations(&configs);
1154 const size_t N = configs.size();
1155 for (size_t i=0; i<N; i++) {
1156 printf("%s\n", configs[i].toString().string());
1157 }
1158 } else {
1159 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
1160 goto bail;
1161 }
1162 }
1163
1164 result = NO_ERROR;
36b80681 1165
a534180c
TAOSP
1166bail:
1167 if (asset) {
1168 delete asset;
1169 }
1170 return (result != NO_ERROR);
1171}
1172
1173
1174/*
1175 * Handle the "add" command, which wants to add files to a new or
1176 * pre-existing archive.
1177 */
1178int doAdd(Bundle* bundle)
1179{
1180 ZipFile* zip = NULL;
1181 status_t result = UNKNOWN_ERROR;
1182 const char* zipFileName;
1183
1184 if (bundle->getUpdate()) {
1185 /* avoid confusion */
1186 fprintf(stderr, "ERROR: can't use '-u' with add\n");
1187 goto bail;
1188 }
1189
1190 if (bundle->getFileSpecCount() < 1) {
1191 fprintf(stderr, "ERROR: must specify zip file name\n");
1192 goto bail;
1193 }
1194 zipFileName = bundle->getFileSpecEntry(0);
1195
1196 if (bundle->getFileSpecCount() < 2) {
1197 fprintf(stderr, "NOTE: nothing to do\n");
1198 goto bail;
1199 }
1200
1201 zip = openReadWrite(zipFileName, true);
1202 if (zip == NULL) {
1203 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1204 goto bail;
1205 }
1206
1207 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1208 const char* fileName = bundle->getFileSpecEntry(i);
1209
1210 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1211 printf(" '%s'... (from gzip)\n", fileName);
1212 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1213 } else {
1e8883fc
DZ
1214 if (bundle->getJunkPath()) {
1215 String8 storageName = String8(fileName).getPathLeaf();
1216 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1217 result = zip->add(fileName, storageName.string(),
1218 bundle->getCompressionMethod(), NULL);
1219 } else {
1220 printf(" '%s'...\n", fileName);
1221 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1222 }
a534180c
TAOSP
1223 }
1224 if (result != NO_ERROR) {
1225 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1226 if (result == NAME_NOT_FOUND)
1227 fprintf(stderr, ": file not found\n");
1228 else if (result == ALREADY_EXISTS)
1229 fprintf(stderr, ": already exists in archive\n");
1230 else
1231 fprintf(stderr, "\n");
1232 goto bail;
1233 }
1234 }
1235
1236 result = NO_ERROR;
1237
1238bail:
1239 delete zip;
1240 return (result != NO_ERROR);
1241}
1242
1243
1244/*
1245 * Delete files from an existing archive.
1246 */
1247int doRemove(Bundle* bundle)
1248{
1249 ZipFile* zip = NULL;
1250 status_t result = UNKNOWN_ERROR;
1251 const char* zipFileName;
1252
1253 if (bundle->getFileSpecCount() < 1) {
1254 fprintf(stderr, "ERROR: must specify zip file name\n");
1255 goto bail;
1256 }
1257 zipFileName = bundle->getFileSpecEntry(0);
1258
1259 if (bundle->getFileSpecCount() < 2) {
1260 fprintf(stderr, "NOTE: nothing to do\n");
1261 goto bail;
1262 }
1263
1264 zip = openReadWrite(zipFileName, false);
1265 if (zip == NULL) {
1266 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1267 zipFileName);
1268 goto bail;
1269 }
1270
1271 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1272 const char* fileName = bundle->getFileSpecEntry(i);
1273 ZipEntry* entry;
1274
1275 entry = zip->getEntryByName(fileName);
1276 if (entry == NULL) {
1277 printf(" '%s' NOT FOUND\n", fileName);
1278 continue;
1279 }
1280
1281 result = zip->remove(entry);
1282
1283 if (result != NO_ERROR) {
1284 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1285 bundle->getFileSpecEntry(i), zipFileName);
1286 goto bail;
1287 }
1288 }
1289
1290 /* update the archive */
1291 zip->flush();
1292
1293bail:
1294 delete zip;
1295 return (result != NO_ERROR);
1296}
1297
1298
1299/*
1300 * Package up an asset directory and associated application files.
1301 */
1302int doPackage(Bundle* bundle)
1303{
1304 const char* outputAPKFile;
1305 int retVal = 1;
1306 status_t err;
1307 sp<AaptAssets> assets;
1308 int N;
1309
1310 // -c zz_ZZ means do pseudolocalization
1311 ResourceFilter filter;
1312 err = filter.parse(bundle->getConfigurations());
1313 if (err != NO_ERROR) {
1314 goto bail;
1315 }
1316 if (filter.containsPseudo()) {
1317 bundle->setPseudolocalize(true);
1318 }
1319
1320 N = bundle->getFileSpecCount();
1321 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1322 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1323 fprintf(stderr, "ERROR: no input files\n");
1324 goto bail;
1325 }
1326
1327 outputAPKFile = bundle->getOutputAPKFile();
1328
1329 // Make sure the filenames provided exist and are of the appropriate type.
1330 if (outputAPKFile) {
1331 FileType type;
1332 type = getFileType(outputAPKFile);
1333 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1334 fprintf(stderr,
1335 "ERROR: output file '%s' exists but is not regular file\n",
1336 outputAPKFile);
1337 goto bail;
1338 }
1339 }
1340
1341 // Load the assets.
1342 assets = new AaptAssets();
1343 err = assets->slurpFromArgs(bundle);
1344 if (err < 0) {
1345 goto bail;
1346 }
1347
1348 if (bundle->getVerbose()) {
1349 assets->print();
1350 }
1351
1352 // If they asked for any files that need to be compiled, do so.
1353 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1354 err = buildResources(bundle, assets);
1355 if (err != 0) {
1356 goto bail;
1357 }
1358 }
1359
1360 // At this point we've read everything and processed everything. From here
1361 // on out it's just writing output files.
1362 if (SourcePos::hasErrors()) {
1363 goto bail;
1364 }
1365
1366 // Write out R.java constants
1367 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
fa19db5e
XD
1368 if (bundle->getCustomPackage() == NULL) {
1369 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
1370 } else {
1371 const String8 customPkg(bundle->getCustomPackage());
1372 err = writeResourceSymbols(bundle, assets, customPkg, true);
1373 }
a534180c
TAOSP
1374 if (err < 0) {
1375 goto bail;
1376 }
1377 } else {
1378 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1379 if (err < 0) {
1380 goto bail;
1381 }
1382 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1383 if (err < 0) {
1384 goto bail;
1385 }
1386 }
1387
6648ff78
JO
1388 // Write out the ProGuard file
1389 err = writeProguardFile(bundle, assets);
1390 if (err < 0) {
1391 goto bail;
1392 }
1393
a534180c
TAOSP
1394 // Write the apk
1395 if (outputAPKFile) {
1396 err = writeAPK(bundle, assets, String8(outputAPKFile));
1397 if (err != NO_ERROR) {
1398 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1399 goto bail;
1400 }
1401 }
1402
1403 retVal = 0;
1404bail:
1405 if (SourcePos::hasErrors()) {
1406 SourcePos::printErrors(stderr);
1407 }
1408 return retVal;
1409}