在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"            }        }    }}

更多相关文章

  1. Android布局文件的属性值解析
  2. Android LinearLayout的android:layout_weight属性
  3. Android 中自定义属性(attr.xml,TypedArray)的使用
  4. Android 防界面劫持方案,无视Android系统版本限制,无需操作栈
  5. Android中UI组件android:layout_gravity属性的使用

随机推荐

  1. HTML中表格和表单的应用实例 (MD)
  2. CSS选择器优先级、模块化与伪类选择器的
  3. 学web前端有什么计划?
  4. NA公链(Nirvana)NAC公链独步公链江湖
  5. 助力你的年度大戏:“金三银四”的折腾
  6. “金三银四”的折腾之聊一聊面试
  7. 高效、易用、功能强大的 api 管理平台
  8. 京东技术:用最小的图片格式,打造最优的用户
  9. Nginx架构详解:nginx 的安装和配置
  10. 强大的开源企业级数据库监控利器Lepus