question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`isCleartextTrafficPermitted()` fails on OpenJDK 8 + Robolectric

See original GitHub issue

How to reproduce:

  • Install OpenJDK 8
  • Use current 3.3.0-SNAPSHOT version
  • new OkHttpClient(): The code fails in the static initializer, when trying to figure out the platform.

The code that breaks was introduced here: https://github.com/square/okhttp/issues/2513

Either the detection of the runtime is broken, or the code is wrong there. I’ll try to figure out an automatic test for this.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:17
  • Comments:39 (15 by maintainers)

github_iconTop GitHub Comments

21reactions
dave-r12commented, May 29, 2016

I was able to get the test green. Here’s what I did:

  1. Change to using OkHttp-3.3.1. The project was using a SNAPSHOT version, but 3.3 has since been released.
  2. Upgrade to robolectric 3.1-rc1.
  3. Added config annotation with SDK specified to the test class:
@RunWith(RobolectricGradleTestRunner.class)
@Config(sdk = 23)
public class CreateOkHttpClientTest {

As for why it works, I don’t know. It appears the default SDK is 21. The bit I don’t understand is why it isn’t throwing a ClassNotFoundException given this class is only in SDK 23. The test project itself is depending on API 23, which may have something to do with it.

15reactions
dave-r12commented, Jun 1, 2016

Ouch. This (hideous) workaround also appears functional with 3.0,

@RunWith(RobolectricGradleTestRunner.class)
@Config(shadows = CreateOkHttpClientTest.MyNetworkSecurityPolicy.class)
public class CreateOkHttpClientTest {

  @Test
  public void canCreateAClient() throws Exception {
    OkHttpClient client = new OkHttpClient();
    assertNotNull(client);
  }

  @Implements(NetworkSecurityPolicy.class)
  public static class MyNetworkSecurityPolicy {
    @Implementation public static NetworkSecurityPolicy getInstance() {
      try {
        Class<?> shadow = MyNetworkSecurityPolicy.class.forName("android.security.NetworkSecurityPolicy");
        return (NetworkSecurityPolicy) shadow.newInstance();
      } catch (Exception e) {
        throw new AssertionError();
      }
    }

    @Implementation public boolean isCleartextTrafficPermitted() {
      return true;
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Robolectric test fails when trying to inflate layout that has a ...
getApplicationContext() . The ApplicationContext has no theme at all, using an Activity 's Context suggested (which will always have the current ...
Read more >
Diff - platform/libcore - Google Git
Used for nullability annotations in OpenJDK source. droiddoc_exported_dir ... ErrnoException; method public static int setsid() throws android.system.
Read more >
Category: ElectricSpock - u-ryo's blog
これは、 isCleartextTrafficPermitted() fails on OpenJDK 8 + Robolectric #2533にあるように、 NetworkSecurityPolicy をShadowしてやればよい。
Read more >
NetworkSecurityPolicy - Android Developers
The policy currently consists of a single flag: whether cleartext network traffic is permitted. See isCleartextTrafficPermitted() .
Read more >
Testing Jetpack Security with Robolectric | by Matthew Dolan
You can then read and write encrypted data seamlessly as if it were a normal SharedPreferences file. sharedPreferences.edit().apply { putString( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found