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.
Post my solution here, hope it will help:
Android Studio will automatically decompress *.gz
file and remove .gz
extension while pack APK file.
Referring to aapt source code, We found it was caused by Android aapt (Android Asset Packaging Tool) default pack strategy, we cannot even add filter options to exclude .gz
to prevent deompressing!
https://cs.android.com/android/platform/superproject/+/master:frameworks/base/tools/aapt/Package.cpp
Change project source code to adpat packed Android APK contents:
//old
YourRequestMethod("~/path/to/file.gz", (gzipContent)=>{
const unzipContent = YourUnzipMethod(gzipContent);
console.log(unzipContent);
})
//new
YourRequestMethod("~/path/to/file", (unzipContent)=>{
console.log(unzipContent);
})
Force prevent Android Studio from decompressing *.gz file!!!
//Modify ~/YourAndroidProject/build.gradle file:
android {
...
//Add aapt options
aaptOptions {
noCompress ['.keep_gz']
}
//Add apk pack hooks
applicationVariants.all { variant ->
variant.mergeAssetsProvider.configure {
doFirst {
renameFiles("${projectDir}/src/main/assets", ".gz", ".keep_gz")
println "---> Rename src files: *.gz to *.keep_gz at: "+projectDir;
}
doLast {
def assetsDir = "${buildDir}/intermediates/assets/${variant.dirName}"
renameFiles(assetsDir, ".keep_gz", ".gz")
println "---> Rename APK files before pack: *.keep_gz to *.gz at: "+assetsDir;
renameFiles("${projectDir}/src/main/assets", ".keep_gz", ".gz")
println "---> Rename src files: *.keep_gz to *.gz at: "+projectDir;
}
}
}
}
//Add file rename method
def renameFiles(String dirPath, String fromExt, String toExt) {
fileTree(dir: dirPath, include: "**/*" + fromExt).each { file ->
def renamedFile = new File(file.parent, file.name.replace(fromExt, toExt))
file.renameTo(renamedFile)
}
}