All Products
Search
Document Center

Introduction

Last Updated: Apr 01, 2021

The component-based framework refers to the framework in which mPaaS will divide an app into one or more Bundle projects and a Portal project based on Open Service Gateway Initiative (OSGi) technology. mPaaS manages the life cycle and dependencies of each Bundle project, and uses the Portal project to merge all Bundle project packages into a runnable .apk package.

The mPaaS framework is suitable for teams to develop apps collaboratively, and the framework includes functions such as component initialization and embedding, so that you can easily access mPaaS components.

Bundle project

A traditional native project consists of a main module or a main module and several sub-modules. An mPaaS Bundle project generally consists of a main module named app and several sub-modules.

For example, in Alipay, a Bundle generally consists of a main module named app and the following three sub-modules:

  • api: pure code API, the definition of API interface.
  • biz: the implementation of API interface operation.
  • ui: such as activity, custom view.
Important: there is at least one sub-module named api. If there is no sub-module, the API package of the Bundle cannot be packed. And the Bundle cannot be relied on by other Bundles.

After you read this topic, you will learn about the Bundle project from the following aspects:

Bundle is different from traditional engineering

Bundle is essentially a native project. Compared to a native project, a Bundle project has an additional Apply plug-in of mPaaS in the build.gradle of project**, main Module**, and sub-module. The specific differences are described as follows:

Project root directory build.gradle

In build.gradle in the project root directory, the dependency on the mPaaS plug-in is added:

Important: due to the iteration of functions, the plug-in version may continue to increase.

  1. classpath 'com.alipay.android:android-gradle-plugin:3.0.0.9.13'

image

build.gradle of the main module

In build.gradle of the main module, a declaration of mPaaS bundle Apply plug-in is added. This indicates that the project is a Bundle project. The Bundle configuration is as follows:

  1. apply plugin: 'com.alipay.bundle'

The following configuration has been added to the main module build.gradle:image

Where:

  • version: the version of the Bundle.
  • group: the groupid of the Bundle.
  • exportPackages: Describes which package names all the classes of the current Bundle project are under. The package names can be a collection. For non-statically linked Bundles, you must enter exportPackages, otherwise there will be a problem that the class cannot be loaded. For example, if all the codes are under com.alipay.demo and com.alipay.bundle, then you can write com.alipay or com.alipay.demo, com.alipay.bundle in exportPackages. The package name can neither be too long nor too short.
  • initLevel: The time to load the Bundle when the framework starts. The timing range is 0-100. The smaller the number is, the earlier the loading occurs. Among them, 11110000 means loading during use, that is, lazy loading.
  • packageId: Describes the ID of the current Bundle resource, which is needed for aapt packing. Due to the multi-Bundle architecture, the packageId of each Bundle must be unique and cannot be the same as the packageId of other Bundles. The packageId currently used by mPaaS is as follows:
Bundle packageId
com.alipay.android.phone.thirdparty:androidsupportrecyclerview-build 28
com.alipay.android.phone.mobilesdk:framework-build 30
com.alipay.android.phone.rome:pushservice-build 35
com.alipay.android.phone.sync:syncservice-build 38
com.alipay.android.phone.wallet:nebulabiz-build 41
com.alipay.android.phone.mobilecommon:share-build 42
com.alipay.android.phone.wallet:nebulacore-build 66
com.alipay.android.mpaas:scan-build 72
com.alipay.android.phone.wallet:nebula-build 76
com.alipay.android.phone.securitycommon:aliupgrade-build 77

Add the following dependencies on mPaaS in dependencies:

  1. dependencies {
  2. compile project(":api")
  3. apt 'com.alipay.android.tools:androidannotations:2.7.1@jar'
  4. //mPaaS dependencies
  5. provided 'com.alipay.android.phone.thirdparty:fastjson-api:1.1.45@jar'
  6. provided 'com.alipay.android.phone.thirdparty:androidsupport-api:13.23@jar'
  7. }

build.gradle of the sub-module

In build.gradle of the sub-module, a declaration of mPaaS Apply plug-in is added. This indicates that the project is a sub-module project of the Bundle, and the API package of this Bundle will eventually be packed.

  1. apply plugin: 'com.alipay.library'

Add the following dependencies on mPaaS in dependencies:

  1. dependencies {
  2. apt 'com.alipay.android.tools:androidannotations:2.7.1@jar'
  3. //mPaaS dependencies
  4. provided "com.alipay.android.phone.thirdparty:utdid-api:1.0.3@jar"
  5. provided "com.alipay.android.phone.mobilesdk:framework-api:2.1.1@jar"
  6. }

Bundle properties

The design concept of the Bundle property in this framework originates from the OSGi Bundle. But this design is more concise and lighter than the OSGi Bundle.

The following table lists the Bundle properties and descriptions:

Property Description
Bundle-Name The Bundle name is from the group in the build.gradle file and the name </>defined in settings.gradle.
Bundle-version Bundle version is from version in the build.gradle file.
Init-Level The time to load the Bundle comes from the properties: init.level defined in the build.gradle file.
Package-Id The packageid of the Bundle resource comes from the properties defined in the build.gradle file.
Contains-Dex Whether to include dex. This will automatically be determined by the compiler plug-in.
Contains-Res Whether to include resources. This will automatically be determined by the compiler plug-in.
Native-Library The compiler plug-in can automatically determine the included so files.
Component-Name From the Activity,Service,BroadcastReceiver, and ContentProvider defined in the AndroidManifest.xml file.
exportPackages For the name of the package where all the classes of this Bundle are located, see the build.gradle of the main module.

Bundle interface package

A Bundle may contain multiple sub-modules, such as biz, api, UI. When you compile and pack the Bundle, each sub-module will generate an interface package in the format of .jar. Among these packages, only the API interface packages can be used by other Bundles.

At the same time, a Bundle project package is also generated during the packing. All sub-modules are contained in this project package. The project package can be used by the Portal project. The project package is compiled in the Portal and the .apk package is generated finally.

  • The interface package packed by the sub-module of Bundle, only provides customized java/kotlin interface classes, excludes the resource under res directory. And these interface packages can only packed from the api modules.
  • Each Bundle project directly depends on each other through the API package of the Bundle. You need to configure the dependency API in the dependency in the build.gradle of the Bundle. For example, Bundle A depends on the bapi sub-module of Bundle B. Then you need to configure the dependency on bapi in the dependency in the build.gradle of the corresponding sub-module of Bundle A.

    1. provided "com.alipay.android.phone:bundleB:1.0.1:bapi@jar"
  • The groupId:artifactid:version:classifier involved in the dependency corresponds to the group, name, version, and sub-module names declared in the Bundle.
  • By default, the name of Bundle is the folder name of the main module. The Bundle name can be modified in settings.gradle, as shown in the following code, where app is the project name of the main module:

    1. include ':api', ':xxxx-build'
    2. project(':xxxx-build').projectDir = new File('app')

Bundle project package

  • The .jar package packed by the whole Bundle project, which is an.apk file but the suffix is .jar, for example, framework-build.jar.
  • To rely on Bundle in Portal, you need to declare the dependency on Bundle in dependency in build.gradle of the main module of Portal, which is shown as follows:

    1. dependencies {
    2. bundle "com.alipay.android.phone.mobilesdk:framework-build:version@jar"
    3. manifest "com.alipay.android.phone.mobilesdk:framework-build:version:AndroidManifest@xml"
    4. }
  • There are two types of Bundle packages: debug package and release package. When Portal depends on the debug package of Bundle, you need to add :raw to the debug package.
    • When Portal depends on the debug package of the Bundle, use
      bundle "com.alipay.android.phone.mobilesdk:framework-build:version:raw@jar"
    • When Portal depends on the release package of the Bundle, use
      bundle "com.alipay.android.phone.mobilesdk:framework-build:version@jar"

Note:

  • When you pack the Portal package, you need to make sure the following items:
    • Which Bundles are to be packed in the main dex of the app. Static link. Bundle with ContentProvider must be placed in a static link.
    • Which are dynamically loaded. If the app is not big, it is recommended to be packed in the main dex.
  • If you want to pack the Bundle code into the main dex, you need to configure the current Bundle in the slinks file of Portal. The configuration content is: groupId-artifactId. If the configuration content ends with -build, you need to remove -build. For example, if the groupId is com.mpaas.group and the artifactId is testBundle-build, you need to add a line in the slinks file:com.mpaas.group-testBundle.
  • Static link: Pack the Bundle code into classes.dex in apk, or into classes1.dex or classes2.dex. Then you can load the classes in the Bundle when the project starts.
  • Dynamic loading: Store the Bundle code in lib/armeabi. When you use a Bundle class, the framework automatically creates a BundleClassLoader for loading. In this case, you need to configure exportPackages of the Bundle.

Portal project

The Portal project merges all the Bundle project packages into a runnable .apk package.

The difference between Portal and traditional engineering

The difference between Portal and traditional development projects is inbuild.gradle:

Project root directory build.gradle

As shown in the following figure, the classpath has an additional com.alipay.android:android-gradle-plugin:2.1.3.2.7 plug-in:

Important: due to the iteration of functions, the plug-in version may continue to increase.

image

This plug-in contains the Portal plug-in, which can merge the Bundles during the packing process.

  • Merged .jar of the Bundle
  • Merged AndroidManifest of the Bundle

Main module directory build.gradle

The declaration of mPaaS Apply Portal plug-in is added, which indicates that the project is a Portal project. The Portal configuration is as follows:

  1. apply plugin: 'com.alipay.portal'

At the same time, add the corresponding dependency on Bundle in dependencies. The statements in dependencies are the declarations of Bundle and manifest, which are used to indicate which Bundles or manifests the Portal depends on:

image

Notes:

  • Usually no code is written under Portal.
  • The following types of resources, such as style, drawable, and string, used in the Bundle project must be placed in the Portal project. Otherwise, the resources will not be found during compilation or runtime:
    • Resources used in AndroidManifest.xml.
    • The resources passed to NotificaionManager for use.
    • Resources used by the getResources().getIdentifier() method.
    • If there are the preceding three situations in the referenced third-party aar package, you also need to decompress aar and copy the corresponding resources into the Portal project.

Project dependencies

An app based on the mPaaS framework includes one or more Bundles and a Portal. An app can have only one Portal project, but there can be multiple Bundle projects.

Through the mPaaS plug-in, the Portal project merges all the Bundle project packages into a runnable .apk package. After the merge, the plug-in deploys the Bundle project to the specified warehouse address. The warehouse address is defined in build.gradle in the main module of Bundle, as shown in the following code:

  1. uploadArchives {
  2. repositories {
  3. mavenLocal()
  4. }
  5. }

The warehouse address is uploaded to the local ~/.m2 warehouse address. You can also add a custom warehouse address as follows:

  1. mavenDeployer {
  2. mavenLocal()
  3. repository(url: "${repository_url}") {
  4. authentication(userName: 'userName', password: 'userName_pwd')
  5. }
  6. snapshotRepository(url: "${repository_url}") {
  7. authentication(userName: 'userName', password: 'userName_pwd')
  8. }
  9. }

After the upload is completed, the Bundle is stored in the designated warehouse in the form of groupid:artifactid:version:classifier@type. So, if you declare dependency in the build.gradle</>of the outermost main module of Portal, you can specify dependencies for each Bundle, as shown in the following code:

  1. dependencies {
  2. bundle 'com.alipay.android.phone.mobilesdk:quinox-build:2.2.1.161221190158:nolog@jar'
  3. manifest 'com.alipay.android.phone.mobilesdk:quinox-build:2.2.1.161221190158:AndroidManifest@xml'
  4. }

In addition, the interdependence between Bundle projects also needs to declare the warehouse dependency address in the outermost build.gradle of the Bundle.

Note: The username and password in the following configuration are not the logon user name and password of the console. You must Submit a ticket to get these two values.On this page:

  • mavenLocal() describes the dependent local warehouse address.
  • maven{} declares the address of the remote warehouse that it depends on.
  1. allprojects {
  2. repositories {
  3. mavenLocal()
  4. mavenCentral()
  5. maven {
  6. credentials {
  7. username "{username}"
  8. password "{password}"
  9. }
  10. url "http://mvn.cloud.alipay.com/nexus/content/repositories/releases/"
  11. }
  12. }
  13. }

Bundle compilation and packing results

After you compile and pack the package with the mPaaS plug-in, a Bundle will generate a project package, which is a .jar package. For more information, see Bundle Project > Bundle Project Package and Bundle Project > Bundle interface package.

The project package will be published to the designated warehouse in the form of groupid:artifactid:version:classifier@type. The release warehouse address is defined in build.gradle in the Bundle main module as follows:

  1. uploadArchives {
  2. repositories {
  3. mavenLocal()
  4. }
  5. }

The preceding configuration specifies that the release warehouse is a local Maven warehouse (mavenLocal). If you need to modify the address of the local Maven warehouse (default~/.m2) or add a release warehouse, see Configure release warehouse.

Add Bundle dependencies

You can add Bundle dependencies to the Portal, or you can add dependencies to other Bundles. You only need to:

  1. Declare the dependent warehouse address in build.gradle at the outermost layer of Portal or Bundle. The dependent warehouse needs to correspond to the preceding Bundle release warehouse. For the configuration method of the dependent warehouse, see Configure dependent warehouse.
  2. Declaredependencies in build.gradle of Portal or the main module of Bundle. An example of adding Bundle (quinox) dependency is as follows:
  1. dependencies {
  2. bundle 'com.alipay.android.phone.mobilesdk:quinox-build:2.2.1.161221190158:nolog@jar'
  3. manifest 'com.alipay.android.phone.mobilesdk:quinox-build:2.2.1.161221190158:AndroidManifest@xml'
  4. }