Android | Get the version of the OkHTTP library at run time

I recently analyzed crash reports from my application and found several stack traces that point to okhttp

My application is not dependent on okhttp explicitly.

AFAIK okhttp version depends on Android OS version and okhttp library located on the device

To help with troubleshooting, I decided to write a version of the okhttp library and it looks like this: I found some useful classes for this

Just to make sure that I wasn’t mistaken, I took com.android.okhttp.internal.http.HttpURLConnectionImpl cool stack-trace form and tried Class.forName it - success

I also noticed that com.squareup.okhttp , converted to com.android.okhttp , looks like during build, so I completely tried such options

  • Class.forName("com.android.okhttp.internal.Version")java.lang.ClassNotFoundException
  • Class.forName("com.squareup.okhttp.internal.Version")java.lang.ClassNotFoundException
  • Class.forName("okhttp3.internal.Version")java.lang.ClassNotFoundException
  • Class.forName("com.android.okhttp.internal.http.HttpURLConnectionImpl") → success

Can someone explain why? What did I miss?

Update

I pulled okhttp.jar from my adb pull /system/framework/okhttp.jar , but it contains MANIFEST.MF only

+6
source share
1 answer

of 4.xx google uses okhttp part of the square

 /** * This implementation uses HttpEngine to send requests and receive responses. This class may use * multiple HttpEngines to follow redirects, authentication retries, etc. to retrieve the final * response body. * * <h3>What does 'connected' mean?</h3> This class inherits a {@code connected} field from the * superclass. That field is <strong>not</strong> used to indicate whether this URLConnection is * currently connected. Instead, it indicates whether a connection has ever been attempted. Once a * connection has been attempted, certain properties (request header fields, request method, etc.) * are immutable. */ public class HttpURLConnectionImpl extends HttpURLConnection { private String defaultUserAgent() { String agent = System.getProperty("http.agent"); return agent != null ? Util.toHumanReadableAscii(agent) : Version.userAgent(); } 

https://github.com/square/okhttp/blob/master/okhttp-urlconnection/src/main/java/okhttp3/internal/huc/HttpURLConnectionImpl.java

http://square.imtqy.com/okhttp/

it all depends on the device - which version of os u is used, because api is developing, u can use reflections, but you need to know which field is on a specific api

see https://github.com/square/okhttp/blob/master/CHANGELOG.md

to compare versions of different versions of api: https://android.googlesource.com/platform/external/okhttp/

u can try at the beginning

 System.getProperty("http.agent"); 

edit:

through reflections

  HttpURLConnection connection = (HttpURLConnection) new URL("http://google.com") .openConnection(); Method method = connection.getClass().getDeclaredMethod("defaultUserAgent"); method.setAccessible(true); String okEngineVersion = (String) method.invoke(connection, new Object[]{}); 

same as

 String okEngineVersion = System.getProperty("http.agent"); 

and if you want to worry:

  • each class is processed in the same way → as equal (without version control - u can only check the majority value of magic - the java compiler from the class)
  • the manifest /system/framework/okhttp.jar does not contain version properties

if u wants the okhttp.internal.Version class, then:

  File file = new File("/system/framework/okhttp.jar"); // using javaxt-core lib Jar jar = new Jar(file); jar.getVersion(); // load dex DexFile dexfile = DexFile.loadDex(file.getAbsolutePath(), File.createTempFile("opt", "dex", _context.getCacheDir()).getPath(), 0); Enumeration<String> dexEntries = dexfile.entries(); ClassLoader systemClassLoader = DexClassLoader.getSystemClassLoader(); while (dexEntries.hasMoreElements()) { String className = dexEntries.nextElement(); Class<?> aClass = systemClassLoader.loadClass(className); } 

conclusion: if you want to avoid application crashes from changes in the library, your own version of the library and download classes on the fly or compilation using apk

+1
source

All Articles