分类 默认分类 下的文章

Debug Release采用不同资源

From Steffen Funke

Just to give a working example to my comment above:

declare a resValue in your defaultConfig which will become the Application's name. (Attention: if you choose to name it app_name, be sure to delete the original app_name property from your strings.xml file, or it will clash.)

defaultConfig {
    // applicationId, versionCode, etc.

    (...)

    // define your base Applications name here
    resValue 'string', 'app_name', 'MyApp'
}

set your productFlavors as you did already. You can leave them empty if it is ok for you to concat your App's name with the flavor's name only, or provide an explicit resValue, which will override the value from defaultConfig.

productFlavors {
    dev {
        // resValue 'string', 'app_name', 'MyAppDevFlavor'
    }

    prod {
        // resValue 'string', 'app_name', 'MyAppProdFlavor'
    }
}

configure the resValue's name at gradle configuration time

android.applicationVariants.all { variant ->
    // get app_name field from defaultConfig
    def appName = variant.mergedFlavor.resValues.get('app_name').getValue()

    // concat new App name with each flavor's name
    appName = "${appName}"
    variant.productFlavors.each { flavor ->
        appName += " ${flavor.name}"
    }

    // optionally add buildType name
    appName += " ${variant.buildType.name}"

    // your requirement: if buildType == debug, add DEV to App name
    if(variant.buildType.name == "debug") {
        appName += " DEV"
    }

    // set new resVale
    variant.resValue 'string', 'app_name', appName
}

In your AndroidManifest, set the app_name field:

    <application
    ...
    android:label="@string/app_name"
    ...
    >

As I mentioned above, be sure to delete the default app_name property from strings.xml

啦啦啦,试用typecho

Typecho

传说中的轻博客

今天心血来潮建了一个离线博客,以后技术各方面的东西都往这里面写吧,当然也免不了吐槽。主要typecho支持markdown,比较方便,又没有workpress那么笨重

2016年3月23日