CAM*_*BAP 6 java android okhttp
最近我从我的应用程序分析了崩溃报告,发现了几个指向的堆栈跟踪 okhttp
我的应用程序不依赖于okhttp显式.
AFAIK okhttp版本依赖于Android操作系统版本,而okhttp库本身则放在设备上
为了帮助排除故障,我决定记录okhttp库版本,看起来我发现了几个有用的类
只是为了确保我没有弄错我采用com.android.okhttp.internal.http.HttpURLConnectionImpl类形式堆栈跟踪并尝试Class.forName它 - 成功
另外我注意到com.squareup.okhttp转换到com.android.okhttp看起来像构建时,所以我完全尝试了这样的变体
Class.forName("com.android.okhttp.internal.Version") - > java.lang.ClassNotFoundExceptionClass.forName("com.squareup.okhttp.internal.Version") - > java.lang.ClassNotFoundExceptionClass.forName("okhttp3.internal.Version") - > java.lang.ClassNotFoundExceptionClass.forName("com.android.okhttp.internal.http.HttpURLConnectionImpl") - >成功有谁能解释为什么?我错过了什么?
更新
我从我的设备中取出了okhttp.jar,adb pull /system/framework/okhttp.jar但它MANIFEST.MF仅包含
从 4.xx 开始,谷歌正在使用 squareup 的 okhttp 部分
/**
* 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();
}
Run Code Online (Sandbox Code Playgroud)
http://square.github.io/okhttp/
一切都取决于设备 - 你使用什么操作系统版本,因为 api 正在发展,你可以使用反射,但你需要知道特定 api 上的字段是什么
请参阅https://github.com/square/okhttp/blob/master/CHANGELOG.md
要比较不同的 api 版本,请使用: https: //android.googlesource.com/platform/external/okhttp/
你可以从一开始就尝试
System.getProperty("http.agent");
Run Code Online (Sandbox Code Playgroud)
编辑:
通过反射
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[]{});
Run Code Online (Sandbox Code Playgroud)
与...一样
String okEngineVersion = System.getProperty("http.agent");
Run Code Online (Sandbox Code Playgroud)
如果你想打扰:
如果你想要 okhttp.internal.Version 类那么:
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);
}
Run Code Online (Sandbox Code Playgroud)
结论:如果您想避免应用程序因库更改而崩溃,请交付自己的库版本并动态加载类或使用 apk 进行编译