Android - Library components used across different Applications or Product Flavors

    Android framework supports applications or different product flavors of the same application (like free and pro) to use the same library dependencies. The library could expose components like Activities, Services, UI widgets etc. These java based libraries are linked into these multiple applications and are not shared unlike native libraries which are loaded once into memory and shared across processes.

    A typical application with product flavors might be setup to change the package name during the compilation process,

 android {
    ...
    productFlavors {
        pro {
            applicationId = "com.sample.testapp.pro"
        }
        free {
            applicationId = "com.sample.testapp.free"
        }
    }
 }

 dependencies {
    ...
    compile project(":LibraryModule")
}

    Now, when the app is run in proDebug and feeDebug, multiple applications are installed on the device. These applications are differentiated by their package name and any attempt to launch a library component like the following,

        Intent intent = new Intent(this, com.sample.library.LibraryActivity.class);
        startActivity( intent );

 would be picked from the library packaged into the application. This is how Android guarantees the uniqueness of a component by considering both the package name and the component name. Now, when both product flavor apps are launched, Activity Manager uses package and component name to keep track of the active components,

- ActivityRecord{42bfca20 u0 com.sample.testapp.pro/com.sample.library.LibraryActivity t1156}
- ActivityRecord{4272fd38 u0 com.sample.testapp.free/com.sample.library.LibraryActivity t1155}

No comments: