使用在摇篮生成类型运行使用ContentProvider的一台设备上相同的应用程序一台、摇篮、应用程序、类型

2023-09-12 08:22:50 作者:╰つ◆ 尛懒男℃

我已经设置了摇篮到包名称后缀添加到我的调试应用程序,这样我就可以有版本,我使用的版本和调试版本上的一个电话。我引用这一点: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.

我看到一种可能性,以解决这个问题。如果我理解正确的话,你应该可以指定不同的文件建立时使用。然后,我应该能够把不同的部门在不同的资源文件(从清单一套权威字符串资源),并告诉摇篮使用不同的资源调试版本。那可能吗?如果是的话那么就如何实现这一目标的任何提示将真棒!

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!

或者,也许有可能使用摇篮清单直接修改?有关如何使用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.

推荐答案

无现有的答案能让我满意,但是自由接近。因此,这就是为什么我这样做。 首先,此刻我有工作:

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的工作室测试版0.8.2 在摇篮插件0.12。+ 在摇篮1.12

我的目标是与发布运行调试版本版本相同使用相同的设备的ContentProvider

在 build.gradle 您的应用程序设置后缀为调试生成的:

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

buildTypes {
    debug {
        applicationIdSuffix ".debug"
    }
}

在 AndroidManifest.xml中文件中设置安卓当局属性的的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>

Jetpack App Startup源码解析

在您的 code 设置管理​​局属性,可以用于任何需要在您的实现:

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的工作室测试版0.9.2 在摇篮插件0.14.1 在摇篮2.1

基本上,如果你需要为不同的构建可以从build.gradle文件做自定义一些值:

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

使用的 buildConfigField 的从 BuildConfig.java 类访问它 使用的 resValue 的,从资源,例如访问 @字符串/ 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或有香味的目录,并在其中覆盖XMLS或值。不过,我不打算使用它例如下面

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 类

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

和建立/生成/ RES /生成/调试/价值/ 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 再次使用相同的资源和 @字符串/部门的太

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 +空格键),这些生成的资源不工作,所以你必须将它们手动键入

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