China has defended Pakistan against strong criticism by US President Donald Trump that Islamabad was not doing enough to counter terrorism in the region.

Chinese Foreign Ministry spokeswoman Hua Chunying told a regular press briefing in Beijing on Tuesday that Pakistan "has made great sacrifices and contributions to fighting terrorism."

"The international community should fully affirm the efforts by Pakistan," she said.

In recent years, China and Pakistan have close diplomatic, economic and security ties to counter their common rival India.

China, however, has its own security concerns in the region, in particular any links between terrorists in Pakistan and Afghanistan and some militant groups Beijing blames for violence in the far western region of Xinjiang.

The Chinese government is investing to link its far western Xinjiang region to the Pakistani port of Gwadar in Balochistan province with upgraded infrastructure, power lines and transport links.

But fears over safety arose earlier this year when two Chinese workers were abducted in the provincial capital Quetta. Balochistan is at the heart of the China Pakistan Economic Corridor project but also racked by separatist and Taliban-linked insurgencies.

The remarks by the senior Chinese official come after the American president denounced Islamabad for offering safe haven to "agents of chaos."

“We can no longer be silent about Pakistan’s safe havens for terrorist organisations, the Taliban and other groups that pose a threat to the region and beyond,” Trump said in a major speech outlining US policy on Afghanistan,

Trump indicated that single-minded approach would extend to US relations with troubled ally Pakistan. He also warned that vital aid could be cut. "We have been paying Pakistan billions and billions of dollars at the same time they are housing the very terrorists that we are fighting."

“Pakistan has much to gain from partnering with our effort in Afghanistan. It has much to lose by continuing to harbor terrorists.”

Successive US governments have criticized Pakistan for links with the Taliban militants and for harboring slain al-Qaeda leader Osama bin Laden.

Elsewhere in his speech, Trump also said the United States wanted India to help more with Afghanistan, especially in the areas of economic assistance and development.

US President Donald Trump, left, greets military leaders before his speech on Afghanistan at the Fort Myer military base on August 21, 2017 in Arlington, Virginia. (Photo by AFP)

US making Pakistan as scapegoat for its failures

Senior civilian and military officials in Islamabad have frequently said the US government is making Pakistan a scapegoat to cover Washington’s failure in Afghanistan.

The seemingly staunch shift from Pakistan to India within Trump's speech could test an already strained relationship with Pakistan.

Naeem Khalid Lodhi, a defense analyst and retired general, said the United States was to blame for its own failure in Afghanistan.

“They are shifting blame to Pakistan,” he said. “Pakistan should not remain silent against such US behavior and we should work to build a new political and strategical bloc with big powers like Russia and China.”

A senior Pakistani intelligence official said, “Pakistan itself is the victim of terrorism. We are fighting militants and have conducted many ground and aerial operations and destroyed their sanctuaries. We want to eradicate them physically and ideologically.”

Pakistan, Afghanistan engage in blame game

Pakistan and Afghanistan regularly accuse each other of sheltering their enemy insurgents. Both sides, however, deny such an allegation.

In early June, Afghan President Ashraf Ghani accused Pakistan of instigating an “undeclared war of aggression” against his country.

Kabul blames elements in the Pakistani spy agency, Inter-Services Intelligence (ISI), for supporting the Taliban militants. Islamabad blames the Afghan government for giving refuge to militants on its side of the border. The two sides also accuse one another of not doing enough to stop militants engaging in cross-border attacks.

Afghanistan has been gripped by insecurity since the United States and its allies invaded the country as part of Washington’s so-called war on terror in 2001. Many parts of the country remain plagued by militancy despite the presence of foreign troops.

The United States invaded Afghanistan in October 2001 after the September 11 attacks on New York and Washington, and overthrew the Taliban regime. US forces have been bogged down there through the presidencies of George W. Bush, Barack Obama and now Trump.

During the past 16 years, the Taliban militants have been conducting terrorist attacks across the country, killing and displacing civilians.

In addition, the Daesh Takfiri terrorist group, which is mainly active in Syria and Iraq, has recently managed to take recruits from Afghan Taliban defectors.

Militants are now launching attacks on both Pakistani and Afghan soil.