原生的打包还没研究 估计肯定比这个简单
首先 要在清单文件中 加入代码
<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实例进行获取就完事儿了
时间: 2024-10-15 00:35:21