How can I set different build type for each build variant

ε祈祈猫儿з 提交于 2020-01-15 03:25:25

问题


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



来源:https://stackoverflow.com/questions/53185968/how-can-i-set-different-build-type-for-each-build-variant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!