这两天为一个App打包准备发布上架,有几十个渠道,须要为每一个渠道做一些修改,好比应用名称图标,如今把过程当中遇到的问题及解决方法记录下来,以备后需。java
如下是须要修改的地方:android
之前也作过相似的修改包名之类的字符串这类的小改动,问题不大,此次遇到的问题是, 须要为各个渠道配置不一样的应用图标和启动页,这是之前没有作过的。下面是每一个点解决方法。app
一、修改包名。gradle
修改包名是这四个中最简单的,只须要在 productFlavors 中 的每一个渠道里重写 applicationId:ui
productFlavors { hello{ applicationId "com.xxx.xxxx" } }
二、渠道商信息server
不一样渠道商信息的配置其实和修改包名差很少,不一样的是它的格式是,好比配置一个渠道商ID,用buildConfigField "int", "AGENT_ID", "1",这样编译后在BuildConfig文件下生成:public static final int AGENT_ID = 1;而后你就能够BuildConfig.AGENT_ID 这样引用。渠道商名称的话用buildConfigField "String", "AGENT_NAME", "\"xx\""。这样生成public static final String AGENT_NAME = "xx";xml
Ahello { applicationId "com.xxx.xxxx"//修改包名 buildConfigField "int", "AGENT_ID", "1"//渠道商ID buildConfigField "String", "AGENT_NAME", "\"Ahello\""//渠道商名称 }
三、修改应用名blog
修改应用名就不像上面那么简单,不过查了一下也不难。使用的是:resValue "string", "你要设置的key", "你要设置的值"图片
resValue "string", "app_name", "Ahello"
须要注意的是这个是resValue 是增长一个值,若是你在string.xml中已经有一个app_name,请先删除,不然编译不过。资源
Ahello { applicationId "com.xxx.xxxx"//修改包名 buildConfigField "int", "AGENT_ID", "1"//渠道商ID buildConfigField "String", "AGENT_NAME", "\"Ahello\""//渠道商名称 resValue "string", "app_name", "Ahello"//应用名 resValue "string", "copyright", "Ahello @ 2018" resValue "string", "customer_server_phone_number", "财富热线:110110" }
四、修改图标、图片资源
修改图片资源花费的时间最多,我先想到的两种方案,第一把要修改的图标按渠道商命名,而后所有丢到drawable里。而后修改AndroidManifest.xml里android:icon="${app_icon}"
<application android:name="com.xxx.xxx" android:allowBackup="true" android:icon="${app_icon}" android:label="@string/app_name" android:supportsRtl="false" tools:replace="android:supportsRtl,android:label" android:theme="@style/AppTheme.NoActionBar">
在gradle里则加manifestPlaceholders = [app_icon: "@drawable/logo_xxxx"]
Ahello { applicationId "com.xxx.xxxx"//修改包名 buildConfigField "int", "AGENT_ID", "1"//渠道商ID buildConfigField "String", "AGENT_NAME", "\"Ahello\""//渠道商名称 resValue "string", "app_name", "Ahello"//应用名 resValue "string", "copyright", "Ahello @ 2018" resValue "string", "customer_server_phone_number", "财富热线:110110" manifestPlaceholders = [app_icon: "@drawable/logo_xxxx"]//应用图标 }
可是这种方法有一个显而易见的缺点,就是A渠道包里凭空多了几十个其余渠道包的图片,形成了apk包过大,这不是我想要的结果。
而后第二种方法是,为不一样的渠道拉分支,这样能够避免apk过大问题,而且以上这些配置均可以省,可是拉分支就不能批量打包,一个个打太麻烦了。果断放弃。
而后问了朋友有没有解决方法,朋友能够分渠道建文件夹,把图片同一命名分别放到各渠道文件夹,而后gradle里写sourceSets
Ahello { applicationId "com.xxx.xxxx"//修改包名 sourceSets { Ahello { res.srcDirs = ['src_custom/Ahello/res'] } } }
这种方法能够有效解决换图片问题,同时,还能够替换assets文件夹文件,甚至java类文件
Ahello { applicationId "com.xxx.xxxx"//修改包名 sourceSets { Ahello { res.srcDirs = ['src_custom/Ahello/res']//替换res文件 rassets.srcDirs = ['src_custom/Ahello/assets']//替换assets文件 java.srcDirs = ['src_custom/Ahello/java']//替换java类文件 } } }
因此最后的完整修改是:
Ahello { applicationId "com.xxx.xxxx"//修改包名 buildConfigField "int", "AGENT_ID", "1"//渠道商ID buildConfigField "String", "AGENT_NAME", "\"Ahello\""//渠道商名称 resValue "string", "app_name", "Ahello"//应用名 resValue "string", "copyright", "Ahello @ 2018" resValue "string", "customer_server_phone_number", "财富热线:110110" manifestPlaceholders = [app_icon: "@drawable/logo_xxxx"]//应用图标 sourceSets { Ahello { res.srcDirs = ['src_custom/Ahello/res']//替换res文件 rassets.srcDirs = ['src_custom/Ahello/assets']//替换assets文件 java.srcDirs = ['src_custom/Ahello/java']////替换类文件 } } }
我以为每一个渠道都写一个sourceSets,再从新写渠道名,这样写有点麻烦,就把sourceSets这部分单独拿出来放到defaultConfig里。固然这两种写法各有缺点,看我的习惯。
这里有个小插曲,我在为不一样渠道建立文件时,无心发现android studio已经为上述问题提供了解决方案。具体是这样的:项目右键-->new-->Android Resource Directory.
as已经内置了不一样的类型可供选择,而且能够选择渠道,省去了gradle里手动写配置。但也有一个问题,就是建立的文件夹跟main是同一级的,像咱们这样有几十个渠道,就会显得很混乱。
因此我最后没有采用as内置的方法,而用上面一种。
第一次写,写得有点乱,就这样。