Thanks for your answers.
You probably are right, and the best way to do with cards is to have one card for the "technology" (invisible building) as the first level of the corresponding real building.
Example: building barbarian_stone_walls
levels walls_tech (invisible building) stone_wall large_stone_wall epic_stone_wall

The first level would require a non-existent hidden_resource, but the building will be created in one of these three occasions:

1- Shopping: as a request of a barbarian diplomat or as an offer of a "civilized" diplomat (would it be feasible?) The possibility to trigger one offer of this type is also interesting, but I can't imagine the required conditions. Perhaps that the civilized faction construct one level (ex. large_stone_wall), and that one diplomat is near the barbarian faction leader.

2- Spying: when a barbarian spy is inside a settlement with building_min_level large_stone_wall, there is a probability to get this technology.
I would prefer that staying several turns (5 for example) in the settlement was the requirement for getting the technology.

3- Alliance: after some years of alliance between the two factions, there is some probability of getting the technology. Perhaps more complicated requirements would be necessary.

The problem is: Where would this wall_tech building be created? Is it possible to build this technology in all the settlements of the given faction? What about the new settlements conquered after the event?
I would prefer some kind of faction_capability only attainable by one of those three methods, but I think they are hard_coded.
Other possibility is to give some trait to all the generals of that faction, enabling them to construct the building, but I don't know if a trait can be used as a requirement for a building.

As you can see, there are many doubts about this subject.