Android中利用productFlavors配置多渠道
阿新 • • 發佈:2018-12-15
build.gradle(app)設定
android { flavorDimensions "default" productFlavors{ //不同渠道 360{ dimension "default" //替換AndroidManifest中的值 manifestPlaceholders = [channel_value:"360"] } qq{ dimension "default" //替換AndroidManifest中的值 manifestPlaceholders = [channel_value:"qq"] } } }
AndroidMainfest設定
<application
...
<meta-data android:name="CHANNEL_VALUE" android:value="${channel_value}"/>
...
</application>
程式碼中獲取渠道值
/** * 從Manifest中獲取meta-data值 * @param context * @param key * @return */ public static String getMetaData(Context context,String key) { String value = null; try { ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo( context.getPackageName(), PackageManager.GET_META_DATA); value = appInfo.metaData.getString(key); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (value==null) value=""; return value; }