项目build.gradle的那些事(小记)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nzzl54/article/details/79011240

忙碌的2017.12已过,接着迎接忙碌的2018....

很久没写东西了,今天想跟着经验写最近对build.gradle的一些认为值得记录的东西。

一、关于签名

以前很多人都喜欢直接把签名的信息直接写在gradle,但是这样的做法不是太好,我们可以这样做,用一个文件专门存下签名的配置信息,此处姑且叫signing.properties,大致信息如下:放在和build.gradle同一级别目录下即可。

KEYSTORE_FILE = keystore存放路径
KEYSTORE_PASSWORD = keystore密码
KEY_ALIAS = key别名
KEY_PASSWORD = key密码

接下来我们在项目的build.gradle中读取这个文件:

Properties props = new Properties()
props.load(new FileInputStream(file("signing.properties")))

signingConfigs {
        release {
            keyAlias props['KEY_ALIAS']
            keyPassword props['KEY_PASSWORD']
            storeFile file(props['KEYSTORE_FILE'])
            storePassword props['KEYSTORE_PASSWORD']
        }
}

二、生成名字特定的包.apk,如xxx+版本_日期.apk --> test1.1.0_20180108.apk,test1.1.0_20180108_debug.apk

目前我使用的两种:

1.

 //生成debug包和release包名字定义,release包:名称+版本+日期.apk,debug包:名称+版本+日期+debug.apk
    applicationVariants.all{ variant->
        variant.outputs.each { output->
            def oldFile = output.outputFile
            if(variant.buildType.name.equals('release')){
                def releaseApkName = 'test' + defaultConfig.versionName + '-'+buildTime()+'.apk'
                output.outputFile = new File(oldFile.parent, releaseApkName)
            }else {
                def debugApkName = 'test' + defaultConfig.versionName + '-'+buildTime()+'-debug.apk'
                output.outputFile = new File(oldFile.parent, debugApkName)
            }
        }
    }

def buildTime() {
    def df = new SimpleDateFormat("yyyyMMdd")
    df.setTimeZone(TimeZone.getDefault())
    return df.format(new Date())
}

2.

//第二种方法 -- 生成debug包和release包名字定义,release包:名称+版本+日期.apk,debug包:名称+版本+日期+debug.apk
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {
                    def fileName = outputFile.name.replace("app", "test" + "_v${variant.versionName}_${buildTime()}")
                    outputFileName = fileName
                } else {
                    def fileName = outputFile.name.replace("app", "test_debug" + "_v${variant.versionName}_${buildTime()}")
                    outputFileName = fileName
                }
            }
        }
    }

三、方法数超过64k的问题
现在项目都比较大,很容易出现方法数超过64k,当超过的时候As就无法运行,解决的方法是在build.gradle中加入:
defaultConfig {
       ...............
        multiDexEnabled true
       ...............
}
或者:
compile 'com.android.support:multidex:1.0.0'
然后在你写应用入口的XXApplication中继承MultiDexApplication,如:
public class BaseApplication extends MultiDexApplication {

    /** 启用MultiDex*/
 @Override
 protected void attachBaseContext(Context base) {
  super.attachBaseContext(base);
  MultiDex.install(this);
 }
}

好啦,2018继续前进,开发知识面要继续拓宽!


猜你喜欢

转载自blog.csdn.net/nzzl54/article/details/79011240