Phone [Q] Is it possible to make a Java-Based Mobile OS run .apk files?

Hello!
I am a very novice Java programmer, with little experience in Java so far. I have, however, thought about writing my very own mobile OS in Java (when I obtain more experience). I would very much like the OS to be able to run .apk files. I read somewhere that the compatability layer for .apk files is written in Java. I was wondering three main things: is it possible to incorporate an apk compatability layer into a Java-Based OS, how difficult would it be to do this (if it is in fact possible), and, if it is doable, would this be legal?
Thank you!