Search code examples
flutterdarttrie

trie implementation throws shrinker error in flutter


I'm working on a search that I can use with flutter_typeahed package. I found a package called trie, but it doesn't have null safety so I thought of including that and contribute to the project as well as my own.

Since the entire trie file is quite big, I'm providing a link to pastebin.

This is the Search class that I've written:

class Search {
  final Map<String, RecipeModel> _map = Map.fromIterable(
      Store.instance.getAllRecipes(),
      key: (recipe) => RecipeModel().recipeName!);

  late final Trie trie;
  Search() {
    // This will be O[n]
    trie = Trie.list(_map.keys.toList());
  }

  late RecipeModel recipe;

  RecipeModel returnRecipe(String? suggestion) {
    if (suggestion == null) return recipe;
    // This will be O(1) instead of O(n) [better]
    final RecipeModel? found = _map[suggestion];
    return found ?? recipe;
  }

  List<String> returnSuggestions(String prefix) {
    //will return O[W*L] ad-hoc search was O[n^2]
    return trie.getAllWordsWithPrefix(prefix);
  }
}

But I'm getting the following error:

Cannot fit requested classes in a single dex file (# methods: 66909 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
The number of method references in a .dex file cannot exceed 64K.

The shrinker may have failed to optimize the Java bytecode.
To disable the shrinker, pass the `--no-shrink` flag to this command.

What can I do to make my code efficient and fix the error?

N.B: RecipeModel is simply a model which holds recipeName, authorId etc. And Store.instance.getAllRecipes(), returns a list of recipes


Solution

  • I had to enable multidex. And I had to change the following things:

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.food_app"
        minSdkVersion 17
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true // add this line
    }
    
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'com.android.support:multidex:1.0.1' // add this one
    }