Endospory in plants is the retention and development of gametophytes, partially or entirely, within the walls of the generative spore. This is a trait present in many heterosporous plant species.
Origin.
There is debate as to whether endospory or heterospory evolved first. Some debate centers upon the requirement of endospory to develop before heterospory. Endospory is assumed to follow heterospory but it has been suggested that without endospory, early plant species dependency on water fertilization and environmental impacts on gametophytic gene expression would have reduced the chances of heterospory in the Late Devonian. Heterospory and endospory are often found co-occurring and the origin of endospory is drawn from comparisons in extant species. Fossils provide evidence of the origin of heterospory in the middle to late Devonian with earliest record of fossil taxa being Cyclostigma and Bisporangiostrobus, late Devonian genera. Early fossil records of endospory have not been discussed in literature, but the oldest extant lineage with heterospory, the Selaginella, have been recognized as a potential intermediate in the morphological evolution to endospory due to its megaspores' potential for photosynthesis and rhizoids extending from the trilete structure.
Ovule structures began diversifying during the late Devonian, suggesting that endospory originated in around this time. It is possible that in some lineages, heterospory was an consequence of endospory through developmental changes of endospory.  In tracheophytes specifically, endospory and heterospory may have evolved separately a number of times. 
It has been suggested that heterospory and endospory may be adaptively linked, but with independent developmental control. Phylogenetic inference of hornworts demonstrates that endospory is homoplastic. This is observed in the separate origins of endospory across multiple orders of liverworts.
Endosporic gametophytes.
Endosporic megagametophytes extend only rhizoids and the archegonium from the spore wall, they often lack chlorophyll, and they do not acquire nutrients from the soil. Endosporic megagametophyte evolution directly correlates with endosporic microgametophytes, which are extremely reduced, and release flagellated sperm after their complete development and production of the antheridia within the spore wall.
Evolutionary benefits.
During gametophyte development, endosporic gametophytes are dependent on their sporophyte parent. The development of the gametophyte within the spore wall directly reduces the environmental impacts on the gametophytic gene expression resulting in higher genetic variation and rates of diversification. 
The retention of gametophytes within the spore wall additionally provided advantages for selection in ecological settings after fertilization. The support provided by the spore wall, which is similar but not as advanced as an ovule, increased reproductive success allowing for strong selective advantages during competition. Larger, enclosed megaspores were able to respond independently to the environment in regards to habitat and resources. 
Ecological benefits.
The development of gametophytes within spore walls provided improvements in sexual function as well as protection from harsh conditions. Nutrient dependence during gametophyte growth is fully supplied by the spore wall,  resulting in endosporic megagametophytes increased the ability to store metabolites, lengthening the time a spore could live without water and the ability to populate new and disturbed habitats.
References.
 ;templatestyles src="Reflist/styles.css" / ;