-->

Android: Application for different requirements an

2019-09-17 15:02发布

问题:

Using build variant in Android we can support different flavors. I need to develop an application where I am supporting different clients. Each client needs are a little different.

However, the basic data, network call class etc are same for all clients. How can I ensure partial code of my application remains same for all flavors?

This will help in maintaining one repository for all common classes.

回答1:

You need to understand how build variant works.

Each client needs are a little different is a vague statement

Imagine you have an application which has different screen's for different countries. But major functionalities are the same.

Now using build variants you can make different flavors

1) For country one : This will have screens designed specific to country one

2) For country two : This will have screens designed specific to country two

3) Common part : All the common business logic can be put under your common package

While project is build, the common part is considered and specific flavor too becomes part of flavorXX.apk

productFlavors {

        employee {
            applicationId "com.myapp.employee"
        }
        driver {
            applicationId "com.myapp.driver"
        }
        asset {
            applicationId "com.myapp.asset"
        }
        vehicle {
            applicationId "com.myapp.vehicle"
        }
    }
    sourceSets {
        asset {
            manifest.srcFile 'src/asset/AndroidManifest.xml'
        }
        driver {
            manifest.srcFile 'src/driver/AndroidManifest.xml'
        }
        employee {
            manifest.srcFile 'src/employee/AndroidManifest.xml'
        }
        vehicle {
            manifest.srcFile 'src/vehicle/AndroidManifest.xml'
        }
    }

In the above example , I am having different flavors of same application. Inorder to split accordingly you need to understand which part of your app goes into specific flavor and which can be kept common. Go through below links for more details.

Understanding Product Flavors reference link 1

Understanding Product Flavors reference link 2