1、build.gradle文件
读出AndroidManifest.xml 里的版本 和版本号 修改输出文件名
apply plugin: 'com.android.application'
//获取时间戳
def getDate() {
def date = new Date()
def formattedDate = date.format('yyyyMMddHHmm')
return formattedDate
}
//从androidManifest.xml中获取版本号
def getVersionNameFromManifest() {
def manifestParser = new com.android.builder.core.DefaultManifestParser()
return manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
}
//从androidManifest.xml中获取Code
def getVersionCodeFromManifest() {
def manifestParser = new com.android.builder.core.DefaultManifestParser()
return manifestParser.getVersionCode(android.sourceSets.main.manifest.srcFile)
}
android {
compileSdkVersion 19
buildToolsVersion "23.0.0"
String outputName = "wsTvLauncher"
defaultConfig {
applicationId "com.chinacenter.mylauncher"
minSdkVersion 14
targetSdkVersion 18
//dex突破65535限制
//multiDexEnable true
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
//签名
signingConfigs {
debug {
}
relealse {
storeFile file("../../_keystore/matoandroid.keystore")
storePassword "mato@zyyxmu"
keyAlias "android.keystore"
keyPassword "mato@zyyxmu"
}
}
buildTypes {
debug {
}
release {
//签名
signingConfig signingConfigs.relealse
// 是否进行混淆 对应老版本的runProguard
minifyEnabled false
// 混淆文件的位置
/tools/proguard/proguard-android.txt和app/proguard-rules.pro
//proguardFiles 一个文件的时候用proguardFile
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile 'proguard-rules.pro'
}
}
//在生成的apk文件,修改下命名而已
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace(".apk", "-${outputName}" + getVersionNameFromManifest()+".apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/android-async-http-1.4.4.jar')
compile files('libs/fastjson-1.1.26.jar')
compile files('libs/universal-image-loader-1.9.2-with-sources.jar')
compile files('libs/android-logging-log4j-1.0.2.jar')
compile files('libs/log4j-1.2.17.jar')
compile project(':ijkmediawidget')
}
2、在gradle文件里设置不同的 host 环境。使不同的
build.gradle文件
buildConfigField "String", "URL", "\"
http://vms.xxx.com\""
java文件:
public static String mCurrentApi= BuildConfig.URL;
buildTypes { debug_ { buildConfigField "String", "URL", "\"http://xx.xx.xx.com\"" }}
java文件:
public static String mCurrentApi= BuildConfig.URL;
public static String getHost { if (DEBUG) { return "your qa host"; } return "your production host"; }
3、多渠道打包
manifest文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.demo.test1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="34"
android:versionName="v34.1.1">
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<manifest package="com.demo.test1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="34"
android:versionName="v34.1.1">
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
设置脚本的运行环境
//buildscript {
// //支持java 依赖库管理(jcenter/maven/ivy),用于项目的依赖。
// repositories {
// jcenter()
// }
// //依赖包的定义。支持jcenter/maven/ivy,远程,本地库,也支持单文件
// dependencies {
// classpath 'com.android.tools.build:gradle:1.3.0'
// }
//}
//声明构建的项目类型,这里当然是android了
apply plugin: 'com.android.application'
//设置编译android项目的参数
def releaseTime() {
return new Date().format("MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.demo.test1"
minSdkVersion 17
targetSdkVersion 22
versionCode 34
versionName "34.1.1"
// dex突破65535的限制
multiDexEnabled true
// 默认是umeng的渠道
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
}
//Android默认配置,eclipse转Android studio时要添加
// sourceSets {
// main {
// manifest.srcFile 'AndroidManifest.xml'
// java.srcDirs = ['src']
// resources.srcDirs = ['src']
// aidl.srcDirs = ['src']
// renderscript.srcDirs = ['src']
// res.srcDirs = ['res']
// assets.srcDirs = ['assets']
// }
// 测试所在的路径,这里假设是tests文件夹,没有可以不写这一行
// instrumentTest.setRoot('tests')
// }
//这个是解决lint报错的代码,移除lint检查的error
lintOptions {
abortOnError false
}
/**
* 签名设置
*/
signingConfigs {
debug {
}
release {//E:\src\trunk\_keystore
storeFile file("E:/src/trunk/_keystore/matoandroid.keystore")
storePassword "mato@zyyxmu"
keyAlias "android.keystore"
keyPassword "mato@zyyxmu"
}
}
/**
* 混淆设置
*/
buildTypes {
debug {
// 显示Log
// buildConfigField "boolean", "LOG_DEBUG", "true"
//
// versionNameSuffix "-debug"
// minifyEnabled false
// zipAlignEnabled false
// shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不显示Log
// buildConfigField "boolean", "LOG_DEBUG", "false"
// 是否进行混淆
minifyEnabled false
// zipAlignEnabled true
// // 移除无用的resource文件
// shrinkResources true
signingConfig signingConfigs.release
// 混淆文件的位置
/tools/proguard/proguard-android.txt和app/proguard-rules.pro
// proguard-rulesFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//打包文件名 - 不要写在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 = outputFile.name.replace(".apk", "_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk")
def fileName = outputFile.name.replace(".apk", "_v${defaultConfig.versionName}_${releaseTime()}.apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
// 友盟多渠道打包
productFlavors {
wandoujia {}
_360 {}
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
//依赖库
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.1.1'
}