Open sandeep-desai-ck opened 3 years ago
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
Verified that the above suggestion (dropping target to below 28) does indeed work as an interim solution.
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 Why not to create a PR to fix the issue?
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