8

When I add a GZIP-ed file to my Android project's assets, the ".gz" extension is stripped when the project is packaged. (So, for instance, "foo.gz" in my assets folder needs to be accessed in code using getAssets().open("foo").) This doesn't seem to happen with other extensions (e.g., ".html") that I'm using. The asset is still GZIP-ed (I have to wrap the input stream in a GZIPInputStream to read it).

Is this standard behavior or a bug? If it's standard, is there any documentation about which extensions are stripped and which are preserved?

EDIT: I misstated things sightly. I'm experiencing this problem with the Eclipse plug-in. I haven't tried running aapt directly to see if the problem is with the tool itself or with how the plug-in is using it.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 3
    The Android asset package tool (aapt) recognizes .gz files and adds them to the .zip without re-compressing them (i.e. it strips off the gzip header and drops them into a "compressed" entry in the zip archive; this is a build time optimization). The behavior you're describing sounds different though. – fadden Jan 14 '11 at 23:48
  • Stripping the .gz extension seems particularly programmer-unfriendly. Also, the .apk archive has the file in the same place in the assets folder as it would be with any other name. – Ted Hopp Jan 30 '11 at 17:35
  • This seems to be deliberate (based on filename only) within aapt, Maybe the thought was that if it is gzipped .... then um confuse people? http://www.google.com/codesearch#uX1GffpyOZk/tools/aapt/Package.cpp&exact_package=git://android.git.kernel.org/platform/frameworks/base.git&q=%22Writing%20all%20files%22&type=cs&l=309 – Greg Giacovelli Aug 15 '11 at 05:30
  • for some strange reason, in my case .gz files have their extension stripped, but do not require `GZIPInputStream` to read them, just plain input stream. on the other hand, if I rename .gz files to .dat -- I have to use `GZIPInputStream` to uncompress them. – lenik Mar 30 '13 at 04:28
  • 1
    Here's a working link to the relevant AAPT source line: http://androidxref.com/4.4.4_r1/xref/frameworks/base/tools/aapt/Package.cpp#377 – plinehan Jul 23 '14 at 22:31
  • @plinehan - Thanks for that link. The relevant code, I think, starts at [line 329](http://androidxref.com/4.4.4_r1/xref/frameworks/base/tools/aapt/Package.cpp#329). It seems that only `.gz` extensions are stripped. This behavior (obviously intentional) makes no sense to me. – Ted Hopp Jul 24 '14 at 02:07

1 Answers1

0

Here How i solve it, just doing a cordova before build hook. https://gist.github.com/josx/fc76006e6d877b17fefd

#!/usr/bin/env node

/**
 * Lets clean up some files that conflicts with aapt.
 * https://osvaldojiang.com/p/137
 * https://github.com/driftyco/ionic/issues/4584
 * http://stackoverflow.com/questions/4666098/why-does-android-aapt-remove-gz-file-extension-of-assets
 * https://forum.ionicframework.com/t/android-build-failed-ionic-cordova-unable-to-add-asset-file-file-already-in-archive/41146
 */

var glob = require('glob');
var fs = require('fs');
var path = require('path');

var deleteFilesFromFolder = function(globExp) {
  // Find files
  glob(globExp, function(err,files) {
    if (err) throw err;
    files.forEach(function(item, index,array) {
      console.log(item + " found");
    });

    // Delete files
    files.forEach(function(item, index,array) {
      fs.unlink(item, function(err) {
        if (err) throw err;
          console.log(item + " deleted");
      });
    });
  });
};

var globExp = path.resolve(__dirname, '../../www/lib') + '/**/*.gz';
deleteFilesFromFolder(globExp);
josliber
  • 43,891
  • 12
  • 98
  • 133
josx
  • 15
  • 5
  • 1
    Correct me if I'm wrong, but that looks like it just deletes all the files ending in `.gz`. Why would I want to do that? – Ted Hopp Jan 14 '16 at 20:57