Skip to content Skip to sidebar Skip to footer

Android: Searchableinfo Is Null When Using Packagenamesuffix In Gradle Build Script

I encountered that the method getSearchableInfo always returns null during SearchView initialization if I use the packageNameSuffix in the project's Gradle build script. SearchView

Solution 1:

I am using product flavors that completely changes the package name. I found that by using the ComponentName(Context pkg, Class<?> cls) constructor for my search activity I get a valid SearchableInfo.

SearchManagersearchManager= (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfosearchableInfo= searchManager.getSearchableInfo(newComponentName(this, SearchActivity.class));

I also had to adjust my provider to use a different package so my searchable xml is in each flavor directory and the provider is listed in the manifest for each flavor as well. This is my directory structure:

srcmain
    AndoridManifest.xml
  flavor1
    res
      xml
        search.xml
    AndroidManifest.xml
  flavor2
    res
      xml
        search.xml
    AndroidManifest.xml

main/AndroidManifest.xml

<applicationandroid:icon="@drawable/ic_launcher"android:logo="@drawable/actionbar_icon"android:label="@string/app_name"android:name=".App"android:allowBackup="true"android:theme="@style/AppTheme"><activityandroid:name=".ui.MainActivity"android:launchMode="singleTask"><meta-dataandroid:name="android.app.default_searchable"android:value=".ui.activity.SearchActivity"/></activity><activityandroid:name=".ui.SearchActivity"android:launchMode="singleTop"><intent-filter><actionandroid:name="android.intent.action.SEARCH" /></intent-filter><meta-dataandroid:name="android.app.searchable"android:resource="@xml/search"/></activity><providerandroid:authorities=".provider.SuggestionProvider"android:name="com.example.main.provider.SuggestionProvider"android:exported="false"android:enabled="true"
        />
</applicaiton

flavor1/AndroidManifest.xml

<application><providertools:replace="android:authorities"android:authorities="com.example.flavor1.provider.SuggestionProvider"android:name="com.example.main.provider.SuggestionProvider"android:exported="false"android:enabled="true"
        /></application>

flavor1/res/xml/search.xml

<searchablexmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/app_name"android:includeInGlobalSearch="false"android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"android:searchMode="queryRewriteFromText"android:searchSuggestAuthority="com.example.flavor1.provider.SuggestionProvider"android:searchSuggestSelection="title LIKE ?"android:searchSuggestThreshold="0"
    ></searchable>

flavor2/AndroidManifest.xml

<application><providertools:replace="android:authorities"android:authorities="com.example.flavor2.provider.SuggestionProvider"android:name="com.example.main.provider.SuggestionProvider"android:exported="false"android:enabled="true"
        /></application>

flavor2/res/xml/search.xml

<searchablexmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/app_name"android:includeInGlobalSearch="false"android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"android:searchMode="queryRewriteFromText"android:searchSuggestAuthority="com.example.flavor2.provider.SuggestionProvider"android:searchSuggestSelection="title LIKE ?"android:searchSuggestThreshold="0"
    ></searchable>

Post a Comment for "Android: Searchableinfo Is Null When Using Packagenamesuffix In Gradle Build Script"