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.7.+' | |
| } | |
| } | |
| apply plugin: 'android' | |
| dependencies { | |
| compile fileTree(dir: 'libs', include: '*.jar') | |
| } | |
| android { | |
| compileSdkVersion 19 | |
| buildToolsVersion "19.0.1" | |
| 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 | |
| } | |
| samsung { | |
| initWith release | |
| versionNameSuffix ".1.0" | |
| packageNameSuffix ".samsung" | |
| } | |
| google { | |
| initWith release | |
| versionNameSuffix ".1.0" | |
| packageNameSuffix ".play" | |
| } | |
| } | |
| // different package name | |
| // productFlavors { | |
| // google { | |
| // packageName = 'com.sina.book.play' | |
| // } | |
| // samsung { | |
| // packageName = 'com.sina.book.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 -> | |
| // channel.properties def by user | |
| 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 | |
| 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 buildTypeName = variant.buildType.name | |
| if (variant.zipAlign) { | |
| def oldFile = variant.outputFile; | |
| def newFile = appName + "-v" + versionName + "-" + buildTypeName + ".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) | |
| } | |
| def readBuildTypes(variant) { | |
| def channelsProps = new Properties() | |
| // channel.properties def by user | |
| def channelsFile = rootProject.file('channel.properties') | |
| if (channelsFile.exists()) { | |
| channelsProps.load(new FileInputStream(channelsFile)) | |
| } | |
| channelsProps.each { key, value -> | |
| println "[Key]: ${key}" | |
| println "[Value]: ${value}" | |
| } | |
| } | |
| //allprojects { | |
| // afterEvaluate { project -> | |
| // // channel.properties def by user | |
| // def propsFile = rootProject.file('local.properties') | |
| // def configName = 'release' | |
| // | |
| // if (propsFile.exists() && android.signingConfigs.hasProperty(configName)) { | |
| // def props = new Properties() | |
| // props.load(new FileInputStream(propsFile)) | |
| // android.signingConfigs[configName].storeFile = file(props['key.store']) | |
| // android.signingConfigs[configName].storePassword = props['key.store.password'] | |
| // android.signingConfigs[configName].keyAlias = props['key.alias'] | |
| // android.signingConfigs[configName].keyPassword = props['key.alias.password'] | |
| // | |
| // println "[storeFile]: ${android.signingConfigs[configName].storeFile}" | |
| // println "[storePassword]: ${android.signingConfigs[configName].storePassword}" | |
| // println "[keyAlias]: ${android.signingConfigs[configName].keyAlias}" | |
| // println "[keyPassword]: ${android.signingConfigs[configName].keyPassword}" | |
| // } | |
| // } | |
| //} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment