Android |在运行时获取OkHTTP库版本

时间:2016-03-24 08:41:23

标签: java android okhttp

最近,我从我的应用程序中分析了崩溃报告,发现了几个指向okhttp

的堆栈跟踪

我的应用并不明确依赖okhttp

AFAIK okhttp版本取决于Android操作系统版本,okhttp库本身放置在设备上

为了帮助排除故障,我决定记录okhttp库版本,看起来我找到了几个有用的类

  1. com.squareup.okhttp.internal.Version
  2. okhttp3.internal.Version
  3. 只是为了确保我没有弄错我采用com.android.okhttp.internal.http.HttpURLConnectionImpl类形式堆栈跟踪并尝试Class.forName它成功

    另外我注意到com.squareup.okhttp转换为com.android.okhttp看起来像构建时,所以我完全尝试了这样的变体

    1. Class.forName("com.android.okhttp.internal.Version") - > java.lang.ClassNotFoundException
    2. Class.forName("com.squareup.okhttp.internal.Version") - > java.lang.ClassNotFoundException
    3. Class.forName("okhttp3.internal.Version") - > java.lang.ClassNotFoundException
    4. Class.forName("com.android.okhttp.internal.http.HttpURLConnectionImpl") - >成功
    5. 任何人都可以解释原因吗?我错过了什么?

      更新

      我已从设备adb pull /system/framework/okhttp.jar中删除了okhttp.jar,但它仅包含MANIFEST.MF

1 个答案:

答案 0 :(得分:1)

来自4.xx谷歌正在使用方块的okhttp部分

 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[]{});

strong_parameters

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

一切都取决于设备 - 你使用的os版本因为api正在发展,你可以使用反射,但你需要知道特定api上的字段

请参阅http://square.github.io/okhttp/

比较不同的api版本使用:https://github.com/square/okhttp/blob/master/CHANGELOG.md

你可以在开始时尝试

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

编辑:

通过反思

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

相同
{{1}}

如果你想打扰:

  • 每个班级都以同样的方式对待 - &gt; as equals(没有版本控制 - 你只能检查魔术次要主要编号 - 来自类的java编译器版本)
  • /system/framework/okhttp.jar的清单不包含版本属性

如果你想要okhttp.internal.Version类,那么:

{{1}}
  

结论:如果您想避免应用程序从库更改交付崩溃   自己的库版本和加载类或使用apk编译

相关问题