Skip to content

Instantly share code, notes, and snippets.

@AvatarQing
Created December 29, 2014 13:29
Show Gist options
  • Save AvatarQing/49f1242747fa1c955bb4 to your computer and use it in GitHub Desktop.
Save AvatarQing/49f1242747fa1c955bb4 to your computer and use it in GitHub Desktop.
2048gradle友盟渠道打包+应用名称替换脚本
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'com.android.tools.build:gradle:1.0.0' }
}
apply plugin: 'com.android.application'
dependencies {
compile 'com.android.support:support-v4:21.0.0'
compile 'com.google.android.gms:play-services:+'
compile project(':CommonAdLib:CommonAdLib')
compile project(':CommonAnalyticsLib:CommonAnalyticsLib')
compile project(':CrossPromotionLib:CrossPromotionLib')
}
tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
StringReader appReader=new StringReader(file("${projectDir.getAbsolutePath()}/configs/appbuild.txt").getText("utf-8"))
Properties appProperties=new Properties();
appProperties.load(appReader);
android {
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig {
versionCode Integer.parseInt(appProperties.getProperty("versionCode"))
versionName appProperties.getProperty("versionName")
minSdkVersion 9
targetSdkVersion 21
applicationId appProperties.getProperty("packageName")
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
signingConfigs {
myConfig{
StringReader signReader=new StringReader(file("${projectDir.getAbsolutePath()}/configs/signature.txt").getText("utf-8"))
Properties signProperties=new Properties();
signProperties.load(signReader);
storeFile file(signProperties.getProperty("storeFile"))
storePassword signProperties.getProperty("storePassword")
keyAlias signProperties.getProperty("keyAlias")
keyPassword signProperties.getProperty("keyPassword")
}
}
buildTypes{
release {
signingConfig signingConfigs.myConfig
minifyEnabled true
proguardFile 'proguard-project.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['native-libs']
jni.srcDirs = [] //disable automatic ndk-build
}
instrumentTest.setRoot('tests').java
}
sourceSets.all { entry->
def isProductFlavorName={ name->
def result=false
if(name){
android.productFlavors.all { flavor->
if(flavor.name==name){
result=true
}
}
}
return result
}
def isBuildTypeName={ name->
def result=false
if(name){
android.buildTypes.all { type->
if(type.name==name){
result=true
}
}
}
return result
}
def s=File.separator
if(isBuildTypeName(entry.name)){
entry.setRoot("build-types${s}${entry.name}");
}else if(isProductFlavorName(entry.name)){
entry.setRoot("product-flavors${s}${entry.name}");
}
}
productFlavors {
yingyongbao {
}
baidu {
}
hiapk {
}
market91 {
}
market360 {
}
lenovo {
}
xiaomi {
}
other {
}
}
clean {
delete += file('native-libs')
delete += file('product-flavors')
delete += file('build-types')
}
preBuild.doFirst{
def String dirNativeLibs="native-libs"
file(dirNativeLibs).mkdirs()
fileTree("libs") { include "**/*.so" }.copy { into dirNativeLibs}
}
preBuild.doFirst{
productFlavors.each{ flavor->
//- [Updating XML with XmlParser](http://groovy.codehaus.org/Updating+XML+with+XmlParser)
//- [Updating XML with XmlSlurper](http://groovy.codehaus.org/Updating+XML+with+XmlSlurper)
// ------
// Define general updating action
def updatingXmlElements={xmlInFilePath,xmlOutFilePath,nodeMap->
// Updating logics
def logicUpdatingElement={elementAttrName,xmlRoot,key,value->
def item=xmlRoot."${elementAttrName}".find{ it.@name==key }
item?.value= value
}
def logicUpdatingElementString=logicUpdatingElement.curry("string")
def logicUpdatingElementBool=logicUpdatingElement.curry("bool")
def logicUpdatingElementInteger=logicUpdatingElement.curry("integer")
// Read xml file
String encoding="utf-8"
def xmlRoot = new XmlParser().parseText(new File(xmlInFilePath).getText(encoding))
// Updating item
nodeMap.string.each{key,value->
logicUpdatingElementString(xmlRoot,key,value)
}
nodeMap.bool.each{key,value->
logicUpdatingElementBool(xmlRoot,key,value)
}
nodeMap.integer.each{key,value->
logicUpdatingElementInteger(xmlRoot,key,value)
}
// Save modifications
def stringWriter = new StringWriter()
new XmlNodePrinter(new PrintWriter(stringWriter)).print(xmlRoot)
groovy.xml.StreamingMarkupBuilder smb=new groovy.xml.StreamingMarkupBuilder()
smb.encoding=encoding
def xmlResult = smb.bind {
mkp.pi(xml: "version='1.0' encoding='${encoding}'")
mkp.yieldUnescaped stringWriter.toString()
}
def File outFile=new File(xmlOutFilePath)
outFile.getParentFile().mkdirs();
outFile.write(xmlResult.toString(),"utf-8")
}
// ------
// Prepare updating
// Common constants
def final String encoding="utf-8"
def final s=File.separator
def final String orginalResDir=sourceSets.main.res.srcDirs.asList()[0]
def final String flavorResDir=sourceSets."${flavor.name}".res.srcDirs.asList()[0]
// Common variables
def String updatingFilePath
def String originalFilePath
def String flavorFilePath
def nodeMapString = [:]
def nodeMap = [:]
// Common behavior
nodeMap.string=nodeMapString
def updatingXml={action->
nodeMapString.clear()
action()
originalFilePath="${orginalResDir}${s}${updatingFilePath}"
flavorFilePath="${flavorResDir}${s}${updatingFilePath}"
updatingXmlElements(originalFilePath,flavorFilePath,nodeMap)
}
// Updating analytics configs
updatingXml{
nodeMapString.umeng_channel_id = flavor.name
updatingFilePath="values${s}analytics_config.xml"
}
// Updating app name in English
updatingXml{
nodeMapString.app_name=appProperties.getProperty("appName_en")
updatingFilePath="values${s}strings.xml"
}
// Updating app name in Chinese
updatingXml{
nodeMapString.app_name=appProperties.getProperty("appName_zh")
updatingFilePath="values-zh${s}strings.xml"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment