Natural selection is a significant driver of population divergence and speciation of plants. Due to local adaptation to geographic regions with ecological gradients, plant populations harbored a wide range of adaptive genetic variation to enable them to survive the heterogeneous habitats. This is all the more necessary for desert plants, as they must tolerant more striking gradients of abiotic stresses. However, the genomic mechanism by which desert plants adapt to ecological heterogeneity remains unclear, which could help to guide the sustainability of desert ecosystems. Here, using restriction-site-associated DNA sequencing in 38 natural populations, we investigated the genomic divergence and environmental adaptation of sand rice, Agriophyllum squarrosum, an annual pioneer species that covers sand dunes in northern China. Population genetic structure analyses showed that sand rice could be divided into three geographically distinct lineages, namely, Northwest, Central, and East. Phylogeographic analyses revealed that the plant might originate locally in Bergen County and further differentiated into the East lineage and then the Central lineage. Ecological niche modeling found that different lineages occupied distinct ecological niches, suggesting that the ecological gradient would have triggered genomic differentiation among sand rice lineages. Ecological association study supported that the three SNPs under divergent selection were closely correlated with precipitation gradients, indicating that precipitation might be the most important stress trigger for lineage diversity in sand rice. These adaptive SNPs could be used to genotype suitable germplasms for the ecological restoration of specific desertified lands. Further analyses found that genetic structure could significantly overestimate the signals for balancing selection. Within the Central lineage, we still found that 175 SNPs could be subject to balancing selection, which could be the means by which sand rice maintains genetic diversity and adapts to multiple stresses across heterogeneous deserts and sandy lands. From a genomic point of view, this study highlighted the local and global adaptation patterns of a desert plant to extreme and heterogeneous habitats. Our data provide molecular guidance for the restoration of desertified lands in the arid and semi-arid regions of China and could facilitate the marker assistant breeding of this potential crop to mitigate climate change.