I encountered that the method getSearchableInfo
always returns null during SearchView
initialization if I use the packageNameSuffix
in
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.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(new ComponentName(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:
src
main
AndoridManifest.xml
flavor1
res
xml
search.xml
AndroidManifest.xml
flavor2
res
xml
search.xml
AndroidManifest.xml
main/AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:logo="@drawable/actionbar_icon"
android:label="@string/app_name"
android:name=".App"
android:allowBackup="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:launchMode="singleTask">
<meta-data
android:name="android.app.default_searchable"
android:value=".ui.activity.SearchActivity"/>
</activity>
<activity android:name=".ui.SearchActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/search"/>
</activity>
<provider
android:authorities=".provider.SuggestionProvider"
android:name="com.example.main.provider.SuggestionProvider"
android:exported="false"
android:enabled="true"
/>
</applicaiton
flavor1/AndroidManifest.xml
<application>
<provider
tools: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
<searchable xmlns: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>
<provider
tools: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
<searchable xmlns: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>