株式会社オブライト
Mobile Development2026-03-04

Capacitor Native Plugin Development Guide | Swift/Kotlin Integration

Complete guide to Capacitor native plugin development. Learn Swift/Kotlin integration, custom plugin creation procedures, Community Plugins utilization, and distribution methods. Practical development know-how from Oflight Inc.


Understanding Capacitor Plugin Architecture

Capacitor plugins provide a bridge for calling native functionality from JavaScript. Plugins consist of three layers: TypeScript/JavaScript web layer, Swift (iOS) native layer, and Kotlin/Java (Android) native layer. At Oflight Inc. in Shinagawa-ku, we believe understanding this three-layer architecture is the first step in plugin development. In the web layer, inherit the Plugin interface from `@capacitor/core` and write TypeScript type definitions for each method. Development teams in Minato-ku found that enriching type definitions enabled IDE autocomplete, improving development efficiency. In the native layer, inherit the CAPPlugin class in Swift/Kotlin and expose methods with @objc (Swift) or @PluginMethod (Kotlin) annotations. Projects in Shibuya-ku expressed amazement that implementing native API calls in Swift/Kotlin enabled seamless use from JavaScript. Development teams in Setagaya-ku leveraged Promise-based asynchronous APIs to achieve readable code avoiding callback hell. Projects in Meguro-ku built robust plugins with proper error handling and timeout processing. At Oflight in Shinagawa-ku, we provide detailed explanations of plugin architecture and best practices.

Setting Up Plugin Development Environment

Custom Capacitor plugin development requires a proper development environment. First, install `@capacitor/cli` and generate plugin boilerplate with `npx cap plugin:generate` command. Development teams in Ota-ku found that this single command auto-generates TypeScript definitions, Swift/Kotlin templates, package.json, README, etc., significantly shortening initial setup. iOS development requires macOS, Xcode, and CocoaPods or Swift Package Manager. Projects in Minato-ku used Xcode 15 and Swift 5.9 to leverage the latest Swift Concurrency features. Android development requires Android Studio, Kotlin, and Gradle. Development teams in Shibuya-ku used Android Studio Hedgehog and actively adopted Kotlin 1.9 new features. For local plugin testing, use `npm link` to link the plugin under development to the Capacitor app. Projects in Setagaya-ku built environments where plugin changes are reflected in the app in real-time by leveraging symbolic links. Development teams in Meguro-ku develop plugins and apps simultaneously using VSCode multi-root workspaces. At Oflight in Shinagawa-ku, we support optimal development environment construction.

Web Layer Implementation - TypeScript Interface Definition

In the plugin's web layer, define the interface for calling native functionality from JavaScript. Describe the plugin interface in `src/definitions.ts`, defining the types of arguments and return values for each method. Development teams in Minato-ku designed optional arguments and default values appropriately, providing user-friendly APIs. Write web implementation (browser environment fallback) in `src/web.ts`. Projects in Shibuya-ku provided simple implementations using localStorage for the web version, facilitating testing during development. In `src/index.ts`, register the plugin with Capacitor using registerPlugin. Development teams in Setagaya-ku leveraged namespaces to provide multiple plugins in one package. Enriching type definitions provides an excellent development experience for TypeScript users. Projects in Meguro-ku wrote detailed JSDoc comments so detailed descriptions appear in VSCode IntelliSense. Development teams in Ota-ku designed generic plugin interfaces using Generics, increasing reusability. At Oflight in Shinagawa-ku, we provide practical guidelines for TypeScript utilization.

iOS Layer Implementation - Swift Native Code

In the iOS plugin layer, implement native functionality in Swift. Create a class inheriting the CAPPlugin class in `ios/Plugin/Plugin.swift`. Development teams in Minato-ku leveraged Swift 5.9's new macro features to reduce boilerplate code. Add @objc attributes to each method to make them callable from JavaScript. Projects in Shibuya-ku unified @objc func naming conventions to achieve highly readable code. Use UIKit and Foundation frameworks to call native APIs. Camera plugins in Setagaya-ku implemented advanced shooting features using AVFoundation. For asynchronous processing, leverage DispatchQueue or Swift Concurrency's async/await. Location plugins in Meguro-ku implemented CLLocationManager delegate patterns to achieve continuous location acquisition. For error handling, use the CAPPluginCall reject method to communicate errors to the JavaScript side. Projects in Ota-ku defined detailed error codes and messages to facilitate debugging. For permission processing, add necessary descriptions to Info.plist and request at appropriate timing. At Oflight in Shinagawa-ku, we provide Swift implementation best practices.

Android Layer Implementation - Kotlin Native Code

In the Android plugin layer, implement native functionality in Kotlin or Java. Create a class inheriting the Plugin class in `android/src/main/java/.../Plugin.kt`. Development teams in Shibuya-ku leveraged Kotlin Coroutines to write asynchronous processing concisely. Add @PluginMethod annotations to each method to make them callable from JavaScript. Projects in Minato-ku appropriately set annotation parameters to finely control method behavior. Use various Android SDK APIs to call native APIs. File access plugins in Setagaya-ku supported scoped storage from Android 11 onward using Storage Access Framework. For asynchronous processing, leverage Kotlin Coroutines or RxJava. Network plugins in Meguro-ku achieved modern implementations combining Coroutines + Retrofit. For error handling, use the PluginCall reject method. Projects in Ota-ku appropriately placed try-catch blocks to prevent crashes. For permission processing, declare in AndroidManifest.xml and request at runtime with ActivityCompat. At Oflight in Shinagawa-ku, we provide Kotlin implementation best practices.

Plugin Testing and Debugging

In plugin development, thorough testing and debugging determine quality. For web layer testing, implement unit tests using Jest or Vitest. Development teams in Minato-ku also use TypeScript type tests to verify interface definition consistency. For iOS layer testing, use the XCTest framework. Projects in Shibuya-ku leveraged Xcode's test navigator and created comprehensive tests while checking coverage reports. For Android layer testing, use JUnit and Espresso. Development teams in Setagaya-ku created mock objects with Mockito to implement unit tests eliminating external dependencies. For real device and simulator testing, create sample apps. Projects in Meguro-ku prepared samples for each framework (React, Vue, Angular) in the example/ directory and thoroughly verified functionality. For debugging, leverage Xcode's LLDB debugger and Android Studio's Debugger. Development teams in Ota-ku efficiently identify bugs by combining breakpoints and console logs. At Oflight in Shinagawa-ku, we support testing strategies and debugging techniques.

Leveraging and Contributing to Community Plugins

Capacitor Community hosts numerous high-quality plugins developed by community members. The @capacitor-community organization has over 50 published plugins, enabling integration with popular services like Stripe, Firebase, Google Analytics, and Facebook Login. Startups in Shibuya-ku reduced in-house implementation effort by 80% by leveraging Community Plugins. EC apps in Minato-ku implemented payment functionality in a short timeframe using @capacitor-community/stripe. Development teams in Setagaya-ku forked existing plugins and customized them to meet specific requirements. Projects in Meguro-ku contributed in-house developed plugins to the Community organization, receiving high praise from other developers. Development teams in Ota-ku learned plugin development best practices by referencing Community Plugins source code. When selecting plugins, check GitHub star counts, last update dates, and issue response status. At Oflight in Shinagawa-ku, we support Community Plugins utilization know-how and contribution methods.

Plugin Distribution and Version Management

Publishing developed plugins to npm enables reuse in other projects and teams. Set the name in package.json in `@scope/capacitor-plugin-name` format. Development teams in Minato-ku managed as private packages using organization scopes, promoting internal reuse. For version management, adopt semantic versioning (semver). Projects in Shibuya-ku raised major versions for breaking changes, maintaining backward compatibility. Publish to npm registry with `npm publish` command. Development teams in Setagaya-ku built mechanisms to automatically publish on tag push in CI/CD pipelines. In README, include installation methods, usage examples, API specifications, and license information. Projects in Meguro-ku prepared detailed documentation and GIF animations to improve user experience. Maintain CHANGELOG and clearly state changes in each version. Development teams in Ota-ku adopted conventional commits and achieved automatic CHANGELOG generation. At Oflight in Shinagawa-ku, we provide plugin distribution and maintenance best practices.

Plugin Performance Optimization

Plugin performance directly impacts overall app user experience. To minimize bridge call overhead, avoid frequent small calls and adopt batch processing. Location plugins in Shibuya-ku reduced call counts by 90% by returning location data in arrays. For large data transfers, consider Base64 encoding or file path passing. Image processing plugins in Minato-ku drastically reduced memory usage by saving image data to the file system and returning only paths to JavaScript. Implement asynchronous processing appropriately to avoid blocking the main thread. File I/O plugins in Setagaya-ku achieved smooth UI experiences leveraging Swift's async/await and Kotlin's Coroutines. To prevent memory leaks, avoid strong reference cycles and properly implement deinit or onDestroy. Projects in Meguro-ku monitored memory usage using Instruments (iOS) and Android Profiler. Development teams in Ota-ku automated performance tests to prevent regressions. At Oflight in Shinagawa-ku, we provide specialized performance optimization knowledge.

Plugin Security Considerations

Plugin development requires proper security risk management. Thoroughly validate user input to prevent injection attacks. Financial app plugins in Minato-ku implemented input value sanitization and whitelist validation. Keep permissions to the necessary minimum and write clear descriptions in Info.plist and AndroidManifest.xml. Projects in Shibuya-ku separated camera and microphone permissions, requesting only necessary functionality. For handling sensitive data, use iOS Keychain or Android Keystore System. Authentication plugins in Setagaya-ku encrypted and saved token information in Keychain, achieving secure storage. Enforce HTTPS communication to prevent man-in-the-middle attacks. API client plugins in Meguro-ku implemented certificate pinning to strengthen communication security. Regularly check dependency vulnerabilities and update. Development teams in Ota-ku use Dependabot to automatically receive dependency update PRs. At Oflight in Shinagawa-ku, we provide security best practices.

Practical Plugin Development Examples

There is much to learn from actual plugin development examples. Healthcare apps in Minato-ku developed custom plugins integrating HealthKit and Google Fit, achieving cross-platform health data acquisition. Media apps in Shibuya-ku implemented video encoding plugins, enabling high-quality video processing within apps. IoT apps in Setagaya-ku developed Bluetooth LE plugins, achieving communication with smart devices. Retail apps in Meguro-ku implemented barcode scanner plugins with MLKit (iOS) and ML Kit (Android), achieving fast product recognition. Educational apps in Ota-ku implemented voice recognition plugins with Speech Framework (iOS) and Speech Recognizer (Android), providing pronunciation checking functionality. These examples leverage the latest native platform APIs to provide functionality difficult to achieve with web alone. At Oflight in Shinagawa-ku, we support plugin development optimized for your business requirements, referencing these practical examples.

Oflight's Plugin Development Support

Oflight Inc., based in Shinagawa-ku, Tokyo, professionally supports Capacitor native plugin development. We have handled numerous custom plugin development requests from companies throughout Tokyo including Minato-ku, Shibuya-ku, Setagaya-ku, Meguro-ku, and Ota-ku. Engineers with extensive Swift/Kotlin native development experience provide end-to-end support from requirements definition to design, implementation, testing, and distribution. We also support migration of existing Cordova plugins to Capacitor format, dramatically improving performance and maintainability by modernly rewriting in the latest Swift/Kotlin. We provide comprehensive services including plugin performance optimization, security audits, documentation creation, npm package publishing, and continuous maintenance. We also support contribution to Community Plugins, assisting with publication as open source projects. If you are considering Capacitor native plugin development, please contact Oflight in Shinagawa-ku. We will develop high-quality plugins that realize your business requirements.

Feel free to contact us

Contact Us