安卓:在使用摇篮打造packageNameSuffix脚本时SearchableInfo为null [英] Android: SearchableInfo is null when using packageNameSuffix in Gradle build script

查看:198
本文介绍了安卓:在使用摇篮打造packageNameSuffix脚本时SearchableInfo为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的方法 getSearchableInfo 总是在搜索查看如果我用 packageNameSuffix 在项目的摇篮构建脚本。

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

搜索查看初始化:

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
        }
        [...]
    }
}

如果不使用包的后缀,给定的组件名 ComponentInfo {} com.example.android/com.example.android.MapActivity 搜索查看,以及与其相关的 SuggestionsProvider 做工精细。

If the package suffix is not used, the given componentName is ComponentInfo{com.example.android/com.example.android.MapActivity} and the SearchView as well as its associated SuggestionsProvider work fine.

但是,如果 packageNameSuffix 设置为调试,给定的组件名 ComponentInfo {com.example.android.debug / com.example.android.MapActivity} SearchManager的收益的,而不是返回各自的 SearchableInfo 对象。

But if packageNameSuffix is set to ".debug", the given componentName is ComponentInfo{com.example.android.debug/com.example.android.MapActivity} and the SearchManager returns null, instead of returning the respective SearchableInfo object.

有谁知道如何得到正确的 SearchableInfo SearchManager的?谢谢!

Does anyone know how to get the right SearchableInfo from the SearchManager? Thanks!

尤金·马丁诺夫在评论中提到这behaviur可能有做的不当或缺失当局重命名的东西。但我也配置了当局的生成依赖型的命名,我在战后初期为简单起见,省略。

Eugen Martinov mentioned in the comments that this behaviur could have to do something with an improper or missing authorities renaming. But i also configured a build type dependent naming of the authorities, that i omitted in the initial post for the sake of simplicity.

项目的的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 /调试/ RES /价值/构建-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 /发行/ RES /价值/构建-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 是一个错误即可。

Installing both the debug (with the packageNameSuffix option) and the release apk on the same device works. I don't get an error like Failure [INSTALL_FAILED_CONFLICTING_PROVIDER]... But as already said, SearchableInfo is null then.

同时安装APK withouth的在 packageNameSuffix 选项通向以下错误:失败[INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES] - 安装失败,因为该设备已经有一个应用程序同样的包,但不同的签名。

Installing both apk withouth the packageNameSuffix option leads into the following error: Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES] - Installation failed since the device already has an application with the same package but a different signature.

或者我失去了一些东西?

Or am i missing something here?

[/编辑]

推荐答案

我使用的产品口味是完全改变了包名。我发现,通过使用组件名(上下文PKG,类&LT;&GT; CLS)。构造我的搜索活动,我得到一个有效的SearchableInfo

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));

我也不得不调整我的供应商,所以我的搜索XML是每个味道目录和供应商在清单中列出每个味道,以及使用不同的包。
这是我的目录结构:

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

主/ 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中

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

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中

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

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>

这篇关于安卓:在使用摇篮打造packageNameSuffix脚本时SearchableInfo为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆