每次启用 Proguard 并使用 Retrofit 时,我都会遇到这个 null 异常,为了解决这个问题,我将使用 Retrofit 的类添加到 Proguard 列表中,例如:
-keep class com.abohani.example.utils.ban { *; }
即使我添加了它,我仍然遇到以下错误:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.abohani.example.utils.BanUtil$2.onResponse(BanUtil.java:91)
此错误仅在启用 Proguard 时发生,禁用时根本不会出现错误,错误/代码示例中指出的行:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.API_PATH)
.addConverterFactory(GsonConverterFactory.create())
.build();
ReApi Api = retrofit.create(ReApi.class);
Call<Model> Connection = Api.GetList(UID,Key);
Connection.enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
List<BanItem> LatestItems = response.body().GetMain();
for (int i = 0; i < LatestItems.size(); i++) {
}
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
t.printStackTrace();
}
});
第91行是“for”循环行,响应是正确的,而且根本不为空,这只是由于启用了混淆器而引起的,我已经将其添加到列表中,但不知道为什么它不起作用。
什么原因造成的?以及如何在不将类添加到 Proguard 列表的情况下解决此错误,因为有些类我无法显示其完整代码,并且我必须使用 Proguard 对其进行加密。
提前致谢。
最佳答案
-keep class com.abohani.example.utils.** { *; }.
尝试添加到 proguard 并检查。确保所有模型都位于 utils 包下。
关于java - 启用 Proguard 时使用 Retrofit2 的 NullException int java.util.List.size(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794486/