Division of labor, where subpopulations perform complementary tasks simultaneously within an assembly, characterizes major evolutionary transitions of cooperation in certain cases. Currently, the mechanism and significance of mediating the interaction between different cell types during the division of labor, remain largely unknown. Here, we investigated the molecular mechanism and ecological function of a policing system for optimizing the division of labor inBacillus velezensisSQR9. During biofilm formation, cells differentiated into the extracellular matrix (ECM)-producers and cheater-like nonproducers. ECM-producers were also active in the biosynthesis of genomic island-governed toxic bacillunoic acids (BAs) and self-resistance; while the nonproducers were sensitive to this antibiotic and could be partially eliminated. Spo0A was identified to be the co-regulator for triggering both ECM production and BAs synthesis/immunity. Besides its well-known regulation of ECM secretion, Spo0A activates acetyl-CoA carboxylase to produce malonyl-CoA, which is essential for BAs biosynthesis, thereby stimulating BAs production and self-immunity. Finally, the policing system not only excluded ECM-nonproducing cheater-like individuals but also improved the production of other public goods such as protease and siderophore, consequently, enhancing the population stability and ecological fitness under stress conditions and in the rhizosphere. This study provides insights into our understanding of the maintenance and evolution of microbial cooperation.