Why was Andorra never permanently incorporated into either France or Spain?

In 843 King Charles the Bald of France (grandson to Charlemagne) ceded the valleys of Andorra to Sunifred, Count of Urgell, making the Counts of Urgell the owners of Andorra and holders of its feudal rights. Documents held at the cathedral at La Seu d’Urgell (afirst town on the Spanish side of the Andorra border) from the second half of the 9th century (exact date unknown) show the Urgell family as owning the six parishes of Andorra.
In 988 Borrell II, Count of Urgell, began a series of transfers in which the family ceded the Andorra lands and feudal rights to the Urgell church. Count Ermengol VI made the final transfer in 1132. The first transfer involved a swop of a piece of land in Cerdanya. The final transfer involved a payment of 1,200 melgorian sous from Bishop Pere Berenguer to the Urgells (the document recording this survived). The details of the other transfers are not known.
The church accepted feudal dues from Andorra in livestock, vegetables, bread, and wine. At the end of the 11th-century unrest in the Urgell area, due to local warlords and other marauders, left the church wanting military protection. In 1095 Guillem Guitart of Caboet undertook to defend the church. In 1159 that arrangement was confirmed by treaty. The Caboets would provide military protection and in return would receive the feudal rights to Andorra. The church would retain sovereignty.
Through a series of dynastic marriages, those Caboet rights passed to the Viscounts of Castellbo and eventually to the Counts of Foix. The Bishop and clerics of the Urgell Church opposed the last of these marriages (in 1208, between Roger Bernat II of Foix and Ermessenda, the Castellbo heiress), fearing the expansionist tendencies of the Counts of Foix. Through the 13th century, the Counts of Foix were seizing land in the Urgell region, land often belonging to the church. The House of Foix also supported the Cathar heretics and the Bishop of Urgell threatened ex-communication.
In 1265 Roger Bernat III became Count of Foix, renounced the authority of the church, and claimed total sovereignty over the Valleys of Andorra. The Bishop appealed to King Pere II of Catalonia and Aragon – who was liege lord to both parties. The king, wanting to curb the rising power of the House of Foix, sided with the Bishop. A settlement was negotiated and signed in 1278 in a face-to-face meeting by Count Roger Bernat III of Foix, and Bishop Pere of Urgell. Each recognized the sovereignty of the other over the Valleys of Andorra. Known as the Pariatge (a term signifying ‘equality’) the agreement established the co-principality that exists to this day.
In the 16th century, the rights of the Count of Foix passed to the French Crown when Henry of Foix and Navarre became King Henry IV of France. The French Revolution (beginning in 1789) severed the link briefly but it was restored 13 years later by Napoleon I at the insistence of the Andorrans. The Andorrans feared the collapse of the co-principality would leave them vulnerable to Spanish expansionism and lose them their customs exemptions and other privileges in France.
The only time Andorra has not been independent was from 1813, when the First French Empire under Napoleon I annexed Catalonia and divided it into four departments, with Andorra (and the Bishopric of Urgell) forming part of the district of Puigcerdà. This was reversed once Napoleon I was defeated. Since 1806 the rights of the co-principality have belonged to the French Head of State and are now held by the French President.
In 1993 Andorra voted to adopt of new Constitution, becoming a “parliamentary co-principality” with the co-princes becoming the joint heads of state. The co-princes have no veto power over legislation passed by the General Council (the Andorran legislature) but they do sign off on certain laws, as per the constitution.
Andorra survived because it was balanced between two major powers, Spain and France while offering so little value to either side that it was not worth provoking the other country to seize the territory.
The co-prince system, while mostly wholeheartedly supported by Andorrans, continues to present challenges. There was a mini constitutional crisis in 2009 when Nicolas Sarkozy threatened to resign as co-prince if Andorra did not get themselves off the tax haven list. It was not clear whether he could actually do that, given the authority lies in the office of the French President, not with the individual incumbent.
In 2014 a law allowing civil unions for gay couples (equal to marriage rights in all but name) and full adoption rights was passed and promulgated by co-prince François Hollande, as the signature of one of the two co-princes was needed. The current Bishop of Urgell, Joan-Enric Vives, reluctantly accepted that but he has recently threatened to abdicate as co-prince if abortion is legalized.
In Andorra, having an abortion is punishable by up to six months in prison for the woman and Andorran doctors performing abortions risk up to three years in prison and a five-year ban from practicing. Most women in need of abortion go to Spain.