集成保活,需要注意点 - xiaoniudonghe2015/Android-Java-Code-Style GitHub Wiki

1.加双进程保活后,Firebase Remote Config调用出现异常的问题

错误Log

Caused by: java.lang.IllegalStateException:
      at com.google.firebase.FirebaseApp.getInstance (FirebaseApp.java)
      at com.google.firebase.FirebaseApp.getInstance (FirebaseApp.java)
      at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance (FirebaseRemoteConfig.java)
      at com.common.firebase.config.CommonRemoteConfig.<init> (CommonRemoteConfig.java)
      at com.common.firebase.config.CommonRemoteConfig.getInstance (CommonRemoteConfig.java)
      at com.common.firebase.config.CommonRemoteConfig.access$000 (CommonRemoteConfig.java)
      at com.messenger.util.Utils.isCallFlashSwitchEnabled (Utils.java)
      at com.messenger.util.Utils.isCallFlashEnabled (Utils.java)
      at com.callresult.CallResultService.start (CallResultService.java)
      at com.callresult.CallResultService.showCallResultUI (CallResultService.java)
      at com.callresult.CallResultService.registerPhoneStateReceiver (CallResultService.java)

原因

查看源码,发现完整的log应该是

throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + ProcessUtils.getMyProcessName() + ". Make sure to call FirebaseApp.initializeApp(Context) first.");

在该进程中没有调用初始化firebase导致的

解决办法

双进程Service里面有调用Remote Config的话,在之前调用

FirebaseApp.initializeApp(this);

确保调用之前已经初始化.

2.保活的Service建议不要有业务逻辑,会报Application Context的NPE

3.双进程关于Webview的crash

log

java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported

解决办法

Application中添加以下代码:

public void setWebViewPath(Context context) {
        //java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported
        //android 9.0开始不支持同时使用多个进程中具有相同数据目录的WebView, 针对这个问题,谷歌也给出了解决方案:在初始化的时候,需要为其它进程webView设置目录
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = Utils.getCurrentProcessName(context);
            if (!getPackageName().equals(processName)) {//判断不等于默认进程名称
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

注意再某些App上添加之后,在android10上运行直接crash

⚠️ **GitHub.com Fallback** ⚠️