Adview loadAd() triggering a StrictMode violation

二次信任 提交于 2019-12-10 17:32:50

问题


I am trying to add some adds in my application. When I have StrictMode enabled, I get a bunch of policy violations like this one (maybe 6 or 7 of these). Is this normal?

18817-18817/com.xyz.xyz.xyz D/StrictMode: StrictMode policy violation; ~duration=67937 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=5439503 violation=2
                                                   at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1263)
                                                   at libcore.io.BlockGuardOs.fstat(BlockGuardOs.java:132)
                                                   at java.io.RandomAccessFile.length(RandomAccessFile.java:240)
                                                   at java.util.zip.ZipFile.readCentralDir(ZipFile.java:386)
                                                   at java.util.zip.ZipFile.<init>(ZipFile.java:175)
                                                   at java.util.zip.ZipFile.<init>(ZipFile.java:142)
                                                   at android.webkit.WebViewFactory.getLoadFromApkPath(WebViewFactory.java:357)
                                                   at android.webkit.WebViewFactory.getWebViewNativeLibraryPaths(WebViewFactory.java:407)
                                                   at android.webkit.WebViewFactory.loadNativeLibrary(WebViewFactory.java:511)
                                                   at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:188)
                                                   at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:158)
                                                   at android.webkit.WebSettings.getDefaultUserAgent(WebSettings.java:1239)
                                                   at com.google.android.gms.ads.internal.util.ai.a(:com.google.android.gms.DynamiteModulesA:414)
                                                   at com.google.android.gms.ads.internal.util.y.a(:com.google.android.gms.DynamiteModulesA:419)
                                                   at com.google.android.gms.ads.internal.state.h.a(:com.google.android.gms.DynamiteModulesA:349)
                                                   at com.google.android.gms.ads.internal.a.<init>(:com.google.android.gms.DynamiteModulesA:153)
                                                   at com.google.android.gms.ads.internal.c.<init>(:com.google.android.gms.DynamiteModulesA:104)
                                                   at com.google.android.gms.ads.internal.c.<init>(:com.google.android.gms.DynamiteModulesA:92)
                                                   at com.google.android.gms.ads.internal.g.<init>(:com.google.android.gms.DynamiteModulesA:55)
                                                   at com.google.android.gms.ads.internal.o.<init>(:com.google.android.gms.DynamiteModulesA:89)
                                                   at com.google.android.gms.ads.ChimeraAdManagerCreatorImpl.newAdManagerByType(:com.google.android.gms.DynamiteModulesA:66)
                                                   at com.google.android.gms.ads.internal.client.al.onTransact(:com.google.android.gms.DynamiteModulesA:91)
                                                   at android.os.Binder.transact(Binder.java:387)
                                                   at com.google.android.gms.ads.internal.client.am.newAdManagerByType(:com.google.android.gms:170)
                                                   at com.google.android.gms.ads.AdManagerCreatorImpl.newAdManagerByType(:com.google.android.gms:90)
                                                   at com.google.android.gms.ads.internal.client.al.onTransact(:com.google.android.gms:91)
                                                   at android.os.Binder.transact(Binder.java:387)
                                                   at com.google.android.gms.ads.internal.client.zzv$zza$zza.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zze.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl$1.zzim(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl$1.zzin(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzl.zza(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zzjn(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zzjm(Unknown Source)
                                                   at com.google.android.gms.ads.internal.client.zzae.zza(Unknown Source)
                                                   at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
                                                   at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
                                                   at com.xyz.xyz.xyz.MainActivity.LoadAd(MainActivity.java:539)
                                                   at com.xyz.xyz.xyz.MainActivity.onCreate(MainActivity.java:323)
                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
                                                   at android.app.ActivityThread.-wrap15(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:72

The code for the adview is like this:

private void LoadAd(){
    Log.d(TAG, "LoadAd() called");
    mAdView = new AdView(this);
    //Finds an ad that best fits a user's device.
    LinearLayout adContainer = (LinearLayout)   findViewById(R.id.adView_container);
    mAdView.setAdSize(Utils.getBannerSize(this));
    mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
    adContainer.addView(mAdView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

回答1:


If you set StrictMode to detect DiskReads by detectAll() or detectDiskReads(), StrictMode violation occurs when app reads the disk.

I think DiskReads is not problem for normal situlations. So you can disable this violations like following:

StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder()
    . ...
    .permitDiskReads()
    .permitDiskWrites() // If you also want to ignore DiskWrites, Set this line too.
    . ...
    .build();

StrictMode.setThreadPolicy(threadPolicy);


来源:https://stackoverflow.com/questions/38930737/adview-loadad-triggering-a-strictmode-violation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!