Android Manifest with @String reference - specific

2020-04-12 09:45发布

问题:

I have this issue with the manifest.

Looks like this may be a dupe of : Using @string for android:authorities in a ContentProvider

I have a provider with separate authorities for different versions of the app (so that the different variations can be I store these authorities within the string folders of the difference target res folders.

My manifest looks as such:

   <provider android:authorities="@string/app_provider_auth" android:name="com.mecompany.myapp.provider.CachedFileProvider"/>

NOW, this works fine however I am seeing a Bad Manifest issue when it is installed on a 2.1 OS device. This is the issue as when I change it to a text string it works fine on 2.1.

I'm taking it that on 2.1 (7) or earlier the manifest does not allow you to reference strings from the resource files. So, can I create a separate manifest for version 7-, can I have a if statement in the manifest? or do I have to raise the minSDK (last resort)?

UPDATE:

OK on further searching seems like I might be able to set/switch the provider auth string using my Maven build. I already have a number of profiles and am overriding resource folders. So i am comfortable with the idea, however its the how I can't get my head around. something like

<replaceAuthority>${customerauthority}<replaceAuthority>

However I don't know what the tag is that I should use, or if the way i have hypothesized above is the way I need to go.

回答1:

To change provider from maven you can use <providerAuthorities> tag like here:

    <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>3.5.0</version>
        <extensions>true</extensions>
        <configuration>

            <!-- Установка версии в манифест -->
            <manifest>
                <versionName>${project.version.name}</versionName>
                <versionCode>${project.version.code}</versionCode>

                <!-- провайдер -->
                <providerAuthorities>
                    <property>
                        <name>.database.DatabaseProvider</name>
                        <value>${project.package}.database.DatabaseProvider</value>
                    </property>                        
                </providerAuthorities>

            </manifest>
        </configuration>
    </plugin>


回答2:

With a bit more help from here android-maven-plugin and resource filtering

here https://github.com/jayway/maven-android-plugin/pull/115

and @Konstantin Pridluda I came to an acceptable conclusion.

What I did was to create a new folder within parent project folder called manifests. Then created two sub folder customer1Manifest and customer2Manifest

Inside each of these folders i created a copy of the manifest file then replaced the @string reference with the appropriate hard string authorities. (the normal manifest file is the debug auth)

Then in the POM i switched out the manifests for the appropriate ones like this.

<profiles>
    <profile>
     <id>Customer1</id>
     <activation>
         <activeByDefault>true</activeByDefault>
     </activation>
     <properties>
  <customerManifest>../manifests/customer1Manifest/AndroidManifest.xml</customerManifest>
      </properties>
    </profile>
    <profile>
     <id>Customer2</id>
     <properties>
        <customerManifest>../manifests/customer2Manifest/AndroidManifest.xml</customerManifest>
      </properties>
      </profile>
</profiles>

then later on in the android-maven-plugin phase did this

 <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <extensions>true</extensions>

        <inherited>true</inherited>
        <configuration>
         <androidManifestFile>${customerManifest}</androidManifestFile>
            .........
.......
        </configuration>
</plugin>