apply plugin: 'com.android.application' //代表你是一个应用
def releaseTime() { //这是gradle语法,声明了一个获取当前时间的方法
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android { //你的应用的配置情况
compileSdkVersion 21 //编译你的应用的SDK版本
buildToolsVersion '21.1.2' //使用的tool版本
defaultConfig { //默认配置
applicationId "com.boohee.*" //你的包名
minSdkVersion 14 //兼容最低的Android的版本
targetSdkVersion 21 //一般跟compileSdkVersion一样就可以了
versionCode 1 //版本号,只知道更新用的着
versionName "1.0" //用于改动幅度不大的APP,而又区别原来APP
// dex突破65535的限制 --支持多dex文件
multiDexEnabled true
}
lintOptions { //true--错误发生后停止gradle,
//了解更多http://blog.csdn.net/berber78/article/details/60766091
abortOnError false
}
signingConfigs {
debug {
// No debug config
}
release {
storeFile file("../yourapp.keystore") //在项目的根目录下
storePassword "your password"
keyAlias "your alias"
keyPassword "your password"
}
}
buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug" //版本的后缀 如:XXXX-debug.apk
minifyEnabled false//是否混淆代码
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true //是否压缩
// 移除无用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//混淆配置文件定要配好
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
//${variant.buildType.name} (debug,release)字段
output.outputFile = new File(outputFile.parent+"/${variant.buildType.name}", fileName)
}
}
}
}
}
}
dependencies { //你的依赖
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
...
}
http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ 引用自大神的代码,如有出错或有更多更好的资源请留言
编译apk命令行(需要配置gradle环境)
#此为window的.bat可执行文件
#卸载apk 包名
adb uninstall com.xxx.xxx
::进入项目文件
cd /d D:\AndroidProject\xxx
::杀掉java.exe进程
::taskkill /im java.exe /f
::清理缓存build文件 并运行代码
gradle clean & gradle run
::执行完命令行不关闭窗口
pause
::gradle 编译 gradle run 会编译并安装并启动app
::gradle :app:assembleOldmodelsDebug
::gradle 编译并安装apk cmd /k(不关闭命令行)
::cmd /k gradle installDebug
::#gradle 编译coursepath模块为 aar库
::#gradlew :coursepath:assemble
::#查找编译apk中具体报错的信息 --- 对gradle报错没有给出具体报错信息时使用
::#gradlew compileDebugSources --stacktrace -info