gpt4 book ai didi

android - Proguard收缩导致空指针异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:25 27 4
gpt4 key购买 nike

我的 Android 应用程序中有一个视频 Activity 。在禁用 proguard 的调试构建中,一切都像一个魅力。但是在启用混淆器并且视频 Activity 开始的发布版本中,我得到了一个 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
}
}

异常(exception):

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)

还有我的 Activity :

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

还有我的proguard规则:

-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

最佳答案

请注意您已将类和方法排除在 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 文件中的接口(interface)(作为异常(exception)):

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

关于android - Proguard收缩导致空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com