原生的打包还没研究 估计肯定比这个简单
首先 要在清单文件中 加入代码
然后 先从外层build.gradle进行改动
ext{}和task clean{}为新增代码:
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'
sourceCompatibilityVersion = JavaVersion.VERSION_1_7
targetCompatibilityVersion = JavaVersion.VERSION_1_7
}
task clean(type: Delete) {
delete rootProject.buildDir
}
里面具体版本号根据自己需求来
然后更改大部分的是内层的build.gradle,也就是Model中build.gradle
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/
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src//... 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包
可按下图设置 这样就只生成一个正常包
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"));
if (id == null || id.trim().length() == 0 || id.equals("00000"))
{
id = "99999"; //默认渠道号
}
Log.e("RHID", "RHID_S_3: " + id);
}
catch (Exception e)
{
e.printStackTrace();
id = "99999"; //默认渠道号
}
return id;
}
通过获取MyApplication实例进行获取就完事儿了
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.king2.myelephant"
minSdkVersion rootProject.ext.minSdkVersion //刚才设置外层build.gradle就为了这用
targetSdkVersion rootProject.ext.targetSdkVersion //理由同上
versionCode 1
versionName "1.0"
manifestPlaceholders = [
APPLICATION_NAME: "我的app", //XX为项目名称
CHANNEL_ID_VALUE: "我的app_99999", //默认渠道号 此处和友盟无关 是你要生成的渠道号 不可忽略!!!!
]
}
signingConfigs {
myConfig {
keyAlias 'com.myapp'
keyPassword '12345678'
storeFile file('D:/myappkey.jks')
storePassword '123456789'
}
}
buildTypes {
release {//打包的时候会运行这个模式
signingConfig signingConfigs.myConfig
minifyEnabled false // 是否开启混淆
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
}
productFlavors {
//安卓客户端渠道号
// myapp_99999 // 官网
// myapp_99998 // 应用宝
// myapp_99997 // 百度应用
}
applicationVariants.all { variant ->
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("E:\Myapp", fileName)
}
}
}
productFlavors.all { flavor ->
manifestPlaceholders = [CHANNEL_ID_VALUE: name]
}
}
buildscript {
repositories {
mavenCentral()
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'org.xutils:xutils:3.3.32'
compile 'com.android.support:design:23.3.0'
}
// 编译时间
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}