android studio 多渠道打包,調試正式包,build.gradle解析
1,講解build.gradle檔案。1.1根目錄Android
1.1.1
defaultConfig是Android的根目錄,可以配置包名等資訊,若AndroidMainfest.xml也配置了,以defaultConfig的為準。
1.1.2
signingConfigs是Android的根目錄,可以配置簽名,如:
調試時若想直接用正式的簽名包可以在buildType裡配置。
buildTypes { debug { signingConfig signingConfigs.release } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }}
1.1.3
多渠道打包,修改AndroidMainFest 的meta-data值。
AndroidMainFest 配置如下:
${Value} 是動態配置的。
build.gradle配置如下
productFlavors { huawei { minSdkVersion 14 targetSdkVersion 21 manifestPlaceholders =[Value: huawei] } xiaomi { minSdkVersion 14 targetSdkVersion 21 manifestPlaceholders =[Value: xiaomi] }}
代碼中擷取AndroidMainnifest Value的值
//擷取meta欄位public static String getMetaString(Context con,String name){ ApplicationInfo ai = null; try { ai = con.getPackageManager().getApplicationInfo(con.getPackageName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } Bundle bundle = ai.metaData; return bundle.getString(name);}
打包時直接使用
最後貼上build.gradle檔案。
apply plugin: 'com.android.application'android { compileSdkVersion 21 buildToolsVersion 21.1.2 defaultConfig { applicationId caisheng.com.search minSdkVersion 16 targetSdkVersion 21 versionCode 1 versionName 1.0 } signingConfigs { //你自己的keystore資訊 release { keyAlias 'aolaigo' keyPassword '123456' storeFile file('E:/test.jks') storePassword '123456' } } lintOptions { checkReleaseBuilds false // Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found: abortOnError false } buildTypes { debug { signingConfig signingConfigs.release } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { huawei { minSdkVersion 14 targetSdkVersion 21 manifestPlaceholders =[Value: huawei] } xiaomi { minSdkVersion 14 targetSdkVersion 21 manifestPlaceholders =[Value: xiaomi] } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // compile (name:'cai',ext:'aar') // compile project(':cai') /* compile 'com.facebook.fresco:fresco:0.6.0' compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.github.liuguangqiang.swipeback:library:1.0.2@aar' compile 'com.sothree.slidinguppanel:library:3.1.1' compile 'me.dm7.barcodescanner:zbar:1.5'*/}