I am creating an app linking to an API with about 265 methods to it. I would very much like to break apart the declaration of these APIs into multiple files to keep them organized and accessible. However Retrofit explicitly disallows combining interfaces through extension.
java.lang.IllegalArgumentException: Interface definitions must not extend other interfaces.
I had been trying to declare it as follows.
public interface ApiService extends ProfileService, AccountService {
// Empty interface, methods divided into other services
}
public interface ProfileService {
@GET("/api/v1/protected/profile")
public void getProfile(Callback<Profile> callback);
...
}
public interface AccountService {
@GET("/api/v1/protected/account")
public void getAccount(Callback<Account> callback);
...
}
There is a discussion about this issue on a pull request. The library authors have decided that extending interfaces like this is not in the spirit of the library. https://github.com/square/retrofit/pull/676
Jake Wharton (the author) says that "Retrofit favors composition." And in response to "Do you really have a single adapter with a ton of proxies?", "Yes. They are generated from a service declaration in protos. One interface per service."
I've been reading about composition vs inheritance and fail to grasp how to achieve the goal of breaking up the declaration.
How can I divide the interface declaration up? Is there a best practice that I'm missing?
Thank you.
Just create separate interfaces.
public interface ProfileService {
/* ... */
}
public interface AccountService {
/* ... */
}
ProfileService profileService = mRestAdapter.create(ProfileService.class);
AccountService accountService = mRestAdapter.create(AccountService.class);