pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 Gradle 中使用構(gòu)建類型在一臺(tái)設(shè)備上運(yùn)行使用

Using build types in Gradle to run same app that uses ContentProvider on one device(在 Gradle 中使用構(gòu)建類型在一臺(tái)設(shè)備上運(yùn)行使用 ContentProvider 的相同應(yīng)用程序)
本文介紹了在 Gradle 中使用構(gòu)建類型在一臺(tái)設(shè)備上運(yùn)行使用 ContentProvider 的相同應(yīng)用程序的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我已設(shè)置 Gradle 以將包名稱后綴添加到我的調(diào)試應(yīng)用程序中,這樣我就可以在一部手機(jī)上擁有我正在使用的發(fā)布版本和調(diào)試版本.我引用了這個(gè):http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types

I have set up Gradle to add package name suffix to my debug app so I could have release version that I'm using and debug version on one phone. I was referencing this: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types

我的 build.gradle 文件如下所示:

My build.gradle file looks like this:

...
android
{
    ...
    buildTypes
    {
        debug
        {
            packageNameSuffix ".debug"
            versionNameSuffix " debug"
        }
    }
}

在我開始在我的應(yīng)用程序中使用 ContentProvider 之前,一切正常.我明白了:

Everything works fine until I start using a ContentProvider in my app. I get:

Failure [INSTALL_FAILED_CONFLICTING_PROVIDER]

我了解這是因?yàn)閮蓚€(gè)應(yīng)用(發(fā)布和調(diào)試)正在注冊(cè)相同的 ContentProvider 權(quán)限.

I understand that this happens because two apps (release and debug) are registering same ContentProvider authority.

我看到了解決這個(gè)問題的一種可能性.如果我理解正確,您應(yīng)該能夠在構(gòu)建時(shí)指定要使用的不同文件.然后我應(yīng)該能夠?qū)⒉煌臋?quán)限放在不同的資源文件中(并且從清單設(shè)置權(quán)限作為字符串資源)并告訴 Gradle 使用不同的資源進(jìn)行調(diào)試構(gòu)建.那可能嗎?如果是,那么任何關(guān)于如何實(shí)現(xiàn)這一目標(biāo)的提示都會(huì)很棒!

I see one possibility to solve this. If I understand correctly, you should be able to specify different files to use when building. Then I should be able to put different authorities in different resource files (and from Manifest set authority as string resource) and tell Gradle to use different resource for debug build. Is that possible? If yes then any hints on how to achieve that would be awesome!

或者也許可以使用 Gradle 直接修改 Manifest?任何其他關(guān)于如何在一臺(tái)設(shè)備上運(yùn)行與 ContentProvider 相同的應(yīng)用程序的解決方案都是受歡迎的.

Or maybe it's possible to directly modify Manifest using Gradle? Any other solution on how to run same app with ContentProvider on one device is always welcome.

推薦答案

沒有一個(gè)現(xiàn)有的答案讓我滿意,但是 Liberty 很接近.所以這就是我的做法.首先,目前我正在使用:

None of existing answers satisfied me, however Liberty was close. So this is how am I doing it. First of all at the moment I am working with:

  • Android Studio 測(cè)試版 0.8.2
  • Gradle 插件 0.12.+
  • Gradle 1.12

我的目標(biāo)是使用相同的 ContentProvider 在同一設(shè)備上運(yùn)行 Debug 版本和 Release 版本.

My goal is to run Debug version along with Release version on the same device using the same ContentProvider.

在您的應(yīng)用集后綴的 build.gradle 中用于調(diào)試構(gòu)建:

In build.gradle of your app set suffix for Debug build:

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}


AndroidManifest.xml 文件中設(shè)置 ContentProviderandroid:authorities 屬性:


In AndroidManifest.xml file set android:authorities property of your ContentProvider:

<provider
    android:name="com.example.app.YourProvider"
    android:authorities="${applicationId}.provider"
    android:enabled="true"
    android:exported="false" >
</provider>


在您的 code 中設(shè)置 AUTHORITY 屬性,該屬性可在您的實(shí)施中需要時(shí)使用:


In your code set AUTHORITY property that can be used wherever needed in your implementation:

public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";

提示:之前是BuildConfig.PACKAGE_NAME

我將再次從我當(dāng)前的設(shè)置開始:

Once again I will start with my current setup:

  • Android Studio 測(cè)試版 0.9.2
  • Gradle 插件 0.14.1
  • Gradle 2.1

基本上,如果您需要為不同的構(gòu)建自定義一些值,您可以從 build.gradle 文件中完成:

Basically, if you need to customise some values for different builds you can do it from the build.gradle file:

  • 使用 buildConfigFieldBuildConfig.java 類中訪問它
  • 使用 resValue 從資源中訪問它,例如@string/your_value
  • use buildConfigField to access it from the BuildConfig.java class
  • use resValue to access it from resources e.g. @string/your_value

作為資源的替代方案,您可以創(chuàng)建單獨(dú)的 buildType 或風(fēng)味目錄并覆蓋其中的 XML 或值.但是,我不會(huì)在下面的示例中使用它.

As an alternative for resources, you can create separate buildType or flavour directories and override XMLs or values within them. However, I am not going to use it in example below.

build.gradle 文件中添加以下內(nèi)容:

In build.gradle file add the following:

defaultConfig {
    resValue "string", "your_authorities", applicationId + '.provider'
    resValue "string", "account_type", "your.syncadapter.type"
    buildConfigField "String", "ACCOUNT_TYPE", '"your.syncadapter.type"'
}

buildTypes {
    debug {
        applicationIdSuffix ".debug"
        resValue "string", "your_authorities", defaultConfig.applicationId + '.debug.provider'
        resValue "string", "account_type", "your.syncadapter.type.debug"
        buildConfigField "String", "ACCOUNT_TYPE", '"your.syncadapter.type.debug"'
    }
}

您將在 BuildConfig.java 類中看到結(jié)果

You will see results in BuildConfig.java class

public static final String ACCOUNT_TYPE = "your.syncadapter.type.debug";

并在 build/generated/res/generated/debug/values/generated.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Automatically generated file. DO NOT MODIFY -->
    <!-- Values from default config. -->
    <item name="account_type" type="string">your.syncadapter.type.debug</item>
    <item name="authorities" type="string">com.example.app.provider</item>

</resources>


在您的 authenticator.xml 中使用 build.gradle 文件中指定的資源


In your authenticator.xml use resource specified in build.gradle file

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
                       android:accountType="@string/account_type"
                       android:icon="@drawable/ic_launcher"
                       android:smallIcon="@drawable/ic_launcher"
                       android:label="@string/app_name"
/>


在您的 syncadapter.xml 中再次使用相同的資源,并且 @string/authorities


In your syncadapter.xml use the same resource again and @string/authorities too

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
              android:contentAuthority="@string/authorities"
              android:accountType="@string/account_type"
              android:userVisible="true"
              android:supportsUploading="false"
              android:allowParallelSyncs="false"
              android:isAlwaysSyncable="true"
        />

提示:自動(dòng)補(bǔ)全(Ctrl+Space)不適用于這些生成的資源,因此您必須手動(dòng)輸入它們

Tip: autocompletion(Ctrl+Space) does not work for these generated resource so you have to type them manually

這篇關(guān)于在 Gradle 中使用構(gòu)建類型在一臺(tái)設(shè)備上運(yùn)行使用 ContentProvider 的相同應(yīng)用程序的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現(xiàn) IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當(dāng)前風(fēng)味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復(fù)“意外元素lt;查詢gt;在“清單中找到錯(cuò)誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風(fēng)味庫的多風(fēng)味應(yīng)用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運(yùn)行時(shí)有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 成都中天自动化控制技术有限公司 | 金属抛光机-磁悬浮抛光机-磁力研磨机-磁力清洗机 - 苏州冠古科技 | 苏州注册公司_苏州代理记账_苏州工商注册_苏州代办公司-恒佳财税 | GEDORE扭力螺丝刀-GORDON防静电刷-CHEMTRONICS吸锡线-上海卓君电子有限公司 | 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 语料库-提供经典范文,文案句子,常用文书,您的写作得力助手 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 健康管理师报名入口,2025年健康管理师考试时间信息网-网站首页 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 全自动不干胶贴标机_套标机-上海今昂贴标机生产厂家 | 拉力机-万能试验机-材料拉伸试验机-电子拉力机-拉力试验机厂家-冲击试验机-苏州皖仪实验仪器有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 振动时效_振动时效仪_超声波冲击设备-济南驰奥机电设备有限公司 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 薄壁轴承-等截面薄壁轴承生产厂家-洛阳薄壁精密轴承有限公司 | 微动开关厂家-东莞市德沃电子科技有限公司 | 重庆小面培训_重庆小面技术培训学习班哪家好【终身免费复学】 | 上海物流公司,上海货运公司,上海物流专线-优骐物流公司 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 解放卡车|出口|济南重汽|报价大全|山东三维商贸有限公司 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 蔬菜配送公司|蔬菜配送中心|食材配送|饭堂配送|食堂配送-首宏公司 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 双菱电缆-广州电缆厂_广州电缆厂有限公司 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 即用型透析袋,透析袋夹子,药敏纸片,L型涂布棒-上海桥星贸易有限公司 | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 |