在build.gradle下配置// 打包渠道 productFlavors { x_android studio如何通过配置文件不同渠道取不同的配置">
赞
踩
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}"/>
// 打包渠道
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qh360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
}
apply plugin: 'com.android.application'
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.icloud.game"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
//在这里配置这个后,可以在Manifest中这样替换${APPID} ${CPID}
manifestPlaceholders = [APPID: "100003255",
CPID: "900086000023243000"]
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//签名
signingConfigs {
debug {
}
release {// 自己的签名文件 ../chao.jks
storeFile file("E:/keystore/chao.jks")
storePassword "123456"
keyAlias "key0"
keyPassword "123456"
}
}
buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled true
//Zipalign优化
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
//前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明,后一个文件是自己的定义混淆文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//签名
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为app_v1.0_2015-xx-xx_xxxx.apk
//def fileName = "app_v${defaultConfig.versionName}_${releaseTime()}.apk"
def fileName = "app_v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
// 打包渠道
productFlavors {
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
qh360 {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qh360"]
}
baidu {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support.constraint:constraint-layout:1.0.1'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.android.support:support-v4:25.3.1'
}
如果要引用lib文件夹下的so文件,在build.gradle的android结点下添加:
sourceSets.main.jniLibs.srcDirs = ['libs']
引用添加aar文件,在build.gradle的根结点下添加:
repositories {flatDir { dirs 'libs' }}
同时在dependencies结点下:
compile(name:'gamecenter-release1_0_1', ext:'aar')
这里的name改为你aar文件的名称。
#指定代码的压缩级别
-optimizationpasses 5
#包明不混合大小写
-dontusemixedcaseclassnames
#不去忽略非公共的库类
-dontskipnonpubliclibraryclasses
#优化 不优化输入的类文件
-dontoptimize
#预校验
-dontpreverify
#混淆时是否记录日志
-verbose
# 混淆时所采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#保护注解
-keepattributes *Annotation*
# 保持哪些类不被混淆
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
#如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment
#忽略警告
-ignorewarning
##记录生成的日志数据,gradle build时在本项目根目录输出##
#apk 包内所有 class 的内部结构
-dump proguard/class_files.txt
#未混淆的类和成员
-printseeds proguard/seeds.txt
#列出从 apk 中删除的代码
-printusage proguard/unused.txt
#混淆前后的映射
-printmapping proguard/mapping.txt
########记录生成的日志数据,gradle build时 在本项目根目录输出-end######
#如果引用了v4或者v7包
-dontwarn android.support.**
####混淆保护自己项目的部分代码以及引用的第三方jar包library-end####
#保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}
#保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
#保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable
#保持 Serializable 不被混淆并且enum 类也不被混淆
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
#保持枚举 enum 类不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * {
public void *ButtonClicked(android.view.View);
}
#不混淆资源类
-keepclassmembers class **.R$* {
public static <fields>;
}
########以上通用,下面是避免混淆第三方jar包############
-keep class com.squareup.okio.**{*;}
-keep class com.squareup.okhttp3.**{*;}
-keep class com.squareup.okio.**{*;}
-keepclassmembers class com.icloud.game.activity.MainActivity{
public *;
}
-keepattributes *JavascriptInterface*
对于混淆的语法,例如引入的第三方jar包可以使用以下语法:
-keep class com.UCMobile.PayPlugin.* {*;}
表示将包com.UCMobile.PayPlugin.*;的全部类都不混淆
-keep class com.unionpay.** {*;}
表示包com.unionpay的子包以及子包中的类全都不混淆
gradlew assembleRelease
gradlew assemblexiaomiRelease
第一个语句将会把所有配置的渠道都生成渠道包
第二个语句将会只生成小米的渠道包
签名密码写在gradle中不安全,如果需要在命令行中输入签名密码:
signingConfigs { //gradle assembleRelease
myConfig {
storeFile file("stone.keystore")
storePassword System.console().readLine("\nKeystore password: ")
keyAlias "stone"
keyPassword System.console().readLine("\nKey password: ")
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。