如何用CircleCI构建Cordova Node.js应用生成APK/IPA/浏览器包及排查安卓构建问题
Here's a complete config.yml that covers building for Android, iOS, and browser platforms. Each platform runs in its own job (since iOS requires a macOS environment), and artifacts are stored for easy access.
version: 2.1 jobs: build-android: docker: # Use a CircleCI Android image with pre-installed SDK tools - image: cimg/android:2023.05.1 steps: - checkout - run: name: Update npm to latest version command: sudo npm install -g npm@latest - restore_cache: key: dependency-cache-{{ checksum "package.json" }} - run: name: Install Node.js dependencies command: npm install - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: - node_modules - run: name: Install Cordova globally command: sudo npm install -g cordova - run: name: Accept all Android SDK licenses command: yes | sdkmanager --licenses - run: name: Add Android platform to Cordova project command: cordova platform add android@latest --save - run: name: Build signed/release APK # --packageType=apk ensures APK is generated (default is AAB in newer Cordova versions) command: cordova build android --release -- --packageType=apk - store_artifacts: # Path to the generated APK path: platforms/android/app/build/outputs/apk/release/ destination: android-apk-artifacts build-ios: macos: # Use an Xcode version compatible with your Cordova iOS platform xcode: 14.3.1 steps: - checkout - run: name: Update npm to latest version command: npm install -g npm@latest - restore_cache: key: dependency-cache-{{ checksum "package.json" }} - run: name: Install Node.js dependencies command: npm install - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: - node_modules - run: name: Install Cordova globally command: npm install -g cordova - run: name: Install CocoaPods (required for iOS dependencies) command: sudo gem install cocoapods - run: name: Add iOS platform to Cordova project command: cordova platform add ios@latest --save - run: name: Build iOS IPA (for device deployment) # Note: You'll need to set up code signing via CircleCI's iOS Code Signing feature command: cordova build ios --release --device - store_artifacts: path: platforms/ios/build/device/ destination: ios-ipa-artifacts build-browser: docker: # Use a standard Node.js image - image: cimg/node:20.3.0 steps: - checkout - run: name: Update npm to latest version command: sudo npm install -g npm@latest - restore_cache: key: dependency-cache-{{ checksum "package.json" }} - run: name: Install Node.js dependencies command: npm install - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: - node_modules - run: name: Install Cordova globally command: sudo npm install -g cordova - run: name: Add Browser platform to Cordova project command: cordova platform add browser@latest --save - run: name: Build browser package command: cordova build browser --release - store_artifacts: path: platforms/browser/www/ destination: browser-build-artifacts workflows: build-all-platforms: jobs: - build-android - build-ios - build-browser
Key Notes for Each Platform:
- Android: The
--packageType=apkflag is critical because newer Cordova Android versions default to generating App Bundles (.aab) instead of APKs. The CircleCI Android image comes with most SDK tools, but if you need a specific SDK version, add a step likesdkmanager "platforms;android-33"(replace with your target SDK). - iOS: You must set up code signing (provisioning profiles and certificates) via CircleCI's iOS Code Signing settings in your project. Without this, the IPA build will fail.
- Browser: The build outputs a static
wwwdirectory that you can deploy directly to any web host.
Troubleshooting Android APK Generation Issues
If your existing Android config isn't producing an APK, here are the most common fixes:
Missing SDK License Acceptance:
The Android build will fail silently if licenses aren't accepted. Add this step before building:- run: name: Accept Android SDK licenses command: yes | sdkmanager --licensesDefaulting to App Bundle (AAB):
Newer Cordova Android versions generate AAB files by default. Force APK generation with:- run: name: Build APK command: cordova build android --release -- --packageType=apkMissing SDK Components:
Check your build logs for errors like "SDK platform X not found". Install the required platform with:- run: name: Install required Android SDK platform command: sdkmanager "platforms;android-33" # Replace with your target SDK versionDependency or Plugin Failures:
Ifnpm installorcordova platform addfails, the build won't proceed to generate the APK. Check the logs for errors during these steps—common issues include missing plugins or incompatible dependency versions.Incorrect Artifact Path:
Ensure you're storing artifacts from the correct path. For Cordova Android, the release APK is usually atplatforms/android/app/build/outputs/apk/release/.
Always check the full CircleCI build logs—they'll show exactly where the build is failing or why the APK isn't being generated.
内容的提问来源于stack exchange,提问作者Martand Jagadale

