Skip to content

Instantly share code, notes, and snippets.

@xbee
Forked from MarkMjw/build.gradle
Last active August 30, 2015 05:46
Show Gist options
  • Save xbee/bbff53fa76ec3a8428f6 to your computer and use it in GitHub Desktop.
Save xbee/bbff53fa76ec3a8428f6 to your computer and use it in GitHub Desktop.
build.gradle base on Android Studio, and modify manifest when building different channel.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.2"
compileOptions.encoding = "UTF-8"
signingConfigs {
myConfig{
storeFile file("debug.keystore")
storePassword "123456"
keyAlias "debug"
keyPassword "123456"
}
}
buildTypes{
release {
signingConfig signingConfigs.myConfig
runProguard true
zipAlign true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
debug {
signingConfig signingConfigs.myConfig
}
}
productFlavors {
google {
packageName = 'com.markmao.demo.play'
}
samsung {
packageName = 'com.markmao.demo.samsung'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
//================================================================
// replace meta-data in AndroidManifest.xml
// the regex like this:
// <meta-data android:name="CHANNEL" android:value="*" />
//================================================================
android.applicationVariants.all { variant ->
// rename out apk file
renameApk(variant)
// modify AndroidManifest.xml
variant.processManifest.doLast {
copy {
from("${buildDir}/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/manifests/$variant.name")
def channelName = variant.productFlavors[0].name;
println "[Channel]: ${channelName}"
filter {
String line ->
line.replaceAll("<meta-data android:name=\"CHANNEL\" android:value=\".*\"/>",
"<meta-data android:name=\"CHANNEL\" android:value=\"${channelName}\"/>")
}
// set the path to the modified Manifest:
def manifestPath = "${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml"
variant.processResources.manifestFile = file(manifestPath)
}
}
}
def renameApk(variant) {
// get data for apk renaming
def appName
def versionName
// local.properties def by user
def propsFile = rootProject.file('local.properties')
if (propsFile.exists()) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
appName = props['app_name']
android.defaultConfig.versionName = props['app_version']
versionName = android.defaultConfig.versionName
} else {
appName = "TestGradle"
versionName = "1.0"
}
// println "[App_Name]: ${appName}"
// println "[App_Version]: ${versionName}"
def channelName = variant.productFlavors[0].name;
if (variant.zipAlign) {
def oldFile = variant.outputFile;
def newFile = appName + "-v" + versionName + "-" + channelName + ".apk"
variant.outputFile = new File(oldFile.parent, newFile)
}
// def oldFile = variant.packageApplication.outputFile;
// def newFile = appName + "-v" + versionName +"-" + versionNameSuffix + "-unaligned.apk"
// variant.packageApplication.outputFile = new File(oldFile.parent, newFile)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment