Appium IOS tests running when server started manually but not when server is started via AppiumDriverLocalService

Attempting to run Appium automation scripts on iOS (simulator) on a Mac Mini (M1 chip, if that’s relevant). When I run the tests, they work just fine when the Appium server is started manually (typing “appium” into the terminal and starting it that way). However, when I attempt to start the appium server programmatically, the application under test fails to launch, with the following error:

2021-02-17 03:41:27:256 [W3C] (19077) encountered an error (Failed to load the test bundle. If you believe this error represents a bug, please attach the result bundle at /Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Logs/Test/Test-WebDriverAgentRunner-2021.02.16_22-41-22--0500.xcresult. (Underlying Error: **The bundle “WebDriverAgentRunner” couldn’t be loaded because it doesn’t contain a version for the current architecture. The bundle doesn’t contain a version for the current architecture. Try installing a universal version of the bundle.** dlopen_preflight(/Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/ no suitable image found.  Did find:
2021-02-17 03:41:27:256 [W3C] 	/Users/sagolGoru20/Library/Developer/Xcode/DerivedData/WebDriverAgent-gkbkvswlszzhhbevpokpwtrjdxxq/Build/Products/Debug-iphonesimulator/ mach-o, but wrong architecture))

Here’s the full appium log:

I originally thought that this was an issue with xcode or WDA, but if that were the case, the tests would fail no matter how Appium was started, and as mentioned previously, the tests pass perfectly fine when Appium is started manually (via terminal), so it might be an issue with the PATH or environment variables upon execution from Java, not sure though.

Here is the code I am using to start the server programmatically:

		HashMap<String, String> environment = new HashMap();
		environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));

		AppiumDriverLocalService server = AppiumDriverLocalService
				.buildService(new AppiumServiceBuilder()
						.usingDriverExecutable(new File("//opt/homebrew/Cellar/node/15.8.0/bin/node"))
						.withAppiumJS(new File("//Users/sagolGoru20/.npm-packages/lib/node_modules/appium/build/lib/main.js"))
						.withLogFile(new File("//Users/sagolGoru20/Programming/JavaProjects/MavenProjects/MobileAutomationProjects/firstAppiumProject/appiumLog.txt"))

As the appium log shows, the server seems to be started fine but then throws the above error, so I think the issue may be with how I’m building the AppiumDriverLocalService object

I followed this tutorial in building AppiumDriverLocalService:

Here is the project code. It’s a simple project, I’m just clicking on an “Allow” button (line 26):

Any help would be greatly appreciated.

Was able to solve it by setting all options under “Build Active Architecture only” to “no”, in XCode build settings. Details:

Hopefully this helps someone trying to run XCUITests on Mac devices with the new M1 chip.