久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

問題描述

我已設(shè)置 Gradle 以將包名稱后綴添加到我的調(diào)試應(yīng)用程序中,這樣我就可以在一部手機(jī)上擁有我正在使用的發(fā)布版本和調(diào)試版本.我引用了這個: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īng)用(發(fā)布和調(diào)試)正在注冊相同的 ContentProvider 權(quán)限.

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

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

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)于如何在一臺設(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.

推薦答案

沒有一個現(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 測試版 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 屬性,該屬性可在您的實施中需要時使用:


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 測試版 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 或值.但是,我不會在下面的示例中使用它.

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"
        />

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

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

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

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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;在“清單中找到錯誤?)
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)行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 国产精品18久久久 | 亚洲成人综合社区 | 激情欧美一区二区三区中文字幕 | 综合中文字幕 | 国产成人精品一区二区三 | 欧美久久久久久久久 | 午夜一区二区三区在线观看 | 欧美一区二区三区四区在线 | 中文字幕二区三区 | 久久手机在线视频 | 亚洲视频免费在线观看 | 情侣黄网站免费看 | 四虎影院免费在线 | 亚洲欧美日韩一区二区 | 日韩性生活网 | 北条麻妃一区二区三区在线视频 | 精品免费国产 | 一级片在线视频 | 91一区| 高清亚洲 | 欧美成人高清 | 精品一区在线看 | 亚洲成av人片在线观看 | 91精品国产一区二区三区香蕉 | 国产成人精品一区 | 欧美一区二区成人 | 羞羞羞视频 | 亚洲情综合五月天 | 狠狠色狠狠色综合日日92 | 亚洲黄色在线 | 黑色丝袜三级在线播放 | 99久久99热这里只有精品 | 成人影院在线视频 | 国产精品99久久免费观看 | 亚洲一区二区三区四区五区中文 | 亚洲精品视频二区 | 91麻豆产精品久久久久久夏晴子 | 久久精品国产清自在天天线 | 精品久久久久久亚洲精品 | 国产精品网页 | 中文字幕一区在线 |