Rotary encoders

I was wondering if it is possible to add a rotary encoder (compatible with AP) to this type of inexpensive motors.

I will use those motors for a 4WD rover and I thought about using an optical wheel encoder but the accuracy is quite limited.

Yes, you can buy many with hall effect quadrature encoders.

You will need a motor where the axle protudes from the back. If the endcap is closed, you can not fix the magnets there. If you fix an encoder to the wheels, you will only get the resolution that the given encoder provides (not the added resolution through the gearing) and you will face problems with dirt and mechanical sturdiness.