In a recent release of my App I added proguard. A weird error started to show up:
Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'java.lang.String com.siplay.myapp.b.e.e.b()' on a null object reference
The error is being triggered in this line:
public boolean isVideo() {
return Media.MEDIA_TYPE_VIDEO.equals(mImage.getType());
}
MEDIA_TYPE_VIDEO
is a constant and is declared like this:
public class Media implements Parcelable {
...
public static final String MEDIA_TYPE_VIDEO = "video";
...
@Retention(RetentionPolicy.SOURCE)
@StringDef({Media.MEDIA_TYPE_VIDEO, Media.MEDIA_TYPE_PHOTO, Media.MEDIA_TYPE_THUMB})
public @interface MediaTypes {}
...
public void setType(@MediaTypes String type) {
...
}
I read in many posts that Proguard doesn't mess with the constants declarations. However it must be the offuscated constant or the mImage
object. The stacktrace tells me that this NPE is triggered right after I initialize the views:
public class MyFragment extends Fragment {
...
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mImage = savedInstanceState.getParcelable(Constants.BUNDLE_IMAGE);
...
}
mVideoLoader.setVisibility(View.GONE);
if (isVideo()) { //HERE
...
}
...
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(Constants.BUNDLE_IMAGE, mImage);
...
}
}
Maybe this is happening because I hooked from onActivityCreated?
replace
com.example
with base package ofMedia
Class and put this line in proguard file
-keep class com.example.** { *; }