代码之家  ›  专栏  ›  技术社区  ›  j2emanue

如何使用XML中的Gradle Placeholder ApplicationId创建唯一的ContentProvider权限

  •  2
  • j2emanue  · 技术社区  · 6 年前

    我有以下可搜索的XML声明:

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:label="@string/app_name"
        android:voiceSearchMode="showVoiceSearchButton"
        android:includeInGlobalSearch="true"
        android:searchMode="queryRewriteFromText"
        android:searchSuggestAuthority="com.mypackage.MyRecentSuggestionsProvider"
        android:searchSuggestSelection=" ?"
        android:hint="@string/search_hint">
    
    </searchable>
    

    现在是ContentProvider本身:

    package com.mypackage.contentProviders
    
    import android.content.SearchRecentSuggestionsProvider
    import com.mypackage.BuildConfig
         class MySuggestionsProvider : SearchRecentSuggestionsProvider() {
    
            val AUTHORITY = BuildConfig.APPLICATION_ID + ".MySuggestionsProvider"
            val MODE = SearchRecentSuggestionsProvider.DATABASE_MODE_QUERIES
    
           init{
                setupSuggestions(AUTHORITY, MODE)
            }
        }
    

    现在的问题是,我在Gradle中有3种风格,所以ContentProvider类中定义权限的行使用应用程序ID使其唯一,这样所有3种风格都可以同时存在于设备上。我记得,两个应用程序不能有重复的权限。

    现在讨论我的问题:我不想重写每种风格的searchable.xml,只想为每种风格声明另一个权限。我宁愿这样做:

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:label="@string/app_name"
        android:voiceSearchMode="showVoiceSearchButton"
        android:includeInGlobalSearch="true"
        android:searchMode="queryRewriteFromText"
        android:searchSuggestAuthority="{applicationId}.MyRecentSuggestionsProvider"
        android:searchSuggestSelection=" ?"
        android:hint="@string/search_hint">
    
    </searchable>
    

    我们经常这样做,因为Android清单是一个占位符。是否无论如何,我不必为内容提供程序创建不同的XML声明,而是以某种方式使其每种风格都具有唯一性?

    1 回复  |  直到 6 年前
        1
  •  2
  •   CommonsWare    6 年前

    清单占位符不在资源中工作。但是,您可以:

    • 使用 resValue 在Gradle中,为基于风格的字符串资源定义不同的值,然后 android:searchSuggestAuthority 引用该字符串资源

    • 为每个风格设置源集,在每个风格中定义一个字符串资源,然后 Android:搜索建议授权 引用该字符串资源

    • 为每个口味设置源集,并具有不同的 searchable.xml 在每种情况下,这是你要避免的

    推荐文章