primaree

A simple library for safe initialization of multi-process Android applications

License

License

GroupId

GroupId

com.dpforge
ArtifactId

ArtifactId

primaree
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

aar
Description

Description

primaree
A simple library for safe initialization of multi-process Android applications
Project URL

Project URL

https://github.com/int02h/primaree
Source Code Management

Source Code Management

https://github.com/int02h/primaree

Download primaree

How to add to project

<!-- https://jarcasting.com/artifacts/com.dpforge/primaree/ -->
<dependency>
    <groupId>com.dpforge</groupId>
    <artifactId>primaree</artifactId>
    <version>1.2.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.dpforge/primaree/
implementation 'com.dpforge:primaree:1.2.0'
// https://jarcasting.com/artifacts/com.dpforge/primaree/
implementation ("com.dpforge:primaree:1.2.0")
'com.dpforge:primaree:aar:1.2.0'
<dependency org="com.dpforge" name="primaree" rev="1.2.0">
  <artifact name="primaree" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.dpforge', module='primaree', version='1.2.0')
)
libraryDependencies += "com.dpforge" % "primaree" % "1.2.0"
[com.dpforge/primaree "1.2.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-android-extensions-runtime jar 1.3.61
org.jetbrains.kotlin : kotlin-stdlib-jdk7 jar 1.3.61
androidx.appcompat » appcompat jar 1.1.0
androidx.core » core-ktx jar 1.2.0

Project Modules

There are no modules declared in this project.

Primaree

Build Status Latest release

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.

Versions

Version
1.2.0
1.1.0
1.0.0