现在一个Android工程都会在不同的商店平台上发布产品,为了统计方便都会为每个平台分配一个渠道ID,因此我们需要根据不同的ID来打包针对不同平台的APK。无疑Gradle是一个很好的工具,具体Gradle是什么,请点击这里。
使用新版本的gradle,解决这个问题很方便,使用class="s1">manifestPlaceholders即可(详情请点击这里),但如果碰到老版本的gradle,是不支持的,所以我们需要另想办法。
下面是我弄的一个适用的办法:
1 buildscript { 2 repositories { 3 mavenCentral() 4 } 5 dependencies { 6 classpath 'com.android.tools.build:gradle:0.6.+' 7 } 8 } 9 10 apply plugin: 'android' 11 12 repositories { 13 mavenCentral() 14 maven { url 'libraries' } 15 } 16 17 android { 18 compileSdkVersion 19 19 buildToolsVersion "19" 20 21 defaultConfig { 22 minSdkVersion 8 23 targetSdkVersion 19 24 25 versionCode 1 26 versionName "1.1.1" 27 packageName "~~~~~~~" 28 } 29 30 signingConfigs { 31 release { 32 storeFile file("~~~.keystore") 33 storePassword "~~~" 34 keyAlias "~~~.keystore" 35 keyPassword "~~~" 36 } 37 } 38 39 productFlavors { 40 渠道名 { 41 signingConfig signingConfigs.release 42 packageName "对应的包名" 43 } 44 } 45 46 buildTypes { 47 release { 48 runProguard true 49 proguardFile "proguard.cfg" 50 } 51 } 52 53 sourceSets { 54 main { 55 manifest.srcFile 'AndroidManifest.xml' 56 java.srcDirs = ['src'] 57 resources.srcDirs = ['src'] 58 aidl.srcDirs = ['src'] 59 renderscript.srcDirs = ['src'] 60 res.srcDirs = ['res'] 61 assets.srcDirs = ['assets'] 62 } 63 } 64 65 //替换Manifest中的“CHANNEL_VALUE”为渠道号 66 android.applicationVariants.all{ variant -> 67 variant.processManifest.doLast{ 68 copy{ 69 from("${buildDir}/manifests"){ 70 include "${variant.dirName}/AndroidManifest.xml" 71 } 72 into("${buildDir}/manifests/$variant.name") 73 74 filter{ 75 String line -> line.replaceAll("CHANNEL_VALUE", "${variant.productFlavors[0].name}") 76 } 77 variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml") 78 } 79 } 80 //重命名 81 renameOutputFile(variant) 82 } 83 84 } 85 86 def releaseTime() { 87 return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC")) 88 } 89 90 def renameOutputFile(variant) { 91 def apk = variant.outputFile; 92 def newName = ""; 93 94 // get data for apk renaming 95 def packageName = variant.productFlavors[0].packageName 96 def channelName = variant.productFlavors[0].name 97 def versionName = android.defaultConfig.versionName 98 def versionCode = android.defaultConfig.versionCode 99 def releaseTime = releaseTime() 100 101 if (variant.zipAlign) { 102 newName = "名字-" + versionCode + "-" + versionName + "-" + packageName + "-" + channelName + "-smaller-" + releaseTime + "-release.apk" 103 }else{ 104 newName = "名字-" + versionCode + "-" + versionName + "-" + packageName + "-" + channelName + "-smaller-" + releaseTime + "-unalign.apk" 105 } 106 variant.outputFile = new File(apk.parentFile, newName); 107 } 108 109 dependencies { 110 //compile project(':abc') 111 //compile 'com.android.support:support-v4:13.0.+' 112 113 compile fileTree(dir: 'libs', include: ['*.jar']) 114 } 115 116 task copyNativeLibs(type: Copy) { 117 from fileTree(dir: 'libs', include: '**/*.so') into 'build/native-libs' 118 } 119 120 tasks.withType(Compile) { 121 compileTask -> compileTask.dependsOn copyNativeLibs 122 options.encoding = "UTF-8" 123 } 124 125 tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask -> 126 pkgTask.jniDir file('build/native-libs') 127 }
这里面还会有一个问题,就是中文编码的问题,如果Manifest.xml中含有中文,十有八九会报错的。所以这里有一个解决办法:
修改你的GRADLE_HOME/bin下的gradle执行文件,windows应该是gradle.bat,其它系统的直接就是gradle文件,将DEFAULT_JVM_OPTS的值修改为"-Dfile.encoding=UTF-8"。
祝大家一路顺风。