Android(安卓)使用grade实现Android(安卓)项目debug版与release版共存
在Android 项目中,默认debug版与release版的包名相同,从而导致debug版与release版两者不能共存。为了方便开发,可以通过gradle实现让两者在一台手机上共存
原文地址:http://blog.csdn.net/lj402159806/article/details/54955431
配置app目录下的build.gradle文件
android { ...... buildTypes { ...... debug { //为debug版本的包名添加.debug后缀 applicationIdSuffix ".debug" ...... } }}
在debug节点里添加这个配置后,debug版本的apk 包名会自动添加.debug后缀。比如原本包名为com.example.application的应用,debug包名为com.example.application.debug
权限重复的问题
如果项目中使用了第三方库,而在ManiFest中声明了权限,例如个推
<permission android:name="getui.permission.GetuiService.package_name" android:protectionLevel="normal"/>
android 5.0以上安装应用时会报duplicate permission exception,所以要保证debug和release安装包的permission name 不同,因此我们可以使用applicationId字段,使用方式如下。
"getui.permission.GetuiService.${applicationId}" android:protectionLevel="normal"/>
provider authorities
<provider android:name="com.igexin.download.DownloadProvider" android:authorities="downloads.package_name" android:exported="true" android:process=":pushservice"/>
同样也可以使用applicationId字段来替换
android:authorities="downloads.${applicationId}"
使用manifestPlaceholders属性来替换
但是不是所有的属性都能使用applicationId来替换,比如融云这样的第三方库
可以看到如下data节点内的host=”package_name” 就不能使用applicationId字段来替换
<activity android:name=".activity.navigation.ConversationListActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:host="package_name" android:pathPrefix="/conversationlist" android:scheme="rong"/> intent-filter> activity>
因此我们可以用manifestPlaceholders属性, manifestPlaceholders顾名思义manifest占位符,上面说的饿${applicationId }就是grade自带的manifest占位符,当然我们也可以自定义manifest占位符
分别在release 和debug节点下添加manifestPlaceholders属性如下:
android { ...... buildTypes { ...... release { ....... manifestPlaceholders = [ APP_NAME : "@string/app_name", APPLICATION_ID: "@string/application_id" ] } debug { //为debug版本的包名添加.debug后缀 applicationIdSuffix ".debug" manifestPlaceholders = [ APP_NAME : "@string/app_name_debug", APPLICATION_ID: "@string/application_id_debug" ] ...... } }}
在项目的values/strings.xml文件里添加如下:
<string name="app_name">MyApplicationstring> <string name="app_name_debug">MyApplicationDebugstring> <string name="application_id">com.example.myapplicationstring> <string name="application_id_debug">com.example.myapplication.debugstring>
然后将data节点内的host=”package_name”替换为host=”${APPLICATION_ID}”
<activity android:name=".activity.navigation.ConversationListActivity" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:host="${APPLICATION_ID}" android:pathPrefix="/conversationlist" android:scheme="rong"/> intent-filter> activity>
这样就能在打包release版本时包名使用release的包名,debug版本使用debug包名
app_name也同理,可以将android:label里的内容替换成manifestPlaceHolder变量,如下所示:
android:label="${APP_NAME}"
动态打包so包
apk的大小永远式开发者头疼的问题,而so包就是apk安装包体积过大的主要原因之一,因此很多开发者都会将一些用不到so包去除,以减小apk体积,比如x86的so包,但是如果你需要使用A n droid 官方提供的模拟器来测试你的应用的话,那x86的so包是必不可少,解决方案如下:
就是release版本打包时不包含x86的so包,减少正式版apk的体积,debug版本者包含x86的so包,方便在官方模拟器上测试。
android { ...... buildTypes { ...... release { ....... //设置release版本只包含armeabi和armeabi-v7a的so包 ndk { abiFilters "armeabi", "armeabi-v7a" } } debug { ...... //设置debug版本包含x86的so文件 ndk { abiFilters "armeabi", "armeabi-v7a", "x86" } } }}
更多相关文章
- Android(安卓)Studio 解决The SDK platform-tools is too old问
- android字体大小的设置
- Android(安卓)启动应用程序方式
- Android应用商店——Splash页面的实现,Android运行时权限的使用
- 使用AudioTrack播放PCM音频数据(android)
- Android高手进阶教程(四)之----Android(安卓)中自定义属性(attr.
- Android里面常用的Drawable的使用
- Android:Field can be converted to a local varible.
- Android之MPAndroidChart库使用说明(柱状图、折线图、饼图和组合