Ionic2使用--prod参数构建Android应用失败求助
Hey there, let's work through this production build issue you're hitting. When you run ionic cordova build android --prod, the build fails during the ngc (Angular Compiler) step—this makes sense because the --prod flag enables Ahead-of-Time (AOT) compilation and strict type checking, which the regular dev build skips over. The error points to line 287, column 19 in your app.module.ts, so let's break down the most likely causes and fixes:
Common Causes & Troubleshooting Steps
1. Missing or Incorrect Type Annotations
AOT is way stricter about type safety than dev builds. If you have variables, function parameters, or return values that use implicit any types (no explicit type declared), ngc will throw an error here.
- Go straight to line 287 in
app.module.ts—look for code like untyped variables, or services/providers where dependencies aren't properly typed. - Example: If you have
let myVar;instead oflet myVar: string;, AOT will flag this. Fix it by adding explicit types. - You can also enable
noImplicitAny: truein yourtsconfig.jsonto catch these issues during dev builds too.
2. Invalid Module Imports or Declarations
Double-check that the code at line 287 is importing a module correctly, or that the module is properly declared in your app.module.ts:
- If it's an import statement, verify the file path is correct (no typos, relative paths are right).
- Make sure any module you're importing is actually exported from its source file (e.g.,
export class MyCustomModule {}). - Confirm the module is added to the
importsarray inapp.module.tsif it's a feature module.
3. Circular Dependencies
AOT hates circular references—if your app.module.ts imports a module that also imports app.module.ts (directly or indirectly), this will break the build.
- Manually trace the imports for the code at line 287, or use a tool like
madgeto detect circular dependencies in your project. - Refactor the code to remove the cycle—for example, move shared logic to a separate utility module that both can import.
4. Incomplete Error Message
First things first: You only shared the start of the error. The full message will tell you exactly what's wrong (e.g., "Property 'xyz' does not exist on type 'ABC'" or "Cannot find module 'my-module'").
- Re-run the
--prodbuild and copy the entire error output—this is the fastest way to pinpoint the issue.
5. Cache or Build Artifact Issues
Sometimes stale build files can cause weird AOT errors. Try cleaning your project first:
ionic cordova clean android rm -rf node_modules npm install
Then re-run the production build.
6. Test AOT Compilation Directly
Skip the Ionic wrapper and run the Angular compiler directly to get more detailed errors:
node_modules/.bin/ngc -p tsconfig.json
This will run just the AOT step and show you any hidden issues that the Ionic build might truncate.
Example Scenario
Suppose line 287 in app.module.ts looks like this:
providers: [UserService]
The error might be that UserService has a constructor dependency (like HttpClient) that isn't properly imported or injected, or the service itself has a type error. Checking the full error message would immediately tell you if it's a missing import or type mismatch.
内容的提问来源于stack exchange,提问作者tayyab_fareed

