I want to access the android.webkit.WebSettingsClassic
class on API level 16 but compile my application for API level 19. The WebSettingsClassic
class has been removed in API level 19 and thus the compiler cannot see it. How do I solve this?
Asked
Active
Viewed 125 times
-1

Monstieur
- 7,992
- 10
- 51
- 77
-
1are you sure? Do you have any documentation stating that the class was removed from the public API? – Blackbelt Jun 02 '14 at 07:03
-
1:) This class has not been removed since API 19. Check error properly.. – Pankaj Kumar Jun 02 '14 at 07:05
-
in that case you can copy that class to your own app and use it :) – Renjith K N Jun 02 '14 at 07:05
-
That class is not removed in API 19 . http://developer.android.com/reference/android/webkit/WebSettings.html – Soumil Deshpande Jun 02 '14 at 07:07
-
`WebSettingsClassic` has been removed from the source code completely in 4.4. https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/core/java/android/webkit – Monstieur Jun 02 '14 at 07:08
1 Answers
2
I solved it using the following modified code. This allows you to compile against API level 19.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static String getUserAgent(final Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return WebSettings.getDefaultUserAgent(context);
}
else {
try {
final Class<?> webSettingsClassicClass = Class.forName("android.webkit.WebSettingsClassic");
final Constructor<?> constructor = webSettingsClassicClass.getDeclaredConstructor(Context.class, Class.forName("android.webkit.WebViewClassic"));
constructor.setAccessible(true);
final Method method = webSettingsClassicClass.getMethod("getUserAgentString");
return (String) method.invoke(constructor.newInstance(context, null));
}
catch (final Exception e) {
return new WebView(context).getSettings()
.getUserAgentString();
}
}
}

Monstieur
- 7,992
- 10
- 51
- 77