2 // Copyright 2006 The Android Open Source Project
4 // Package assets into Zip files.
7 #include "AaptAssets.h"
9 #include "ResourceTable.h"
10 #include "ResourceFilter.h"
12 #include <androidfw/misc.h>
14 #include <utils/Log.h>
15 #include <utils/threads.h>
16 #include <utils/List.h>
17 #include <utils/Errors.h>
18 #include <utils/misc.h>
20 #include <sys/types.h>
25 using namespace android;
27 static const char* kExcludeExtension = ".EXCLUDE";
29 /* these formats are already compressed, or don't compress well */
30 static const char* kNoCompressExt[] = {
31 ".jpg", ".jpeg", ".png", ".gif",
32 ".wav", ".mp2", ".mp3", ".ogg", ".aac",
33 ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
34 ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
35 ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
36 ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"
39 /* fwd decls, so I can write this downward */
40 ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<const OutputSet>& outputSet);
41 bool processFile(Bundle* bundle, ZipFile* zip, String8 storageName, const sp<const AaptFile>& file);
42 bool okayToCompress(Bundle* bundle, const String8& pathName);
43 ssize_t processJarFiles(Bundle* bundle, ZipFile* zip);
46 * The directory hierarchy looks like this:
47 * "outputDir" and "assetRoot" are existing directories.
49 * On success, "bundle->numPackages" will be the number of Zip packages
52 status_t writeAPK(Bundle* bundle, const String8& outputFile, const sp<OutputSet>& outputSet)
55 fprintf(stdout, "BENCHMARK: Starting APK Bundling \n");
56 long startAPKTime = clock();
57 #endif /* BENCHMARK */
59 status_t result = NO_ERROR;
63 //bundle->setPackageCount(0);
66 * Prep the Zip archive.
68 * If the file already exists, fail unless "update" or "force" is set.
69 * If "update" is set, update the contents of the existing archive.
70 * Else, if "force" is set, remove the existing archive.
72 FileType fileType = getFileType(outputFile.string());
73 if (fileType == kFileTypeNonexistent) {
74 // okay, create it below
75 } else if (fileType == kFileTypeRegular) {
76 if (bundle->getUpdate()) {
77 // okay, open it below
78 } else if (bundle->getForce()) {
79 if (unlink(outputFile.string()) != 0) {
80 fprintf(stderr, "ERROR: unable to remove '%s': %s\n", outputFile.string(),
85 fprintf(stderr, "ERROR: '%s' exists (use '-f' to force overwrite)\n",
90 fprintf(stderr, "ERROR: '%s' exists and is not a regular file\n", outputFile.string());
94 if (bundle->getVerbose()) {
95 printf("%s '%s'\n", (fileType == kFileTypeNonexistent) ? "Creating" : "Opening",
101 status = zip->open(outputFile.string(), ZipFile::kOpenReadWrite | ZipFile::kOpenCreate);
102 if (status != NO_ERROR) {
103 fprintf(stderr, "ERROR: unable to open '%s' as Zip file for writing\n",
104 outputFile.string());
108 if (bundle->getVerbose()) {
109 printf("Writing all files...\n");
112 count = processAssets(bundle, zip, outputSet);
114 fprintf(stderr, "ERROR: unable to process assets while packaging '%s'\n",
115 outputFile.string());
120 if (bundle->getVerbose()) {
121 printf("Generated %d file%s\n", count, (count==1) ? "" : "s");
124 count = processJarFiles(bundle, zip);
126 fprintf(stderr, "ERROR: unable to process jar files while packaging '%s'\n",
127 outputFile.string());
132 if (bundle->getVerbose())
133 printf("Included %d file%s from jar/zip files.\n", count, (count==1) ? "" : "s");
138 * Check for cruft. We set the "marked" flag on all entries we created
139 * or decided not to update. If the entry isn't already slated for
140 * deletion, remove it now.
143 if (bundle->getVerbose())
144 printf("Checking for deleted files\n");
146 for (i = 0; i < zip->getNumEntries(); i++) {
147 ZipEntry* entry = zip->getEntryByIndex(i);
149 if (!entry->getMarked() && entry->getDeleted()) {
150 if (bundle->getVerbose()) {
151 printf(" (removing crufty '%s')\n",
152 entry->getFileName());
158 if (bundle->getVerbose() && removed > 0)
159 printf("Removed %d file%s\n", removed, (removed==1) ? "" : "s");
162 /* tell Zip lib to process deletions and other pending changes */
163 result = zip->flush();
164 if (result != NO_ERROR) {
165 fprintf(stderr, "ERROR: Zip flush failed, archive may be hosed\n");
170 if (zip->getNumEntries() == 0) {
171 if (bundle->getVerbose()) {
172 printf("Archive is empty -- removing %s\n", outputFile.getPathLeaf().string());
174 delete zip; // close the file so we can remove it in Win32
176 if (unlink(outputFile.string()) != 0) {
177 fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
181 // If we've been asked to generate a dependency file for the .ap_ package,
183 if (bundle->getGenDependencies()) {
184 // The dependency file gets output to the same directory
185 // as the specified output file with an additional .d extension.
186 // e.g. bin/resources.ap_.d
187 String8 dependencyFile = outputFile;
188 dependencyFile.append(".d");
190 FILE* fp = fopen(dependencyFile.string(), "a");
191 // Add this file to the dependency file
192 fprintf(fp, "%s \\\n", outputFile.string());
196 assert(result == NO_ERROR);
199 delete zip; // must close before remove in Win32
200 if (result != NO_ERROR) {
201 if (bundle->getVerbose()) {
202 printf("Removing %s due to earlier failures\n", outputFile.string());
204 if (unlink(outputFile.string()) != 0) {
205 fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
209 if (result == NO_ERROR && bundle->getVerbose())
213 fprintf(stdout, "BENCHMARK: End APK Bundling. Time Elapsed: %f ms \n",(clock() - startAPKTime)/1000.0);
214 #endif /* BENCHMARK */
218 ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<const OutputSet>& outputSet)
221 const std::set<OutputEntry>& entries = outputSet->getEntries();
222 std::set<OutputEntry>::const_iterator iter = entries.begin();
223 for (; iter != entries.end(); iter++) {
224 const OutputEntry& entry = *iter;
225 if (entry.getFile() == NULL) {
226 fprintf(stderr, "warning: null file being processed.\n");
228 String8 storagePath(entry.getPath());
229 storagePath.convertToResPath();
230 if (!processFile(bundle, zip, storagePath, entry.getFile())) {
231 return UNKNOWN_ERROR;
240 * Process a regular file, adding it to the archive if appropriate.
242 * If we're in "update" mode, and the file already exists in the archive,
243 * delete the existing entry before adding the new one.
245 bool processFile(Bundle* bundle, ZipFile* zip,
246 String8 storageName, const sp<const AaptFile>& file)
248 const bool hasData = file->hasData();
251 bool fromGzip = false;
255 * See if the filename ends in ".EXCLUDE". We can't use
256 * String8::getPathExtension() because the length of what it considers
257 * to be an extension is capped.
259 * The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
260 * so there's no value in adding them (and it makes life easier on
261 * the AssetManager lib if we don't).
263 * NOTE: this restriction has been removed. If you're in this code, you
264 * should clean this up, but I'm in here getting rid of Path Name, and I
265 * don't want to make other potentially breaking changes --joeo
267 int fileNameLen = storageName.length();
268 int excludeExtensionLen = strlen(kExcludeExtension);
269 if (fileNameLen > excludeExtensionLen
270 && (0 == strcmp(storageName.string() + (fileNameLen - excludeExtensionLen),
271 kExcludeExtension))) {
272 fprintf(stderr, "warning: '%s' not added to Zip\n", storageName.string());
276 if (strcasecmp(storageName.getPathExtension().string(), ".gz") == 0) {
278 storageName = storageName.getBasePath();
281 if (bundle->getUpdate()) {
282 entry = zip->getEntryByName(storageName.string());
284 /* file already exists in archive; there can be only one */
285 if (entry->getMarked()) {
287 "ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
288 file->getPrintableSource().string());
292 const String8& srcName = file->getSourceFile();
294 fileModWhen = getFileModDate(srcName.string());
295 if (fileModWhen == (time_t) -1) { // file existence tested earlier,
296 return false; // not expecting an error here
299 if (fileModWhen > entry->getModWhen()) {
300 // mark as deleted so add() will succeed
301 if (bundle->getVerbose()) {
302 printf(" (removing old '%s')\n", storageName.string());
307 // version in archive is newer
308 if (bundle->getVerbose()) {
309 printf(" (not updating '%s')\n", storageName.string());
311 entry->setMarked(true);
315 // Generated files are always replaced.
321 //android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
324 result = zip->addGzip(file->getSourceFile().string(), storageName.string(), &entry);
325 } else if (!hasData) {
326 /* don't compress certain files, e.g. PNGs */
327 int compressionMethod = bundle->getCompressionMethod();
328 if (!okayToCompress(bundle, storageName)) {
329 compressionMethod = ZipEntry::kCompressStored;
331 result = zip->add(file->getSourceFile().string(), storageName.string(), compressionMethod,
334 result = zip->add(file->getData(), file->getSize(), storageName.string(),
335 file->getCompressionMethod(), &entry);
337 if (result == NO_ERROR) {
338 if (bundle->getVerbose()) {
339 printf(" '%s'%s", storageName.string(), fromGzip ? " (from .gz)" : "");
340 if (entry->getCompressionMethod() == ZipEntry::kCompressStored) {
341 printf(" (not compressed)\n");
343 printf(" (compressed %d%%)\n", calcPercent(entry->getUncompressedLen(),
344 entry->getCompressedLen()));
347 entry->setMarked(true);
349 if (result == ALREADY_EXISTS) {
350 fprintf(stderr, " Unable to add '%s': file already in archive (try '-u'?)\n",
351 file->getPrintableSource().string());
353 fprintf(stderr, " Unable to add '%s': Zip add failed (%d)\n",
354 file->getPrintableSource().string(), result);
363 * Determine whether or not we want to try to compress this file based
364 * on the file extension.
366 bool okayToCompress(Bundle* bundle, const String8& pathName)
368 String8 ext = pathName.getPathExtension();
371 if (ext.length() == 0)
374 for (i = 0; i < NELEM(kNoCompressExt); i++) {
375 if (strcasecmp(ext.string(), kNoCompressExt[i]) == 0)
379 const android::Vector<const char*>& others(bundle->getNoCompressExtensions());
380 for (i = 0; i < (int)others.size(); i++) {
381 const char* str = others[i];
382 int pos = pathName.length() - strlen(str);
386 const char* path = pathName.string();
387 if (strcasecmp(path + pos, str) == 0) {
395 bool endsWith(const char* haystack, const char* needle)
397 size_t a = strlen(haystack);
398 size_t b = strlen(needle);
399 if (a < b) return false;
400 return strcasecmp(haystack+(a-b), needle) == 0;
403 ssize_t processJarFile(ZipFile* jar, ZipFile* out)
405 size_t N = jar->getNumEntries();
407 for (size_t i=0; i<N; i++) {
408 ZipEntry* entry = jar->getEntryByIndex(i);
409 const char* storageName = entry->getFileName();
410 if (endsWith(storageName, ".class")) {
411 int compressionMethod = entry->getCompressionMethod();
412 size_t size = entry->getUncompressedLen();
413 const void* data = jar->uncompress(entry);
415 fprintf(stderr, "ERROR: unable to uncompress entry '%s'\n",
419 out->add(data, size, storageName, compressionMethod, NULL);
427 ssize_t processJarFiles(Bundle* bundle, ZipFile* zip)
431 const android::Vector<const char*>& jars = bundle->getJarFiles();
433 size_t N = jars.size();
434 for (size_t i=0; i<N; i++) {
436 err = jar.open(jars[i], ZipFile::kOpenReadOnly);
438 fprintf(stderr, "ERROR: unable to open '%s' as a zip file: %d\n",
442 err += processJarFile(&jar, zip);
444 fprintf(stderr, "ERROR: unable to process '%s'\n", jars[i]);