Android:在Gradle构建脚本中使用packageNameSuffix时,SearchableInfo为null
作者:互联网
如果我在项目的Gradle构建脚本中使用packageNameSuffix,则在SearchView初始化期间,方法getSearchableInfo始终返回null.
SearchView初始化:
final SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(componentName);
mSearchView.setSearchableInfo(info);
项目的build.gradle:
android {
[...]
buildTypes {
debug {
packageNameSuffix ".debug"
versionNameSuffix "-debug"
signingConfig signingConfigs.debug
}
[...]
}
}
如果未使用包后缀,则给定的componentName为ComponentInfo {com.example.android/com.example.android.MapActivity},并且SearchView及其关联的RecommendationionsProvider可以正常工作.
但是,如果packageNameSuffix设置为“ .debug”,则给定的componentName为ComponentInfo {com.example.android.debug / com.example.android.MapActivity},并且SearchManager返回null,而不是返回相应的SearchableInfo对象.
有谁知道如何从SearchManager获取正确的SearchableInfo?谢谢!
[编辑]
尤金·马蒂诺夫(Eugen Martinov)在评论中提到,此行为可能需要对不正确或缺少的权限进行更名.但是我还配置了一个与构建类型相关的权限命名,为了简单起见,我在最初的文章中将其省略.
项目的build.gradle:
android {
[...]
sourceSets {
debug {
java.srcDirs = [
'src/main/java'
]
java.srcDirs = [
'src/debug/res',
'src/main/res'
]
}
release {
java.srcDirs = [
'src/main/java'
]
java.srcDirs = [
'src/release/res',
'src/main/res'
]
}
[...]
}
}
src / debug / res / values / build-config.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="cfg_app_name">App - Debug</string>
<string name="cfg_authorities">com.example.debug.SuggestionsProvider</string>
<string name="cfg_maps_key"><!-- some key --></string>
</resources>
src / release / res / values / build-config.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="cfg_app_name">App</string>
<string name="cfg_authorities">com.example.SuggestionsProvider</string>
<string name="cfg_maps_key"><!-- some other key --></string>
</resources>
src / main / res / xml / searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/action_search_hint"
android:label="@string/cfg_app_name"
android:includeInGlobalSearch="false"
android:queryAfterZeroResults="true"
android:searchSuggestAuthority="@string/cfg_authorities"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestThreshold="3" />
在同一设备上安装调试(带有packageNameSuffix选项)和发行版apk均可.我没有收到失败[INSTALL_FAILED_CONFLICTING_PROVIDER]之类的错误.但是,正如已经说过的那样,SearchableInfo为null.
不带packageNameSuffix选项安装两个apk都会导致以下错误:失败[INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]-安装失败,因为该设备已经具有带有相同软件包但签名不同的应用程序.
还是我在这里想念什么?
[/编辑]
解决方法:
我使用的产品口味会完全改变包装名称.我发现通过对我的搜索活动使用ComponentName(Context pkg,Class<?> cls)构造函数,我得到了一个有效的SearchableInfo.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(new ComponentName(this, SearchActivity.class));
我还必须调整提供程序以使用其他程序包,以便我的可搜索xml位于每个flavor目录中,并且该提供程序也列在每种flavor的清单中.
这是我的目录结构:
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
flavour1 / 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>
flavour1 / 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>
flavour2 / 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>
flavour2 / 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>
标签:searchable,build-gradle,gradle,android 来源: https://codeday.me/bug/20191030/1964768.html