2016年4月

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

PHP用curl访问https

昨天用php改写here的api,在做微博登录的时候,需要curl一下微博的api获取用户信息,但是微博的api是https协议,刚开始死活都访问不了,查了半天,才整明白。解决办法如下:

  1. 下载CAcert.pem,在windows中最好下载打包成rar的文件再解压出来,然后在属性中解除限制
  2. 在php.ini中修改curl.cainfo为CAcert.pem的地址
  3. 重启php

Done

安卓6.0修复org.apache包缺失问题

最近把build tool换成23之后,开始出现各种错误,包括gradle打包apk过程中出现警告,还有app莫名崩溃,在网上查了一下,是因为安卓6.0把org.apache.http从包中除去有关系,解决办法:

  1. 找到org.apache.http.legacy.jar并复制到libs里
  2. 在gradle中添加dependencies
  3. 在proguard-rules.pro中写入:
    -dontwarn org.apache.**

-keep class org.apache.* { public <methods>; !private ; }
-keep class android.net.* { public <methods>; !private ; }
-keep class com.google.android.vending.licensing.* { public <methods>; !private ; }
可再适当的添加或删减rules

Done