Urban rooftop agriculture (RA) and photovoltaics (RPV) offer sustainable solutions for energy-food systems in cities but compete for limited rooftop space. We explore the potential benefits (provisioning, economic, and environmental) and allocation strategy of RA and RPV across 13 million buildings in 124 Chinese cities, considering building height, age, function, rooftop type and occupation, and regional productivity. We found that RA yields superior economic benefits, while RPV excels in cradle-to-grave greenhouse gas emission reduction benefits. Prioritizing either RA or RPV compromises 70–100% of the above benefits brought by the other. An optimized allocation to maximize the overall benefits would retain >55% of their potential, meeting 14% (mean, 0.5–99% across cities) of urban vegetable needs and 5% (0.5–27% across cities) of the electricity needs. Such a scenario requires allocating 54% (varied 4–99% across cities) of the flat rooftop area to RA, and all remaining rooftops to RPV. Together, the productivity from rooftop RA and RPV are equivalent to 2.3×103 km2 of cropland and 86 Mt of coal, contribute 1.5% of the national GDP and reduce 1.6% of national greenhouse gas emissions (account for 0.1–33% of city levels), requiring considerable water (up to 18% of urban residential water use) and material demand (e.g., totalling 9 kt silver). By elucidating the benefits and resource costs of rooftop utilization, our findings can support synergetic decision-making to meet multiple sustainability goals in diverse cities.