0

I'm trying to add a search function to my project. When i run the code i got this problem. I saw solutions with proguard file by adding this line (first one ) but it still doesn't work. In other questions they made a mistake by putting android:showAsAction instead of app:showAsAction. In my case, it is ok but Android Studio does not recognize v7 package and the line is on red.

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

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


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


</menu>

proguard-rules.pro

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class android.support.v7.widget.SearchView { *; }

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.janazahapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.android.support:recyclerview-v7:29.0.3'
    implementation 'com.android.support:cardview-v7:29.0.3'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //noinspection GradleCompatible
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'com.google.firebase:firebase-core:11.8.0'
    implementation 'com.google.firebase:firebase-auth:19.3.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.material:material:1.0.0'
}
apply plugin: 'com.google.gms.google-services'

MainActivity.java (piece of code where i get the error on searchView.setOnQueryTextListener method)

 @Override
    public boolean  onCreateOptionsMenu(Menu menu){

            MenuInflater menuInflater = getMenuInflater();
            menuInflater.inflate(R.menu.menu,menu);
            MenuItem item = menu.findItem(R.id.search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

                @Override
                public boolean onQueryTextSubmit(String query) {

                    myAdapter.getFilter().filter(query);
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {

                    myAdapter.getFilter().filter(newText);
                    return false;
                }
        });
        return  true;
    }
NAIIFOS 7
  • 48
  • 6
  • Try using `app:actionLayout` instead. You'll need to make a layout with your `SearchView` in it, them use `getActionView` as usual. Since the search view is in a layout file, proguard won't remove it. – Nicolas Apr 18 '20 at 20:51
  • There is a stackoverflow question about this ? need more explanation to see how to do it – NAIIFOS 7 Apr 19 '20 at 16:41
  • See [this answer](https://stackoverflow.com/a/23451789/5288316). Don't forget setting `maxWidth`. You can add other SearchView attributes in the layout file as needed. – Nicolas Apr 19 '20 at 16:49

0 Answers0