Search code examples
androidkotlinprotocol-buffersandroid-jetpack-datastore

How to import proto definitions when using Proto DataStore?


The enum I want to import in other .proto files

// spc/main/proto/battery_saver_mode_enum.proto:
syntax = "proto3";

option java_package = "com.freephoenix888.savemylife";
option java_multiple_files = false;

enum BatterySaverMode {
  Disabled = 0;
  Enabled = 1;
  Adaptive = 2;
}

The way I try to import it

// src/main/proto/location_preferences.proto
syntax = "proto3";

import "battery_saver_mode_enum.proto";

option java_package = "com.freephoenix888.savemylife";
option java_multiple_files = true;

message LocationPreferences {
  bool isLocationSharingEnabled = 1;
  BatterySaverMode BatterySaverMode = 2;
}

Error

Cannot resolve import 'battery_saver_mode_enum.proto'

Cannot resolve import 'battery_saver_mode_enum.proto' error

My attempts to fix this

When I try Andriod Studio autocomplete I have this:
enter image description here

When I try to use the relative path import "./battery_saver_mode_enum.proto";
I get the error Backslashes, consecutive slashes, ., and .. are not allowed in the virtual path

I have .proto files in the src/main/proto folder because every Proto DataStore guide says to do so. If I pul them in another directory - I get a lot of compilation errors


Solution

  • Just use import "battery_saver_mode_enum.proto"; and do not care about this hint-error.
    It is not a compilation error.
    If you build it - your import works and you will not get any compilation errors about this import.

    The proto3 Language Guide also tells to use import this way