尝试安装未签名的远程 .apk 文件时出现不一致的解析错误

尝试安装未签名的远程 .apk 文件时出现不一致的解析错误

问题描述:

我目前正在开发一款不会在 Play 商店上架的应用,需要某种更新程序.

I'm currently working on an app that will not go on the play store and needed some sort of update routine.

我让它工作了,所以当用户按下按钮时,应用程序会下载一个托管在我们的一台服务器上的 apk 文件.那部分工作正常.这是有趣的东西...

I got it working so when the user presses a button the app downloads an apk file hosted on one of our servers. That part works fine. Here's the interesting stuff...

当我尝试启动安装文件的意图时,出现以下解析错误:

When I try to start the intent to install the file, I get the following parse error:

解析错误解析包时出现问题"

"Parse Error There is a problem parsing the package"

无论在 android 4.0.3 版本上如何,每次都会发生此错误.这是日志猫:

This error happens everytime no matter what on android version 4.0.3. Here is the logcat:

10-03 18:36:05.212: D/asset(567): failed to open Zip archive            '/mnt/sdcard/download/AMWireless.apk'
10-03 18:36:05.272: W/PackageParser(567): Unable to read AndroidManifest.xml of     /mnt/sdcard/download/AMWireless.apk
10-03 18:36:05.272: W/PackageParser(567): java.io.FileNotFoundException:  AndroidManifest.xml
10-03 18:36:05.272: W/PackageParser(567):   at android.content.res.AssetManager.openXmlAssetNative(Native Method)
10-03 18:36:05.272: W/PackageParser(567):   at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
10-03 18:36:05.272: W/PackageParser(567):   at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:455)
10-03 18:36:05.272: W/PackageParser(567):   at android.content.pm.PackageParser.parsePackage(PackageParser.java:425)
10-03 18:36:05.272: W/PackageParser(567):   at com.android.packageinstaller.PackageUtil.getPackageInfo(PackageUtil.java:74)
10-03 18:36:05.272: W/PackageParser(567):   at com.android.packageinstaller.PackageInstallerActivity.onCreate(PackageInstallerActivity.java:277)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.Activity.performCreate(Activity.java:4465)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-03 18:36:05.272: W/PackageParser(567):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 18:36:05.272: W/PackageParser(567):   at android.os.Looper.loop(Looper.java:137)
10-03 18:36:05.272: W/PackageParser(567):   at android.app.ActivityThread.main(ActivityThread.java:4424)
10-03 18:36:05.272: W/PackageParser(567):   at java.lang.reflect.Method.invokeNative(Native Method)
10-03 18:36:05.272: W/PackageParser(567):   at java.lang.reflect.Method.invoke(Method.java:511)
10-03 18:36:05.272: W/PackageParser(567):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-03 18:36:05.272: W/PackageParser(567):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-03 18:36:05.272: W/PackageParser(567):   at dalvik.system.NativeStart.main(Native Method)
10-03 18:36:05.272: W/PackageInstaller(567): Parse error when parsing manifest. Discontinuing installation

然而,我的更新程序适用于运行 2.3.5 版的 droid x2.安装应用的意图非常好,一旦他们确认安装的应用权限.

Yet, my updater works on my droid x2 running version 2.3.5. The intent to install the app goes great and once they confirm the app permissions it installs.

我能够在 2.3.5 版上重现解析错误,方法如下:

I was able to reproduce the parse error on version 2.3.5 here is how:

我通常只是通过 eclipse 安装应用程序,运行它,但是如果我在包资源管理器窗口中右键单击包,使用 android 工具导出未签名的 .apk,并尝试手动安装包,它不会安装.

I normally just install the app through eclipse, by running it, but if I right-clicked on the package in the package explorer window used android tools to export an unsigned .apk, and tried to install the package manually it would not install.

如果我使用项目 bin 文件夹中的 .apk,它可以正常安装并在 2.3.5 上正常更新

If I use the .apk's that are in the project bin folder, it installs fine and updates fine on 2.3.5

这是我使用的 asynctask 类:

Here is the asynctask class I use:

public class UpdateWireless extends AsyncTask<String,Void,String>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected String doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/";
File oldFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/AMWireless.apk");
if(oldFile.exists())
{
oldFile.delete();
}
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "AMWireless.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e){
}
return "File Saved";
}
protected void onPostExecute(String result) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new      File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/download/"+"AMWireless.apk")), "application/vnd.android.package-archive");
startActivity(intent); 
}

}

是的,我需要在 catch 块中做一些事情,但我们会到达那里...

and yes I need to but something in the catch block but we will get there...

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wireless.wmaa"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.DELETE_PACKAGES"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
                                     >
        <activity
            android:label="@string/app_name"
            android:name=".SelectServer" 
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" /> 
             </intent-filter>"
            </activity>
        <activity
            android:name=".UpdateApp"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >  
            <intent-filter>
            </intent-filter>
        </activity>

        <activity
            android:name=".WirelessActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar"
            android:screenOrientation="landscape" >  
            <intent-filter>
            </intent-filter>
        </activity>
    </application>


</manifest>

我从 Eclipse 中导出东西错了吗?这是一个明显的问题吗?我这样做对吗?

Am I Exporting things from Eclipse wrong? Is it a manifest issue? Am I going about this right?

编辑 1:好吧,将其缩小到 android 4.0.3 上这一行的问题:

EDIT 1: Alright narrowed it down a little more to a problem with this line on the android 4.0.3:

InputStream is = c.getInputStream();

这一行抛出一个找不到文件的异常,因为它在我的服务器上看不到该文件,而且从不下载.Android 2.3.5 看到文件没有异常就下载安装好了...这是什么问题?

This line throws a file not found exception, as in it can't see the file on my server and it is never downloads. Android 2.3.5 sees the file has no exceptions and downloads the and installs fine... Whats the problem here?

编辑 2:已解决我本来是想把文件托管在一个由IIS提供服务的站点上,老版本的android不在意,但是新版本就看不到文件了.一旦我将 .apk 移动到运行 apache 的 Web 服务器,我就没有任何问题.因此,IIS 或我对它的配置有些可疑....

EDIT 2: RESOLVED I was originally trying to host the file on a site that was served by IIS, and the old version of android didn't care, but the new version would not see the file. Once I moved the .apk to a web server running apache I had no issues. So, something is fishy with IIS or maybe my configuration of it....

回答了我自己的问题.我托管文件的位置有问题.IIS 和 android 不会很好地一起玩:( 解决方案:将 .apk 文件移动到 apache 服务器.

Answered my own question. Was a problem with where I was hosting the file. IIS and android would not play nice together :( solution: move .apk file to apache server.