尝试使用.apk文件的自动更新代码时,找不到文件异常

时间:2012-10-29 06:28:37

标签: android

我正在使用commonsware示例应用程序来自动更新我的应用程序,它将被放置在服务器上。 但它让我除了ClassNotFound

logcat的

10-29 12:08:45.164: E/CWAC-Update(361): Exception in applying update
10-29 12:08:45.164: E/CWAC-Update(361): java.io.FileNotFoundException: /.CWAC-Update/update.apk (No such file or directory)
10-29 12:08:45.164: E/CWAC-Update(361):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
10-29 12:08:45.164: E/CWAC-Update(361):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
10-29 12:08:45.164: E/CWAC-Update(361):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
10-29 12:08:45.164: E/CWAC-Update(361):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
10-29 12:08:45.164: E/CWAC-Update(361):     at com.commonsware.cwac.updater.SimpleHttpDownloadStrategy.openDownloadFile(SimpleHttpDownloadStrategy.java:92)
10-29 12:08:45.164: E/CWAC-Update(361):     at com.commonsware.cwac.updater.SimpleHttpDownloadStrategy.downloadAPK(SimpleHttpDownloadStrategy.java:48)
10-29 12:08:45.164: E/CWAC-Update(361):     at com.commonsware.cwac.updater.UpdateService.downloadAndInstall(UpdateService.java:70)
10-29 12:08:45.164: E/CWAC-Update(361):     at com.commonsware.cwac.updater.UpdateService.doWakefulWork(UpdateService.java:36)
10-29 12:08:45.164: E/CWAC-Update(361):     at com.commonsware.cwac.wakeful.WakefulIntentService.onHandleIntent(WakefulIntentService.java:103)
10-29 12:08:45.164: E/CWAC-Update(361):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
10-29 12:08:45.164: E/CWAC-Update(361):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 12:08:45.164: E/CWAC-Update(361):     at android.os.Looper.loop(Looper.java:123)
10-29 12:08:45.164: E/CWAC-Update(361):     at android.os.HandlerThread.run(HandlerThread.java:60)
10-29 12:17:39.586: E/CWAC-Update(436):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)

这是代码

/***
Copyright (c) 2012 CommonsWare, LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package com.example.medecure;

import android.app.Activity;
import android.app.Notification;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.commonsware.cwac.updater.ConfirmationStrategy;
import com.commonsware.cwac.updater.DownloadStrategy;
import com.commonsware.cwac.updater.InternalHttpDownloadStrategy;
import com.commonsware.cwac.updater.NotificationConfirmationStrategy;
import com.commonsware.cwac.updater.SimpleHttpDownloadStrategy;
import com.commonsware.cwac.updater.SimpleHttpVersionCheckStrategy;
import com.commonsware.cwac.updater.UpdateRequest;
import com.commonsware.cwac.updater.VersionCheckStrategy;

public class UpdaterDemoActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView versionCodeLabel=(TextView)findViewById(R.id.versionCode);

    try {
      int currentVersionCode=
          getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

      versionCodeLabel.setText(String.valueOf(currentVersionCode));
    }
    catch (Exception e) {
      Log.e("UpdaterDemoActivity", "An impossible exception", e);
    }

    UpdateRequest.Builder builder=new UpdateRequest.Builder(this);
    builder.setVersionCheckStrategy(buildVersionCheckStrategy())
           .setPreDownloadConfirmationStrategy(buildPreDownloadConfirmationStrategy())
           .setDownloadStrategy(buildDownloadStrategy())
           .setPreInstallConfirmationStrategy(buildPreInstallConfirmationStrategy())
           .execute();
  }

  VersionCheckStrategy buildVersionCheckStrategy() {
    return(new SimpleHttpVersionCheckStrategy(
               "http://www.medecure.com/v2/android/update.json"));
                                           //   "http://misc.commonsware.com/update.json"));
  }

  @SuppressWarnings("deprecation")
ConfirmationStrategy buildPreDownloadConfirmationStrategy() {
    // return(new ImmediateConfirmationStrategy());
    Notification n=
        new Notification(android.R.drawable.stat_notify_chat,
                         "Update availalble", System.currentTimeMillis());

    n.setLatestEventInfo(this, "Update Available",
                         "Click to download the update!", null);
    n.flags|=Notification.FLAG_AUTO_CANCEL;

    return(new NotificationConfirmationStrategy(n));
  }

  DownloadStrategy buildDownloadStrategy() {
    if (Build.VERSION.SDK_INT>=11) {
      return(new InternalHttpDownloadStrategy());
    }

    return(new SimpleHttpDownloadStrategy());
  }

  @SuppressWarnings("deprecation")
ConfirmationStrategy buildPreInstallConfirmationStrategy() {
//    return(new ImmediateConfirmationStrategy());
    Notification n=
        new Notification(android.R.drawable.stat_notify_chat,
                         "Update ready to install", System.currentTimeMillis());

    n.setLatestEventInfo(this, "Update Ready to Install",
                         "Click to install the update!", null);
    n.flags|=Notification.FLAG_AUTO_CANCEL;

    return(new NotificationConfirmationStrategy(n));
  }
}

2 个答案:

答案 0 :(得分:1)

有两种方法可以为Android包含jar库:

  1. 将库放在项目根目录的libs/文件夹中。
  2. 在构建路径中包含库。如果选择此路径,则还需要导出库(在Eclipse中的Order and Exports下)。

答案 1 :(得分:0)

检查您的构建路径。库可能不在您的构建路径中。错误清楚地表明它无法找到班级。

om.commonsware.cwac.updater.UpdateRequest