Gradle 一般配置
apply plugin: 'com.android.application'
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.xxxx.xxxx"
minSdkVersion 16
targetSdkVersion 23
versionCode 25
versionName "1.1.1.${buildTime()}"
}
lintOptions{
abortOnError false
}
signingConfigs {
myConfig {
storeFile file("$projectDir/../doc/xxxx.jks")
storePassword properties.getProperty("KEYSTORE_PASSWORD")
keyAlias "xxxx"
keyPassword properties.getProperty("KEY_PASSWORD")
}
}
buildTypes {
release {
zipAlignEnabled true
minifyEnabled true
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'app-rules.pro'
signingConfig signingConfigs.myConfig
}
debug {
zipAlignEnabled true
minifyEnabled false
multiDexEnabled true
}
}
productFlavors {
master {
versionName defaultConfig.versionName + "-MASTER"
}
client1 {
applicationIdSuffix ".client1"
versionName defaultConfig.versionName + "-CLIENT1"
}
client2 {
applicationIdSuffix ".client2"
versionName defaultConfig.versionName + "-CLIENT2"
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def name = outputFile.name.replace("app", "XXXX")
if (name.contains("debug")) {
name = name.replace("debug.apk", "a" + defaultConfig.versionName + "-debug.apk")
} else {
name = name.replace("release.apk", "a" + defaultConfig.versionName + "-release.apk")
}
output.outputFile = new File(outputFile.parent, name)
}
}
repositories {
flatDir {
dirs 'libs'
}
}
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile project(':framework')
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
}
细节
- 在以上defaultConfig里的versionName,我一般都使用日期来命名,这样版本号一看就知道是哪个版本。
- 在signingConfigs中对签名的库密码和密码使用本地文件(local.properties)来保存,在使用过程中导入即可,并且在.gitignore文件中添加/local.properties来排除local文件的上传,这样可以保证密钥的私密性。
-
在applicationVariants里我对apk的文件名称也用日期做了标注,这样的好处就是清晰,一目了然,但这样会有一个问题,每天日期会变,文件名也会变,但android studio构建历史却会沿用老旧的,因此会导致今天调试的apk居然还是昨天编译的,因此我们需要每天清理下构建历史,为了让电脑自动清理,我把命令保存为.bat文件,放入C盘,创建一个每天自动运行的任务,让电脑每天调用即可。如下:
rd /s /q C:\Users\joy\.AndroidStudio2.2\system\external_build_system\Projects
-
为了配合使用jenkins自动构建系统,并自动打包上传apk,我写了个.sh文件让jenkins调用即可,如下:
#! / bin/sh masterfile="app/build/outputs/apk/xxxx-master-release-upload.apk" if [ -f "$masterfile" ]; then echo "\n $masterfile begin upload..." curl -F "file=@$masterfile" -F "uKey=0927c2fb40552824a90670ca41e4f181" -F "_api_key=d2c25a4f6edf6e10fb19efb6fcccb2a2" http://www.xxxx.com echo "\n" else echo "\n$ masterfile need not upload!" fi clientfile="app/build/outputs/apk/xxxx-client-release-upload.apk" if [ -f "$clientfile" ]; then echo "\n$ clientfile begin upload..." curl -F "file=@$vanefile" -F "uKey=0927c2fb40552824a90670ca41e4f181" -F "_api_key=d2c25a4f6edf6e10fb19efb6fcccb2a2" http://www.xxxx.com echo "\n" else echo "\n$ clientfile need not upload!" fi
另外,jenkins的配置如图: