標籤:
原生的打包還沒研究 估計肯定比這個簡單
首先 要在資訊清單檔中 加入代碼
<pre name="code" class="java">
<meta-data android:name="CHANNEL_ID" android:value="${CHANNEL_ID_VALUE}" />
然後 先從外層build.gradle進行改動
ext{}和task clean{}為新增代碼:
<pre name="code" class="java">// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' }}ext { minSdkVersion = 8 targetSdkVersion = 19 compileSdkVersion = 19 buildToolsVersion = '23.0.3'<span style="color:#ff0000;"> sourceCompatibilityVersion = JavaVersion.VERSION_1_7 targetCompatibilityVersion = JavaVersion.VERSION_1_7</span>}task clean(type: Delete) { delete rootProject.buildDir}
裡面具體版本號碼根據自己需求來
然後更改大部分的是內層的build.gradle,也就是Model中build.gradle
<pre name="code" class="java">apply plugin: 'com.android.application'dependencies { compile fileTree(dir: 'libs', include: '*.jar') compile project(':PushSDK')}buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' }}android { compileSdkVersion 19 buildToolsVersion "23.0.3" defaultConfig { applicationId "com.XX" //com.XX為包名 minSdkVersion rootProject.ext.minSdkVersion //剛才設定外層build.gradle就為了這用 targetSdkVersion rootProject.ext.targetSdkVersion //理由同上 versionCode 19 versionName "1.1.6" //版本名稱和版本號碼 根據自己需求來 manifestPlaceholders = [ APPLICATION_NAME : "XX", //XX為項目名稱 UMENG_APPKEY_VALUE : "友盟的APPKEY", //我的項目插入了友盟,可忽略 UMENG_CHANNEL_VALUE: "此處為友盟的渠道號", //友盟的渠道號 沒引用友盟可忽略 CHANNEL_ID_VALUE: "xxxx_99999", //預設渠道號 此處和友盟無關 是你要產生的渠道號 不可忽略!!!! UMENG_MESSAGE_SECRET_VALUE: "**********" //友盟相關 可忽略 ] } signingConfigs { //簽名檔案相關 可自動產生 此處設定參考下面的圖 myConfig { keyAlias '化名' keyPassword '密鑰密碼' storeFile file('密鑰路徑') storePassword '自動產生的密碼' } } buildTypes { release {//打包的時候會運行這個模式 signingConfig signingConfigs.myConfig minifyEnabled false//是否開啟混淆 一般不開啟混淆,因為開啟混淆各種審核不容易通過 最好等都打包好後通過360加固等工具進行加密 zipAlignEnabled true//是否支援zip檔案的壓縮 shrinkResources true//是否對無用資源進行刪減// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' 這行在開啟混淆的時候用 不開啟混淆就注釋掉 } debug {//平時直接運行會走這個 signingConfig signingConfigs.myConfig minifyEnabled false zipAlignEnabled true shrinkResources true// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } 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') } productFlavors { //此處寫想要的渠道號 xxxx_99999 // 360手機小幫手 xxxx_99998 // 應用寶 xxxx_99997 // 百度應用 } applicationVariants.all { variant -> //此處設定產生的apk路徑和名稱 variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def variantName = variant.name; //variantName = variantName.substring(0, variantName.lastIndexOf("_")) def fileName = "${variantName}_v${defaultConfig.versionName}_${releaseTime()}_${variant.buildType.name}.apk" output.outputFile = new File("F:\\XXXX\\XXXX\\XXXX\\XXXX", fileName) } } } productFlavors.all { flavor -> manifestPlaceholders = [CHANNEL_ID_VALUE: name ,UMENG_CHANNEL_VALUE: name] }}// 編譯時間def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))}tasks.withType(JavaCompile) { options.encoding = "UTF-8"}
上面關於簽名的檔案如設定後自動產生 開啟Open Module Settings找到自己的Module →Signing
這樣基本配置就完成了
最後要產生各渠道apk的時候選擇gradle
這樣雙擊會同時產生兩個包,一個正常包一個debug包
可按設定 這樣就只產生一個正常包
這樣就基本完事了 我沒取消debug就自動產生了倆
這樣基本完事了 想要擷取渠道ID的時候 可在 MyApplication裡面增加個方法擷取渠道ID
public String getChannelID(){String id = "99999"; //預設的渠道號try{ApplicationInfo info = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);id = String.format("%05d", info.metaData.getInt("CHANNEL_ID"));Log.e("RHID", "RHID_S_1: " + id);if (id == null || id.trim().length() == 0 || id.equals("00000")){id = "99999"; //預設渠道號Log.e("RHID", "RHID_S_2: " + id);}Log.e("RHID", "RHID_S_3: " + id);}catch (Exception e){e.printStackTrace();id = "99999"; //預設渠道號Log.e("RHID", "RHID_F: " + id);}return id;}通過擷取MyApplication執行個體進行擷取就完事兒了
Eclipse轉成Android Studio項目進行多渠道打包