Proguard收缩导致空指针异常

时间:2015-04-16 06:12:46

标签: android proguard

我的Android应用中有一个视频活动。 在proguard被禁用的调试版本中,所有工作都像魅力。 但是在发布版本中,当启用proguard并且视频活动开始时,我会获得NPE。

我的gradle文件的一部分:

buildTypes {
    debug {
        applicationIdSuffix '.dev'
        versionNameSuffix '-dev'
        minifyEnabled false
        shrinkResources false
    }
    release {
        minifyEnabled true
        shrinkResources true
        debuggable false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
}

例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ua.com.tv24.news/ua.com.tv24.news.ui.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
        at ua.com.tv24.news.ui.fragments.a.b.a(Unknown Source)
        at android.support.v4.app.Fragment.i(Unknown Source)
        at android.support.v4.app.u.a(Unknown Source)
        at android.support.v4.app.u.a(Unknown Source)
        at android.support.v4.app.u.a(Unknown Source)
        at android.support.v4.app.u.j(Unknown Source)
        at android.support.v4.app.p.onCreate(Unknown Source)
        at android.support.v7.app.ActionBarActivity.onCreate(Unknown Source)
        at ua.com.tv24.news.ui.activities.a.onCreate(Unknown Source)
        at ua.com.tv24.news.ui.activities.b.onCreate(Unknown Source)
        at ua.com.tv24.news.ui.activities.MainActivity.onCreate(Unknown Source)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的活动:

 package ua.com.tv24.news.ui.activities;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.FrameLayout;

import com.google.android.libraries.mediaframework.exoplayerextensions.Video;
import com.google.android.libraries.mediaframework.layeredvideo.PlaybackControlLayer;
import com.google.googlemediaframework.adplayer.ImaPlayer;

import de.greenrobot.event.EventBus;
import ua.com.tv24.news.R;
import ua.com.tv24.news.events.ui.VideoPreparedEvent;

public class DashVideoActivity extends ActionBarActivity implements PlaybackControlLayer.FullscreenCallback {
    private ImaPlayer imaPlayer;
    private FrameLayout videoPlayerContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        setContentView(R.layout.activity_dash_video);
        videoPlayerContainer = (FrameLayout) findViewById(R.id.video_frame);

        Bundle bundle = getIntent().getExtras();
        String videoUrl = bundle.getString(VideoActivity.VIDEO_URL_EXTRA);
        String appName = getString(R.string.app_name);

        //TODO get url and name from extras
        VideoItem video = new VideoItem(appName, new Video(videoUrl, Video.VideoType.DASH, ""),
                null);

        createImaPlayer(video);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (imaPlayer != null) {
            imaPlayer.play();
        }
    }

    @Override
    protected void onPause() {
        if (imaPlayer != null) {
            imaPlayer.pause();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (imaPlayer != null) {
            imaPlayer.release();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void createImaPlayer(VideoItem videoListItem) {
        if (imaPlayer != null) {
            imaPlayer.release();
        }

        // If there was previously a video player in the container, remove it.
        videoPlayerContainer.removeAllViews();

        String adTagUrl = videoListItem.adUrl;
        String videoTitle = videoListItem.title;

        imaPlayer = new ImaPlayer(this,
                videoPlayerContainer,
                videoListItem.video,
                videoTitle,
                adTagUrl);
        imaPlayer.setFullscreenCallback(this);

        Resources res = getResources();

        Drawable logo = res.getDrawable(R.drawable.launcher);
        imaPlayer.setLogoImage(logo);

        imaPlayer.play();
    }

    @Override
    public void onGoToFullscreen() {
    }

    @Override
    public void onReturnFromFullscreen() {
    }

    public static class VideoItem implements Parcelable {

        /**
         * The title of the video.
         */
        public final String title;

        /**
         * The actual content video (contains its URL, media type - either DASH or mp4,
         * and an optional media type).
         */
        public final Video video;

        /**
         * The URL of the VAST document which represents the ad.
         */
        public final String adUrl;

        /**
         * @param title The title of the video.
         * @param video The actual content video (contains its URL, media type - either DASH or mp4,
         *              and an optional media type).
         * @param adUrl The URL of the VAST document which represents the ad.
         */
        public VideoItem(String title, Video video, String adUrl) {
            this.title = title;
            this.video = video;
            this.adUrl = adUrl;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {

        }
    }

    public void onEventMainThread(VideoPreparedEvent event) {
        View progress = findViewById(R.id.videoLoaderBar);
        if (progress != null) {
            progress.setVisibility(View.INVISIBLE);
        }
    }
}

我的计划也是:

-dontwarn com.google.android.gms.**

# Picasso 
-dontwarn com.squareup.okhttp.**

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

#okhttp
-keepnames class com.levelup.http.okhttp.** { *; }
-keepnames interface com.levelup.http.okhttp.** { *; }
-keepnames class com.squareup.okhttp.** { *; }
-keepnames interface com.squareup.okhttp.** { *; }

-dontwarn com.squareup.okhttp.internal.http.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

#retrofit
-keepattributes Signature
-keepattributes *Annotation*

-keep class com.google.gson.** { *; }
-keep class com.google.inject.** { *; }
-keep class org.apache.http.* { *; }
-keep class org.apache.james.mime4j.* { *; }
-keep class javax.inject.** { *; }
-dontwarn rx.*
-keep class com.example.testobfuscation.** { *; }
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }

-keep class retrofit.** { *; }
-keep interface retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}
-keepclasseswithmembers class * {
    @ua.* <methods>;
}

-dontwarn retrofit.appengine.**

-keep class ua.com.tv24.news.models.** { *; }
-keepattributes InnerClasses

#xml parser
-keep interface org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.** { *; }

#eventBus
-keepclassmembers class ** {
    public void onEvent(**);
}
-keepclassmembers class ** {
    public void onEventMainThread(**);
}

#support v7
-keep class android.support.v7.** { *; }

#unusage classes
-dontwarn retrofit.appengine.UrlFetchClient
-dontwarn retrofit.RxSupport$1
-dontwarn com.google.ads.a.a
-dontwarn com.google.ads.a.c
-dontwarn com.google.ads.a.d
-dontwarn com.google.ads.a.f
-dontwarn com.google.android.exoplayer.hls.HlsMediaPlaylistParser
-dontwarn okio.DeflaterSink
-dontwarn org.simpleframework.xml.stream.StreamProvider
-dontwarn okio.Okio
-dontwarn org.simpleframework.xml.stream.StreamReader
-dontwarn org.simpleframework.xml.stream.StreamReader$Entry
-dontwarn org.simpleframework.xml.stream.StreamReader$Start
-dontwarn org.simpleframework.xml.stream.StreamReader$Text
-dontwarn retrofit.RestMethodInfo$RxSupport
-dontwarn retrofit.RxSupport
-dontwarn retrofit.RxSupport$2

1 个答案:

答案 0 :(得分:6)

Taker关注您从ProGuard处理中排除了类和方法:

# Hide warnings about references to newer platforms in the library
-dontwarn android.support.v7.**
# don't process support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

如果涉及Reflection,通常会发生错误,因为在这种情况下Proguard无法解析依赖关系。

我只是看到你错过了ProGuard文件中的界面(根据例外情况):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ...