-->

How can I set different build type for each build

2019-07-13 03:16发布

问题:

I have a two flavor for my app. This two flavor use different baseurl. I have 4 url totally.

admintest

adminrelease

usertest

userrelease

android {
    ...
    defaultConfig {...}
    buildTypes {
        debug{   //baseurl for debug   }
        release{   //baseurl for release   }
    }
    // Specifies one flavor dimension.
    flavorDimensions "version"
    productFlavors {
        user {

        }
        admin {

        }
    }
}

I only set one url in debug and relase now and I cant find how can I set url for each flavor.

Thanks for any advice.

回答1:

You can see in below image i have flavors of my app

src >> main

is used as common for all.

src >> flavorVersionTwo

here i created separate strings.xml which will overwrite the main strings.xml for same string_name.

Example

lets suppose you have base_url string in src >> main >> strings.xml

when you are using (in my case) falvorVersionTwo.

if you added base_url string in src >> flavorVersionTwo >> strings.xml then it will overwrite the base_url written in main->strings.xml.

Note:

For more understanding you can Read this and this



回答2:

Do it like following code snippet:

android {
    ...
    defaultConfig {...}
    buildTypes {
        debug{   buildConfigField "String", "MY_URL", "https://debug"   }
        release{   buildConfigField "String", "MY_URL", "https://release"   }
    }
    // Specifies one flavor dimension.
    flavorDimensions "version"
    productFlavors {
        user {

        }
        admin {

        }
    }
}

After edit your build.gradle file,you need sync it.

After sync complete,you can use MY_URL by BuildConfig.MY_URL(BuildConfig is generated by Gradle automatically),it looks like following picture:

If your buildType is release then BuildConfig.MY_URL's value will be https://release or debug then it will be https://debug