ext { //全局變量控制,可在module中的build.gradle文件經過rootProject.ext.xxx開頭來使用 compileSdkVersion = 24 buildToolsVersion = '24.0.3' supportVersion = '24.2.1' }
項目根目錄新建一個簽名用到的密碼管理文件signing.propertiesjava
signing.alias=dou361 #release signing.password=dou361 #release signing.jjdxm_alias=dou361 #debug signing.jjdxm_password=dou361 #debug
在主程序build.gradle的apply plugin: 'com.android.application'下面添加android
Properties props = new Properties() props.load(new FileInputStream(file(rootProject.file("signing.properties"))))
在android{}節點裏面添加微信
signingConfigs { release { keyAlias props['signing.alias'] keyPassword props['signing.password'] storeFile file(rootProject.file("debug.keystore")) storePassword props['signing.password'] } debug { keyAlias props['signing.jjdxm_alias'] keyPassword props['signing.jjdxm_password'] storeFile file(rootProject.file("debug.keystore")) storePassword props['signing.jjdxm_password'] } } buildTypes { debug { signingConfig signingConfigs.debug minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } release { signingConfig signingConfigs.release minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
productFlavors { //接口正式環境仍是測試環境 env_public { buildConfigField "boolean", "isTestEnv", "false" } env_test { buildConfigField "boolean", "isTestEnv", "true" } }
跟buildTypes結合就有四種Build Variants(構建變種)。能夠不修改代碼直接運行相應的apk
會自動運行到BuildConfig裏,能夠判斷不一樣的值去加載不一樣的接口環境app
/** * 是否測試環境 */ public static boolean isTest() { return BuildConfig.isTestEnv; } ServiceInfoManager.getInstance().setEnv(IqbConfig.isTest() ? ServiceInfoManager.Environment.TestEnv : ServiceInfoManager.Environment.PublicEnv);
public class AppUtils { private static Boolean isDebug = null; public static boolean isDebug() { return isDebug == null ? false : isDebug.booleanValue(); } public static void syncIsDebug(Context context) { if (isDebug == null) { isDebug = context.getApplicationInfo() != null && (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; } } } //在本身的 Application 內調用進行初始化 AppUtils.syncIsDebug(getApplicationContext());
//新建表示統一標識的註解 NotProguard @Retention(RetentionPolicy.CLASS) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) public @interface NotProguard { }
NotProguard 是個編譯時註解,不會對運行時性能有任何影響。可修飾類、方法、構造函數、屬性。maven
# Keep annotated by NotProguard -keep @cn.trinea.android.lib.annotation.NotProguard class * {*;} -keep,allowobfuscation @interface cn.trinea.android.lib.annotation.NotProguard -keepclassmembers class * { @cn.trinea.android.lib.annotation.NotProguard *; }
解決方法2:函數
## keep 不想要混淆的類 -keep class com.xx.xx.base.utils.ProguardKeep {*;} -keep class * implements com.xx.xx.base.utils.ProguardKeep {*;} /** * 實現這個接口的類不會進行混淆 * proguard keep */ public interface ProguardKeep { }
sourceSets { main { jniLibs.srcDir 'jniLibs' } }
//配置keystore簽名 signingConfigs { release { storeFile file("KeyStore") storePassword "98765432" keyAlias "lyly" keyPassword "98765432" } } appbuildTypes { debug { signingConfig signingConfigs.release } release { signingConfig signingConfigs.release } }
這樣編譯出來的debug版本直接用的是正式簽名。性能
public class Rom { private Rom() { //no instance } /** * 是不是Oppo */ public static final boolean IS_OPPO; /** * 是不是Vivo */ public static final boolean IS_VIVO; /** * 是不是華爲,注意不包括華爲榮耀 */ public static final boolean IS_HUAWEI; /** * 是不是華爲榮耀 */ public static final boolean IS_HUAWEI_HONOR; /** * 是不是三星 */ public static final boolean IS_SAMSUNG; /** * 是不是努比亞 */ public static final boolean IS_NUBIA; static { final String brand = Build.BRAND.toUpperCase(); IS_OPPO = brand.equalsIgnoreCase("OPPO"); IS_VIVO = brand.equalsIgnoreCase("VIVO"); IS_HUAWEI = brand.equalsIgnoreCase("HUAWEI"); IS_HUAWEI_HONOR = brand.contains("HONOR"); IS_SAMSUNG = brand.contains("SAMSUNG"); IS_NUBIA = brand.contains("NUBIA"); } }
if(isRunAlone.toBoolean()){ apply plugin: 'com.android.application' }else{ apply plugin: 'com.android.library' }