Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidHttpClient not found (when running Robolectric)

I've set up a very simple project to test the integration of Robolectric + Data Binding + Retrolambda. When I run the test suit, I get the following message:

Error:(30, 30) Gradle: error: cannot access AndroidHttpClient
class file for android.net.http.AndroidHttpClient not found

This is pretty odd since I don't use AndroidHttpClient anywhere.


The error occurs here, on the "activity" line:

@Before
public void setup() {
    activity = Robolectric.setupActivity(MainActivity.class); // Error on this line
    textView = (TextView) shadowOf(activity).findViewById(R.id.textView);
    button = (Button) activity.findViewById(R.id.button);
    editText = (EditText) activity.findViewById(R.id.editText);
}

The program never uses AndroidHttpClient. In fact, this is the entire program:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    binding.setUser(new User());
    binding.button.setOnClickListener((v) -> {
        binding.textView.setText(String.format("Hello, %s!", binding.editText.getText()));
        binding.editText.setText("");
    });
}

Ideas as to what's wrong?

like image 731
Technocrat Avatar asked Sep 24 '15 10:09

Technocrat


4 Answers

AndroidHttpClient was removed from the SDK in v23 of the build tools.

As Robolectric is running against earlier versions, it expects it to be there, which is why you're seeing this error.

For now, you can add it back in:

android {
    useLibrary 'org.apache.http.legacy'
}

As detailed here.

There is a GitHub ticket open for Robolectric to fix this. You can follow the thread/ticket here.

Update:

As some people have correctly pointed out, a better way of doing this would be to create a class android.net.http.AndroidHttpClient in your test resources. This would be a preferred method because you're only modifying the test sources, not the production code, in order to accommodate the tests.

like image 95
Ben Pearson Avatar answered Nov 06 '22 18:11

Ben Pearson


I've just added fake class android.net.http.AndroidHttpClient in my test sources. And it solved the issue for now. Waiting for Robolectric to be updated

like image 37
Eugen Martynov Avatar answered Nov 06 '22 17:11

Eugen Martynov


Apparent problem and solution:

AndroidHttpClient was removed from the SDK in API Level 23, while Robolectric was set to run tests with SDK 21:

AndroidHttpClient was removed from the SDK in API Level 23
like image 6
Technocrat Avatar answered Nov 06 '22 17:11

Technocrat


I was able to solve this problem by creating a new class called AndroidHttpClient within a new package android.net.http. After that I had to annotate my Unit Test class with @Config(constants = BuildConfig.class, sdks = 21) which will run the tests against an emulated version of API 21 which is the last version of Android Robolectric supports currently.

There is currently an issue opened here, so once they release version 3.1 everything should be fine and you won't have to use this workaround.

like image 5
CodyEngel Avatar answered Nov 06 '22 18:11

CodyEngel