1.在gradle.properties中添加代码
IS_DEBUG=false
2.在根目录app module下build.gradle文件中添加代码
defaultConfig {
//是否debug
buildConfigField "boolean", "IS_DEBUG", IS_DEBUG
}
signingConfigs {
config {
//加载资源
Properties properties = new Properties()
InputStream inputStream =
project.rootProject.file('local.properties').newDataInputStream()
properties.load(inputStream)
//读取文件
def keyStoreFileDir = properties.getProperty('key.file')
storeFile file(keyStoreFileDir)
//读取字段
def key_keyAlias = properties.getProperty('keyAlias')
def key_keyPassword = properties.getProperty('keyPassword')
def key_storePassword = properties.getProperty('storePassword')
storePassword key_storePassword
keyAlias key_keyAlias
keyPassword key_keyPassword
}
//开启v1和v2签名
release {
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
debug {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
zipAlignEnabled false
//启动资源压缩
shrinkResources false
//混淆
minifyEnabled false
}
release {
minifyEnabled false
signingConfig signingConfigs.config
zipAlignEnabled false
//启动资源压缩
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
applicationVariants.all { variant ->
def type = variant.buildType.name
def buildName
if (IS_DEBUG.toBoolean()) {
buildName = "APP名字"
} else {
buildName = "APP名字"
}
variant.outputs.all {
def df = new SimpleDateFormat("YYYY-MM-dd-HH-mm")
def createTime = df.format(new Date())
if (type.toString() == 'debug') {
outputFileName = 'app-debug.apk'
} else {
def releaseApkName = buildName + "_" + versionName + "_" + createTime + '_' + type + '.apk'
outputFileName = releaseApkName
}
}
}
3.在根目录下local.properties中配置apk文件签名信息
key.file=D\:\\CompanyAndroidProject\\xxx\\xxx\\xxx
keyAlias=xx #别名
keyPassword=xxxxxx #别名密码
storePassword=xxxxxx #打包密码