VATICAN CITY (AP) _ Pope John Paul II gave Marxist Nicaragua and Ethiopia their first cardinals on Saturday and then cautioned all 28 new princes of the Church that their path may be one of hardships and ''even persecution.''

The pontiff told the 28, resplendent in bright red vestments, and the nearly 20,000 faithful gathered for the elevation in St. Peter's Square, that the cardinals ''must have no illusions about the way they will be received.''

''They will often be made a sign of contradiction, sometimes even persecution,'' the pope said.

But he said they should not fear, because ''the Gospel message contains within itself a power, that cannot be stopped.''

''Attempts may be made to oppose it and to stifle it, but in the end it will overcome all opposition, break through every barrier, reach every place, and conquer the heart of every person of good will,'' said John Paul.

His remarks, delivered in Italian, drew vigorous applause from the crowd gathered under hot, hazy skies.

The new cardinals came from 18 countries and include three from Communist countries - one from Czechoslovakia and two from John Paul's native Poland - and the American head of the Ukrainian Catholic Church with its 4.5 million followers, Cardinal Myroslav Ivan Lubachivsky.

In his three consistories since becoming pope in 1978, the 65-year-old pontiff has sharply increased the number of cardinals from Communist and Marxist-ruled lands. Twelve of the 14 cardinals from those areas were named by John Paul.

Cardinals elevated Saturday also included two from the United States, five from Italy, two each from Canada, France and West Germany, and one each from Austria, Belgium, Chile, India, the Netherlands, Nigeria, the Philippines, Spain and Venezuela.

The 21/2 -hour consistory increased the number of the College of Cardinals to 152 from 56 countries. Of the cardinals, 120 are younger than 80 and thus eligible to vote for a new pope. That is the maximum age allowed by church rules decreed by Pope Paul VI.

Asked about the warning that some new cardinals may face persecution, Cardinal John J. O'Connor of New York said he thought John Paul was ''just being realistic.''

''Some cardinals (are) living in daily danger of their lives, where there is repression, oppression,'' he told reporters. ''I didn't get a downbeat sense.''

John Paul, wearing a gold-embroidered red cape over white vestments, took his seat as the Sistine Chapel choir sang in Latin the 131st Psalm ''your priests put on justice and your faithful people sing with joy.''

The cardinals sat in single rows, flanking the pope seated in front of the center door of St. Peter's Basilica.

After the pope spoke, the new cardinals knelt before him and received a red skullcap and a biretta - the three-cornered pillbox cardinal's hat. The pope then blessed and embraced each one.

The outdoor service began when Vatican Secretary of State Cardinal Agostino Casaroli emerged in the square and read out the names of each new cardinal.

Large delegations of American pilgrims from the New York and Boston areas vigorously applauded as the names of their archbishops were called out - O'Connor and Bernard Law of Boston.

Big rounds of applause also followed the names of Polish Cardinal Henryk Roman Gulbinowicz of Wroclaw, an outspoken supporter of Poland's outlawed Solidarity labor union movement, and Nigerian Archbishop Francis A. Arinze, who at 53 is the youngest of the 28 cardinals.

Nicaragua's first cardinal, Miguel Obando y Bravo, and Ethiopia's Paulos Tzadua left the square after the ceremony. The Nicaraguan was to receive $100,000 from the American Knights of Columbus that O'Connor brought for the poor of the Central American country.

The two new Canadian cardinals are Louis-Albert Vachon, 73, and Edourd Gagnon, 67.

Among the dignitaries present were Imelda Marcos, wife of Philippine President Ferdinand Marcos, and Roman Catholic Church leaders from all over, including the Polish primate, Cardinal Jozef Glemp, Cardinal Joseph Bernardin of Chicago and Noble Peace prize winner Mother Teresa of India.

The new cardinals will join the pope in celebrating a special Mass in St. Peter's Basilica on Sunday, before the pope leaves for Salerno south of Naples for a half-day visit.