I am working on a web page that will allow the end-user to upload .apk files. I need a library/component to check that the files uploaded are valid .apk files. I am looking for something more that the ability to just view the conten开发者_运维百科ts.
Thanks in advance...
How deep a validation do you want?
APK is a renamed ZIP archive. So the first line of validation would be trying to open it as a zip. Then you can check if the Android manifest is there and looks right. Finally, you could try loading Java classes from the files... but you'll need a copy of the Dalvik VM for that, since the Android bytecode format is not that of regular Java.
How exactly do you work with ZIPs and XMLs depends on your back-end platform, which you probably should've specified in question tags.
If you need to do this programmatically, I suggest you to try with zip --check filename
or dex2jar filename
. In my case it helped to discard invalid apk.
It is a bit slow for huge number of files, but for single file, this is the fastest and best method possible.
PackageInfo packageInfo = context.getPackageManager().getPackageArchiveInfo(file.getAbsolutePath(), 0);
if(packageInfo != null){
// you can be sure that it is a valid apk
} else {
// not a valid apk
}
精彩评论