I have got the error "Cannot recognize lens. Attach it properly" with my Sony lens (⌀ 40,5 mm, 16-50). After turning on the camera, lens started trying to focus unsuccessfully and showed the error again and again.
Contacts between camera and lens were clean, mounting the lens finished with the right click sound. Both camera and lens are from Sony so "release w/o lens" setting had no effect.
When I tried to subtly push, pull and rotate all parts of the lens, suddenly the "extendable" part of the lens returned back to lens body and problem was solved.
The problem was that the movable part of the lens was not retracted correctly to the lens body - see comparison on the picture. You can fix the position with subtle pushing inside (or pulling out) the overhang, possibly with slight rotation.