Primaree 一个用于安全初始化多进程Android应用程序的简单库

网友投稿 613 2022-10-21

Primaree 一个用于安全初始化多进程Android应用程序的简单库

Primaree 一个用于安全初始化多进程Android应用程序的简单库

Primaree

Primaree - a simple library for safe initialization of multi-process Android applications.

But I have only one process

You think so. But if you take a look at your merged AndroidManifest.xml you may notice that there are some services, content providers or other Android components that specify android:process - so they work in different processes.

What's the problem?

The problem is that your Application.onCreate() method is being called as many times as many processes your application has. It means that you can accidentally initialize your code twice, thrice or even more times. This may lead to deadlocks or concurrent access to entities that are not supposed to have concurrent access. For example, SQLite.

What Primaree actually does?

Just look at the following code:

class App : PrimareeApplication() { override fun onPrimaryCreate() { // ... } override fun onSecondaryCreate(name: String) { // ... } override fun onPrimaryConfigurationChanged() { // ... } override fun onSecondaryConfigurationChanged(name: String) { // ... }}

Primaree provides abstract class PrimareeApplication you can inherit from. Its main purpose is to separate every considerable call in Application (such as onCreate or onConfigurationChanged) into two calls like onPrimaryCreate and onSecondaryCreate. These methods are called in the primary process of your application and every secondary process respectively. All secondary calls also receive the name of the secondary process as their argument in the same format it appears in AndroidManifest.xml.

It worth to be mentioned that onCreate and other considerable calls are marked final so you forced to split your app initialization for primary and secondary processes. It makes you never forget that the application is multi-process.

What if my application class inherits another base class?

In this situation, Primaree is still able to help. It has few handy extensions for the Application class. You may use them as the following:

class App : Application() { override fun onCreate() { super.onCreate() runIfPrimaryProcess { // do something in primary process } if (isPrimaryProcess) { // do something in primary process } else { // do something in secondary process } } }

Download

Add the following dependency to your build.gradle script:

dependencies { implementation 'com.dpforge:primaree:1.2.0'}

License

Copyright (c) 2020 Daniil Popov

Licensed under the MIT License.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:最短路径--昂贵的聘礼
下一篇:Feign调用服务时丢失Cookie和Header信息的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~