Android: SearchableInfo is null when using packageNameSuffix in Gradle build script

前端 未结 1 910
执念已碎
执念已碎 2021-01-14 17:37

I encountered that the method getSearchableInfo always returns null during SearchView initialization if I use the packageNameSuffix in

1条回答
  •  清歌不尽
    2021-01-14 17:47

    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

    
        
            
        
        
            
                
            
            
        
    
        
    

    flavor1/AndroidManifest.xml

    
        
    
    

    flavor1/res/xml/search.xml

    
    
    

    flavor2/AndroidManifest.xml

    
        
    
    

    flavor2/res/xml/search.xml

    
    
    

    0 讨论(0)
提交回复
热议问题