使用 Gradle 中的构建类型在一台设备上运行使用 ContentProvider 的相同应用程序 [英] Using build types in Gradle to run same app that uses ContentProvider on one device

查看:21
本文介绍了使用 Gradle 中的构建类型在一台设备上运行使用 ContentProvider 的相同应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置 Gradle 为我的调试应用程序添加包名称后缀,以便我可以在一部手机上使用我正在使用的发布版本和调试版本.我引用了这个: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"
        }
    }
}

一切正常,直到我开始在我的应用中使用 ContentProvider.我得到:

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

Failure [INSTALL_FAILED_CONFLICTING_PROVIDER]

我知道这是因为两个应用(发布和调试)注册了相同的 ContentProvider 权限.

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

我看到了解决这个问题的一种可能性.如果我理解正确,您应该能够在构建时指定要使用的不同文件.然后我应该能够将不同的权限放在不同的资源文件中(并且从清单中将权限设置为字符串资源)并告诉 Gradle 使用不同的资源进行调试构建.那可能吗?如果是,那么有关如何实现这一目标的任何提示都很棒!

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?关于如何在一台设备上使用 ContentProvider 运行相同应用的任何其他解决方案总是受欢迎的.

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.

推荐答案

没有一个现有的答案让我满意,但是 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

我的目标是使用相同的ContentProvider在同一台设备上运行Debug版本和Release版本.

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

在您的应用程序的 build.gradle 中为调试构建设置后缀:

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

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}


AndroidManifest.xml 文件中设置 android:authorities 你的 ContentProvider 属性:


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>


在您的代码中设置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

我将再次从我当前的设置开始:

Once again I will start with my current setup:

  • Android Studio 测试版 0.9.2
  • Gradle 插件 0.14.1
  • Gradle 2.1

基本上,如果您需要为不同的构建自定义一些值,您可以从 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

作为资源的替代方案,您可以创建单独的 buildType 或风味目录并覆盖其中的 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 文件中添加以下内容:

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 类中看到结果

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

提示:自动补全(Ctrl+Space)对这些生成的资源不起作用,因此您必须手动输入它们

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

这篇关于使用 Gradle 中的构建类型在一台设备上运行使用 ContentProvider 的相同应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆