获取 UpdateEngine 是在 AB OTA 应用程序中的空对象引用上

Getting UpdateEngine is on a null object reference in AB OTA application

I am working on Android_Q. Trying to do AB OTA update from Android application. Have gave necessary information to update engine.

E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.os.IUpdateEngine.bind(android.os.IUpdateEngineCallback)' on a null object reference

public class MainActivity extends Activity {
     private static final String TAG = "OTAupdate";
 private UpdateEngine mUpdateEngine;
 private final UpdateEngineCallbackImpl
        mUpdateEngineCallback = new UpdateEngineCallbackImpl();
float progress;
int status;
ProgressBar progressBar;
Button startButton, pauseButton, stopButton;
boolean paused;
Context mContext;
  private static String[] getInfo() {
    return new String[]{
            "FILE_HASH=/hcckhsjkchjhkjdhjk=",
            "FILE_SIZE="22112",
            "METADATA_HASH=wFVt2qsadadaddd",
            "METADATA_SIZE=3353"
    };
}
     @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton = (Button) findViewById(R.id.btn1);
    pauseButton = (Button) findViewById(R.id.btn2);
    stopButton = (Button) findViewById(R.id.btn3);     
     setupButtonListeners();
   UECallback cb = new UECallback();
    mUpdateEngine = new UpdateEngine();
}
class UpdateEngineCallbackImpl extends UpdateEngineCallback {
    @Override
    public void onStatusUpdate(int status, float percent) {           
    }
    @Override
    public void onPayloadApplicationComplete(int errorCode) {          
        Log.d(TAG,"onPayloadApplicationComplete");

    }
}

@Override
public void onStart() {
    super.onStart();

}

@Override
public void onRestart() {
    super.onRestart();
}


@Override
public void onPause() {
    super.onPause();
}

@Override
public void onStop() {
    super.onStop();
}

private class UECallback extends android.os.UpdateEngineCallback {
    public void onStatusUpdate(int aStatus, float aPercent) {
        progress = aPercent * 100;
        status = aStatus;
        progressBar.setProgress(Math.round(progress), true);
        Log.e(TAG, "current progress is" + (int) progress+"--status--"+aStatus);

    }

    public void onPayloadApplicationComplete(int errCode) {
        Log.e(TAG, "Payload application complete, error:" + Integer.toString(errCode));

        if (errCode == 0) {
            Log.e(TAG, "Installation succeeded!");
        }
    }
}

private void setupButtonListeners() {
    startButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e(TAG, "####### start clicked");
           mUpdateEngine.bind(new UpdateEngineCallback() {
                @Override
                public void onStatusUpdate(int i, float v) {

                }

                @Override
                public void onPayloadApplicationComplete(int i) {

                }
            });      
     mUpdateEngine.applyPayload("file:///data/ota_package/payload.bin",0, 22112,getInfo());

        }
    });

    pauseButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e(TAG, "###### pause clicked");
            if (paused) {
                paused = false;
                mUpdateEngine.resume();
                pauseButton.setText("resume");
                return;
            }

            paused = true;
            pauseButton.setText("pause");
            mUpdateEngine.suspend();
        }
    });

    stopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e(TAG, "###### stop clicked");
            mUpdateEngine.cancel();
            paused = false;

        }
    });

}

}

但出现以下错误:

  java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.os.IUpdateEngine.bind(android.os.IUpdateEngineCallback)' on a null object reference

完整错误日志:

(standard input):4838:05-13 09:50:48.563  3590  6403 I ActivityTaskManager: START u0 
{act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 
pkg=com.example.android.systemupdatersample 
cmp=com.example.android.systemupdatersample/.ui.MainActivity} from uid 1000
(standard input):4844:05-13 09:50:48.708  3590  3629 I ActivityManager: Start proc 
6998:com.example.android.systemupdatersample/u0a58 for activity 
{com.example.android.systemupdatersample/com.example.android.systemupdatersample.ui.MainActivity}
(standard input):4864:05-13 09:50:49.189  6998  6998 E AndroidRuntime: Process: 
com.example.android.systemupdatersample, PID: 6998
(standard input):4865:05-13 09:50:49.189  6998  6998 E AndroidRuntime: java.lang.RuntimeException: 
Unable to start activity 
   ComponentInfo
   {com.example.android.systemupdatersample/com.example.android.systemupdatersample.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean 
  android.os.IUpdateEngine.bind(android.os.IUpdateEngineCallback)' on a null object reference
  standard input):4881:05-13 09:50:49.189  6998  6998 E AndroidRuntime:  at 
  com.example.android.systemupdatersample.ui.MainActivity.onCreate(MainActivity.java:110)
  (standard input):4887:05-13 09:50:49.195  3590  6403 W ActivityTaskManager:   Force finishing activity 
  com.example.android.systemupdatersample/.ui.MainActivity
  (standard input):4903:05-13 09:50:49.290  3590  6914 I ActivityManager: Process 
  com.example.android.systemupdatersample (pid 6998) has died: vis+99 TOP
  (standard input):4912:05-13 09:50:49.697  3590  3622 W ActivityTaskManager: Activity top resumed state 
   loss timeout for ActivityRecord{462c162 u0 com.example.android.systemupdatersample/.ui.MainActivity t-1 f}

所以你的绑定失败了,这可能有几个原因:

  1. 您的 update_engine 服务不是 运行 Android。我发现 update_engine 服务似乎无法在某些模拟器上运行。要使用 adb shell 检查 update_engine 服务,您可以搜索服务:
ucm_imx8m_mini:/ ps -A | grep update
root          2917     1   23004   8812 SyS_epoll_wait      0 S update_engine

您还可以验证 update_engine_client 命令可以 connect/bind 到服务:

    ucm_imx8m_mini:/ update_engine_client --follow                                                                                                                                                
    [INFO:update_engine_client_android.cc(90)] onStatusUpdate(UPDATE_STATUS_IDLE (0), 0)
  1. 您的应用未签名为系统应用,因此无法访问该服务。

  2. 您应用的清单未指定系统 sharedUserID 属性,例如

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.horizonemb.simplesystemapp"
        android:sharedUserId="android.uid.system">
    ...
    </manifest>

如果 none 这些工作,请检查 Google 的 updater_sample 示例的 With the privileged system permissions 部分。