screenshot-tests-for-android icon indicating copy to clipboard operation
screenshot-tests-for-android copied to clipboard

WindowAttachment.setAttachInfo() fails for API 28

Open sandeep-desai-ck opened this issue 4 years ago • 4 comments

same issue as the previous ticket, but still failing on API 28. The constructor for ViewRootImpl is the same in both APIs so I'm not sure why it's still failing in API 28 with NoSuchMethodException

sandeep-desai-ck avatar Jan 07 '21 23:01 sandeep-desai-ck

This is because it tries to use reflection on greylist API. So I suppose, if your app targets versions <28 it should work fine. Otherwise fix in the library needed.

if (Build.VERSION.SDK_INT >= 26) { viewRootImpl = cViewRootImpl .getConstructor(Context.class, Display.class) .newInstance(context, display);

https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

kirillakhmetov avatar Jan 15 '21 17:01 kirillakhmetov

Verified that the above suggestion (dropping target to below 28) does indeed work as an interim solution.

sandeep-desai-ck avatar Jan 19 '21 19:01 sandeep-desai-ck

If you don't have the option to downgrade the API there is another workaround that worked for me: You would explicitly allow calls to hidden APIs

build.gradle: androidTestImplementation 'org.lsposed.hiddenapibypass:hiddenapibypass:2.0'

Test:

@Test
fun screenshotButton() {
   val button = Button(getContext())
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
       HiddenApiBypass.addHiddenApiExemptions("L")
   }
   WindowAttachment.setAttachInfo(button)
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
       HiddenApiBypass.clearHiddenApiExemptions()
   }

   ViewHelpers.setupView(button).layout()
   Screenshot.snap(button).setName("button").record()
}

Since it only allow the hidden API for WindowAttachment.setAttachInfo I guess it's pretty safe and shouldn't influence the rest of the test behavior. There is also an option to allow hidden API without a library by using ADB. For my use case the library was a better fit though.

Might be helpful as well: https://stackoverflow.com/a/55970138/5155371

michaeltroger avatar Aug 03 '21 12:08 michaeltroger

@michaeltroger Why not to create a PR to fix the issue?

sergio-sastre avatar Oct 06 '22 18:10 sergio-sastre