akshay bhange
akshay bhange

Reputation: 2504

search widget is not working in release apk

I'm having a strange issue(for me at least). my app works fine with debug apk. but when I create release apk then it shows error in searchwidget. I'm using support library 'com.android.support:appcompat-v7:21.0.3'

this is my code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.searchmenu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchItem = menu.findItem(R.id.searchwidget);
    final SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(searchItem);
    if(null!=searchManager ) {   
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    searchView.setIconifiedByDefault(false);

    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            Programs.this.adapter.getFilter().filter(newText);
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // this is your adapter that will be filtered
            Programs.this.adapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(textChangeListener);

    return super.onCreateOptionsMenu(menu);
}

my menu code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item 
    android:title="@string/search"
    android:id="@+id/searchwidget"
    app:showAsAction="always|collapseActionView"
    android:icon="@drawable/ic_action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"></item> 


</menu>

first I thought it is proguard problem therefore i used this

-keepclassmembers class android.support.v7.widget.SearchView{
}

but still same problem.

can anyone explain why this is happening?

Upvotes: 10

Views: 1094

Answers (2)

xnagyg
xnagyg

Reputation: 4931

You have to add

-keep class android.support.v7.widget.SearchView { *; }

to proguard-rules.pro file.

Somehow it is related to proguard obfuscation, probably a bug in SearchView. (some version affected, some not)

Upvotes: 26

codeKnight
codeKnight

Reputation: 103

disable the progaurd or make it as "false" from build.gradle

It will increase the size but will get the work done.

it worked for me

Upvotes: -4

Related Questions