need to check if file is a valid APK

若如初见. 提交于 2020-02-25 05:24:59

问题


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 contents.

Thanks in advance...


回答1:


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.




回答2:


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.




回答3:


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
}


来源:https://stackoverflow.com/questions/4749559/need-to-check-if-file-is-a-valid-apk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!