在我的App的最新版本中,我添加了proguard。一个奇怪的错误开始出现:

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


错误在以下行中触发:

public boolean isVideo() {
    return Media.MEDIA_TYPE_VIDEO.equals(mImage.getType());
}


MEDIA_TYPE_VIDEO是一个常量,声明如下:

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) {
        ...
}


我在许多帖子中读到Proguard不会混淆常量声明。但是,它必须是已废除的常量或mImage对象。 stacktrace告诉我,在初始化视图后立即触发了该NPE:

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);
        ...
    }
}


也许是因为我迷上了onActivityCreated而发生了?

最佳答案

com.example替换为Media类的基本包,并将其放入
  proguard文件中的行


-keep class com.example.** { *; }

关于android - 捍卫者缩小了我的常数吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44288187/

10-12 00:31