HI my name is Cristian
Problem Description
Hi my name is Cristian,I am developing an application using Capacitor on macOS. I am using Vite as the web development framework and Android Studio to deploy the app to an Android device. While everything works perfectly in the Vite environment, I am encountering issues when running the app on an Android device through Android Studio, specifically with module resolution, Android permissions, and the @capacitor/network plugin listeners.
Errors Encountered
- Module Resolution Error for @capacitor/core:
In the Android Studio Logcat console, the following error appears:
TypeError: Failed to resolve module specifier ā@capacitor/coreā. Relative references must start with either ā/ā, ā./ā, or āā¦/ā.
This error indicates that the @capacitor/core module is not being resolved correctly in the Android native environment. The module works perfectly in the Vite environment.
- Listener Registration Error for networkStatusChange:
Even though I implemented a listener for the networkStatusChange event in the @capacitor/network plugin, the following message appears in Logcat:
Notifying listeners for event networkStatusChange
No listeners found for event networkStatusChange
This suggests that the plugin attempts to notify changes in network status but does not recognize any registered listeners, even though the listener is configured in the code.
- Potential Issue with Android Permissions:
I have reviewed the Capacitor documentation and followed tutorials to ensure all required Android permissions for the @capacitor/network plugin are set up. Despite this, the issue persists, suggesting that I might have missed something critical in the configuration.
Development Environment
- Operating System: macOS
- Web Framework: Vite
- IDE: Android Studio
- Test Platform: Physical Android Device
- Capacitor:
- @capacitor/cli: 6.2.0
- @capacitor/core: 6.2.0
- @capacitor/network: 6.0.3
Code Implementation
Listener Registration:
import { Network } from '@capacitor/network';
const initializeApp = async () => {
// Register the listener for network status changes
Network.addListener('networkStatusChange', (status) => {
console.log('Network status changed:', status);
});
// Get the current network status
const status = await Network.getStatus();
console.log('Current network status:', status);
};
// Call the initialization function
initializeApp();
Steps Taken
-
Early Listener Registration:
- Ensured that the listener is registered during the initialization of the app.
- Verified that the listener remains active during application lifecycle changes (pause, resume, etc.).
-
Full Synchronization:
- Ran
npx cap sync
to ensure the plugins are correctly synced with the native platforms.
- Ran
-
Route Validation:
- Verified that the import paths (@capacitor/core, @capacitor/network) are correct and work perfectly in the Vite environment.
-
Capacitor Configuration:
- capacitor.config.json file:
{
"appId": "com.testCapacitor.app",
"appName": "app-nativa",
"webDir": "www",
"bundledWebRuntime": false
}
- Android Permissions:
- Added the following permissions to the AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
- Verified that the permissions are declared correctly, but the issue persists.
- Additional Testing:
- Tried setting
"bundledWebRuntime": true
, but it did not resolve the listener issue. - Updated Capacitor and all plugins to their latest versions.
- Tried setting
Expectation
I am seeking guidance on the following:
- Why the @capacitor/core module is not resolving correctly on Android.
- Why the @capacitor/network plugin does not recognize the registered listeners.
- Whether there are additional configurations needed to make Capacitor compatible with Vite and Android Studio.
- If there are any specific Android permissions or configuration steps I might have missed.
- If there are any known issues with the current versions of Capacitor or the @capacitor/network plugin.
Appreciation
Thank you in advance for any guidance or solutions you can provide to resolve these issues. I am happy to share further details or perform additional testing if necessary.