如何用Ionic 3开发可接入移动端的SDK库并发布至Maven?
Hey Jerry! Let’s break down each of your questions with practical, Ionic 3-specific steps—since you’re working with an older version, we’ll stick to tools and workflows that play nicely with it.
1. Creating an Ionic 3 Library Importable by Mobile Apps
First, clarify: if you’re targeting other Ionic/Angular apps, you’ll build a reusable Angular library (Ionic 3 is tied to Angular 5). If you need this library to work with native Android/iOS apps, skip to the next section (we’ll cover wrapping it as a native plugin there). Here’s how to build the library for Ionic apps:
- Start with an Angular workspace: Use Angular CLI v1.7.x (compatible with Angular 5) to create a workspace for your library:
ng new ionic-lib-workspace --createApplication=false cd ionic-lib-workspace ng generate library my-ionic-shared-lib - Add Ionic 3 dependencies: In your library’s
package.json, addionic-angularand@angular/core@5.xas peer dependencies (so consumers of your library already have these installed, avoiding version conflicts). - Build your library code: Add components, services, or payment logic in the
src/libfolder. Make sure to import Ionic modules likeIonicModulein your library’s module file where needed. - Compile and publish: Run
ng build my-ionic-shared-libto generate the compiled library in thedistfolder. You can publish this to npm withnpm publish dist/my-ionic-shared-lib, or use it locally vianpm linkfor testing. - Import into another Ionic app: In the target Ionic 3 app, install the library via npm, then import its module in
app.module.tsto use the components/services.
2. Turning Your Ionic Payment Feature into an Android/iOS Importable SDK
Native Android/iOS apps can’t directly run Ionic’s web-based code, so you’ll need to wrap your payment functionality into a Cordova plugin (the standard for Ionic 3) or a self-contained native SDK with a WebView. Here’s the Cordova approach:
- Scaffold a Cordova plugin: Install
plugman(Cordova plugin generator) and create your plugin:npm install -g plugman plugman create --name PaymentSDK --plugin_id com.yourcompany.paymentsdk --plugin_version 1.0.0 cd PaymentSDK - Add platform support: Set up Android and iOS platform folders:
plugman platform add --platform_name android plugman platform add --platform_name ios - Integrate your Ionic payment code:
- Drop your compiled Ionic payment logic (vanilla JS or bundled Angular code) into the plugin’s
wwwfolder. - Write native bridge code: For Android, edit
src/android/PaymentSDK.javato create a Cordova plugin class that either loads your web payment UI in a WebView or exposes native methods that trigger your web logic via the Cordova bridge. Do the same for iOS insrc/ios/PaymentSDK.m.
- Drop your compiled Ionic payment logic (vanilla JS or bundled Angular code) into the plugin’s
- Test the plugin: Add it to a test Ionic 3 app with
ionic cordova plugin add ../PaymentSDK, then call the plugin’s JS methods to verify payment flows work. - Package for native apps:
- For Android: Extract the compiled AAR from the plugin’s Android build, or build it directly in Android Studio. This AAR can be imported into any Android app as a module.
- For iOS: Build the plugin into a Framework using Xcode, which can be added to iOS projects via CocoaPods or manual import.
- Alternatively, create a wrapper SDK that provides simple native APIs (e.g.,
launchPaymentFlow()in Kotlin/Swift) that handle launching the WebView and passing callbacks between native and web code.
3. Publishing an Ionic-Based Library to Maven
Maven is built for Java/Android artifacts (like AARs, JARs), so you can’t publish the raw Ionic web library directly. But you can publish the native Android wrapper of your Ionic payment library to Maven for easy integration:
- Package your Ionic functionality into an Android AAR: As above, build the Cordova plugin’s Android component into an AAR, or create a standalone Android library that embeds your Ionic web assets (HTML/CSS/JS) and loads them in a WebView with native bridge logic.
- Configure Maven publishing: In your Android library’s
build.gradle, add the Maven publishing plugin and set up details like groupId, artifactId, version, and your target Maven repository (private Nexus, Maven Central, etc.). - Publish: Run
./gradlew publishto upload the AAR to your Maven repo. Android apps can then add it as a Gradle dependency like any other Maven-hosted library.
Note: For iOS, you’d use CocoaPods instead of Maven, but your question focused on Maven—so the above covers the Android side fully.
内容的提问来源于stack exchange,提问作者Jerry4Christ

