Last active
November 27, 2023 13:40
-
-
Save MarkMjw/7998381 to your computer and use it in GitHub Desktop.
build.gradle base on Android Studio, and modify manifest when building different channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| buildscript { | |
| repositories { | |
| mavenCentral() | |
| } | |
| dependencies { | |
| classpath 'com.android.tools.build:gradle:0.6.+' | |
| } | |
| } | |
| apply plugin: 'android' | |
| dependencies { | |
| compile fileTree(dir: 'libs', include: '*.jar') | |
| } | |
| android { | |
| compileSdkVersion 15 | |
| buildToolsVersion "18.1.1" | |
| compileOptions.encoding = "UTF-8" | |
| defaultConfig { | |
| versionName "1.7.5" | |
| } | |
| signingConfigs { | |
| sinaAndroid{ | |
| storeFile file("debug.store") | |
| storePassword "123456" | |
| keyAlias "debug" | |
| keyPassword "123456" | |
| } | |
| } | |
| buildTypes{ | |
| release { | |
| signingConfig signingConfigs.sinaAndroid | |
| runProguard true | |
| zipAlign true | |
| proguardFiles files('proguard.cfg', getDefaultProguardFile('proguard-android.txt')) | |
| } | |
| samsung { | |
| initWith release | |
| versionNameSuffix "samsung" | |
| // packageNameSuffix ".samsung" | |
| } | |
| google { | |
| initWith release | |
| versionNameSuffix "google" | |
| // packageNameSuffix ".google" | |
| } | |
| } | |
| // different package name | |
| // productFlavors { | |
| // google { | |
| // packageName = 'com.xxx.xxx.play' | |
| // } | |
| // samsung { | |
| // packageName = 'com.xxx.xxx.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') | |
| } | |
| } | |
| //================================================================ | |
| // Modify AndroidManifest.xml | |
| // <meta-data android:name="CHANNEL" android:value="*" /> | |
| //================================================================ | |
| android.applicationVariants.all { variant -> | |
| // rename out apk file | |
| renameApk(variant) | |
| def propertyList = variant.productFlavors*.name | |
| propertyList.add(variant.buildType.name) | |
| // read property from file | |
| def variantProperties = new Properties() | |
| propertyList.reverse().each { property -> | |
| def propFile = "channel.properties" | |
| try { | |
| file(propFile).withReader { reader -> | |
| def tmpProps = new Properties() | |
| tmpProps.load(reader) | |
| variantProperties.putAll(tmpProps) | |
| } | |
| } catch(e) { | |
| println "[${variant.name}] Failed to load ${propFile}" | |
| } | |
| } | |
| println "[${variant.name}] manifest properties: ${variantProperties}" | |
| // default channel value is SinaDown | |
| def defaultValue = "16" | |
| def channel = variant.buildType.name | |
| def value = variantProperties.getProperty("${channel}", defaultValue) | |
| println "[Channel]: ${channel} [Value]: ${value}" | |
| // modify AndroidManifest.xml | |
| variant.processManifest.doLast { | |
| copy { | |
| from("${buildDir}/manifests") { | |
| include "${variant.dirName}/AndroidManifest.xml" | |
| } | |
| into("${buildDir}/manifests/$variant.name") | |
| // get the channel value | |
| def channelValue = variantProperties.getProperty("${channel}", defaultValue) | |
| println "[Channel]: ${channel} [Value]: ${channelValue}" | |
| filter { | |
| String line -> | |
| line.replaceAll("<meta-data android:name=\"CHANNEL\" android:value=\".*\"/>", | |
| "<meta-data android:name=\"CHANNEL\" android:value=\"${channelValue}\"/>") | |
| } | |
| // 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 = "TestGradle" | |
| def versionName = android.defaultConfig.versionName | |
| def versionNameSuffix = variant.buildType.versionNameSuffix | |
| if (versionNameSuffix.toString().equals("null")) { | |
| def buildTypeName = variant.buildType.name | |
| versionNameSuffix = buildTypeName.toLowerCase() | |
| } | |
| if (variant.zipAlign) { | |
| def oldFile = variant.outputFile; | |
| def newFile = appName + "-v" + versionName + "-" + versionNameSuffix + ".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