tool-suite-X icon indicating copy to clipboard operation
tool-suite-X copied to clipboard

InstrumentationRegistry.getContext() is deprecated.

Open Lamouresparus opened this issue 1 year ago • 18 comments

Description: In the PropertiesNonPrivilegedTest.java file in ODK-X Android Library, the InstrumentationRegistry.getContext() method is used to get the context of the application. However, this method is deprecated.

Proposed fix: Replace the InstrumentationRegistry.getContext() method with the androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getTargetContext() method.

Example:

// Old code
Context context = InstrumentationRegistry.getContext();
// New code
Context context = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getTargetContext();

Testing: After making the change, rebuild and run the Project app. Run the PropertiesNonPrivilegedTest test. The test should pass.

Lamouresparus avatar Oct 09 '23 10:10 Lamouresparus

@Lamouresparus can you assign me this please?

permission-error avatar Oct 09 '23 11:10 permission-error

hello ,Ms @Lamouresparus this is my first time contributing to an open source project through Outreachy.. could i please be assigned this task?

Giftemmanuel avatar Oct 09 '23 11:10 Giftemmanuel

@Lamouresparus please can you assign it to me, thank you?

Kemyikpe avatar Oct 09 '23 11:10 Kemyikpe

@permission-error you still have #430 and #431, I think you should clear those out first. Thank you

Lamouresparus avatar Oct 09 '23 11:10 Lamouresparus

hello ,Ms @Lamouresparus this is my first time contributing to an open source project through Outreachy.. could i please be assigned this task?

Giftemmanuel avatar Oct 09 '23 11:10 Giftemmanuel

@Giftemmanuel I have assigned this issue to you

Lamouresparus avatar Oct 09 '23 11:10 Lamouresparus

@Lamouresparus could you please assist me in finding this file "PropertiesNonPrivilegedTest.java " i can't seem to find it throughout the "https://github.com/odk-x / tool-suite-X" repository.

Giftemmanuel avatar Oct 11 '23 11:10 Giftemmanuel

@Giftemmanuel there is no code in the tool-suite-X repository. The issue description tells you which code repository the file is in.

wbrunette avatar Oct 11 '23 17:10 wbrunette

@Giftemmanuel, the file you are seeking can be found in the Android Library repository at this location: PropertiesNonPrivilegedTest.java. The specific changes required can be pinpointed to these lines: Line 67, Line 90, and Line 128.

The recommended action is to replace the aforementioned code lines with the following: Context context = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getTargetContext();. After making these changes, please re-run the build to ascertain if the tests now pass. If you encounter any issues or require further assistance, feel free to reach out on Slack, and we can collaborate to resolve them.

permission-error avatar Oct 11 '23 20:10 permission-error

@Giftemmanuel I can help you with a detailed explanation of what @Lamouresparus wants you to do on this task.

First, you'll need to clone the androidlibrary project if you have not already cloned, and it should be on the same directory you have the services project. When you have successfully build the androidlibrary project, you are now ready to start the task.

To start the task, here are the steps to follow:

  1. In the androidlibrary project, search for PropertiesNonPrivilegedTest.java. Here is a simple way to search in Android studio, if you are using a MacBook or Windows pc, double tap on shift key on your keyboard or click on the search icon (Screenshot below),
    Screenshot 2023-10-11 at 8 48 57 PM

  2. After clicking on the search icon or double tap on shift key on your pc keyboard, you'll see the dialog below:

Screenshot 2023-10-11 at 8 52 19 PM
  1. On the highlighted portion pointed by the red arrow, type the class name: PropertiesNonPrivilegedTest

You will see this (Screenshot below):

Screenshot 2023-10-11 at 8 55 52 PM
  1. Click on the class name, pointed by the arrow, it should open the class PropertiesNonPrivilegedTest.java.

Now you are ready to make the changes.

  1. In the PropertiesNonPrivilegedTest.java class, scroll down to line 67, 90 and 128, you will see this line of code: Context context = InstrumentationRegistry.getContext();
Screenshot 2023-10-11 at 8 58 19 PM
  1. Replace each of those lines with this code: Context context = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getTargetContext();

  2. What you'll now have should look like the screenshot below in line 67, 90 and 128:

odx screenshot

  1. Finally, you can head over to the last thing, which is to run the test. Click on the green double play button that is highlighted on the screenshot below to run the test, if all test passed, you're good to submit your PR.
Screenshot 2023-10-11 at 9 09 02 PM

I hope this helps. Good luck. please feel free to ask me any question if you are still not clear on what to do.

Kemyikpe avatar Oct 11 '23 20:10 Kemyikpe

Hey @Lamouresparus , i want to add some little flavour on the code ... Instead of writing the full package name on the code , why not import the package first and write the code without full package name . I mean , lets import this package first import androidx.test.platform.app.InstrumentationRegistry; by replacing import androidx.test.InstrumentationRegistry; then we can make the change in code : Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); instead of the Context context = androidx.test.platform.app.InstrumentationRegistry.getInstrumentation().getTargetContext();

Any suggestion and recommendation on this ?

NiranjanNlc avatar Oct 12 '23 12:10 NiranjanNlc

You can definitely use imports. I think it's for ease of explanation or possibly namespace collision in some files.

wbrunette avatar Oct 12 '23 13:10 wbrunette

You can definitely use imports. I think it's for ease of explanation or possibly namespace collision in some files.

noted .....

NiranjanNlc avatar Oct 12 '23 13:10 NiranjanNlc

Kemyikpe

thank you.. this was really helpful. i have completed the adjustment with no errors, how to i submit this please? do i send a pull request? @Lamouresparus

Giftemmanuel avatar Oct 13 '23 16:10 Giftemmanuel

Yes please submit a PR.

wbrunette avatar Oct 13 '23 17:10 wbrunette

can i expect to get this isuse assighned to me also ?

NiranjanNlc avatar Oct 15 '23 12:10 NiranjanNlc

@NiranjanNlc I have assigned you the issue to practice. Unfortunatly only one PR is accepted... but there are many references to the deprecated function so if you find one you can do a PR on that.

wbrunette avatar Oct 15 '23 22:10 wbrunette

Hey @wbrunette, thanks . I have created the pull request. It's taking more time than usual to perform simple tasks .I faced that when i run the tests , it always says that the Tests were cancelled on device . It was a lot of headache for me and happened to consumes more that usual time on this issues . However, the continuous research and testing the various ideas and steps discussed described on the internet,I found that the issuse was the replica of Google Issue Tracker. As suggested , the downgrading the ANdroid Girraffe to the older version of Android studio worked for me . By downgrading to the Android Studio Flamingo, the issue has vanished like magic. I had communicated this finding ( downgrading the android studio version) in the slack channel and many of my friends really find this useful. please review my pull request @wbrunette and suggest me if more improvement can be done . Thanks.

NiranjanNlc avatar Oct 22 '23 09:10 NiranjanNlc

hello @wbrunette , I am an Outreachy applicant, and I would like to work on this issue as part of my contribution. Could you please assign it to me? thank you.

rchaurase avatar Oct 14 '24 04:10 rchaurase