raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

Fix Android strict mode warning #1623

Closed valeriopilo-tomtom closed 5 months ago

valeriopilo-tomtom commented 5 months ago

Android StrictMode helps Android developers identify application runtime issues. StrictMode's [detectUnbufferedIo()](https://developer.android.com/reference/kotlin/android/os/StrictMode.ThreadPolicy.Builder#detectUnbufferedIo()) check specifically detects when any sort of un-buffered I/O is made.

This check is available in Android SDK 26+.

This fixes the following logcat warning + stacktrace:

StrictMode policy violation; ~duration=196 ms: android.os.strictmode.UnbufferedIoViolation
    at android.os.StrictMode$AndroidBlockGuardPolicy.onUnbufferedIO(StrictMode.java:1648)
    at libcore.io.IoTracker.trackIo(IoTracker.java:35)
    at java.io.FileOutputStream.write(FileOutputStream.java:398)
    at java.io.FileOutputStream.write(FileOutputStream.java:352)
    at java.util.zip.ZipOutputStream.writeInt(ZipOutputStream.java:722)
    at java.util.zip.ZipOutputStream.writeLOC(ZipOutputStream.java:398)
    at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:240)
    at java.util.jar.JarOutputStream.putNextEntry(JarOutputStream.java:109)
    at net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:142)
    at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:101)
    at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:6317)
    <..application code..>

In our instance, the application call was a simple ByteBuddy().subclass("class.name").make().load(classLoader, AndroidClassLoadingStrategy.Wrapping(File("name"))) on an app where StrictMode is enabled with most available checks enabled.