Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npx cap sync android returns [error] Package could not be parsed from Android plugin. #4855

Closed
CarlosNano opened this issue Jul 20, 2021 · 22 comments · Fixed by #5098
Closed

Comments

@CarlosNano
Copy link

Bug Report

Capacitor Version

💊   Capacitor Doctor  💊

Latest Dependencies:

  @capacitor/cli: 3.1.1
  @capacitor/core: 3.1.1
  @capacitor/android: 3.1.1
  @capacitor/ios: 3.1.1

Installed Dependencies:

  @capacitor/cli: 3.1.1
  @capacitor/core: 3.1.1
  @capacitor/android: 3.1.1
  @capacitor/ios: 3.1.1

Platform(s)

Android

Current Behavior

When I run npx cap sync android I get the following error:

✔ Copying web assets from www to android/app/src/main/assets/public in 2.80s
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.76ms
✔ copy android in 2.83s
✔ Updating Android plugins in 13.18ms
[info] Found 1 Capacitor plugin for android:
[email protected]
✖ update android - failed!
[error] Package could not be parsed from Android plugin.
Location:
/Users/myUser/myPath/Sso.kt

Could you please point me to what it could be wrong?

When I compile the plugin on its own in Android studio I get no compilation errors.

Thanks

Expected Behavior

Run npx cap sync android with no errors

Other Technical Details

npm --version output: 7.8.0

node --version output: v14.11.0

@thomasvidas
Copy link
Contributor

If you don't include the plug-in in your Android build, re-run npx cap sync, and then re-include the plug-in, does this fix your issue? It sounds like you might have a misconfigured gradle file

@thomasvidas thomasvidas added the needs reply needs reply from the user label Jul 20, 2021
@CarlosNano
Copy link
Author

CarlosNano commented Jul 21, 2021

Hi @thomasvidas, thanks for your answer

I think it didn't work, these are the steps:

Uninstall plug-in:

> npm uninstall betta

removed 1 package in 2s

127 packages are looking for funding
  run `npm fund` for details

Sync:

> npx cap sync
✔ Copying web assets from www to android/app/src/main/assets/public in 2.01s
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.59ms
✔ copy android in 2.04s
✔ Updating Android plugins in 6.14ms
✔ update android in 44.67ms
[warn] Using the iOS project root for the public directory is deprecated.
       Please follow the Upgrade Guide to move public inside the iOS target directory:
       https://capacitorjs.com/docs/updating/3-0#move-public-into-the-ios-target-directory
✔ Copying web assets from www to ios/App/public in 1.92s
✔ Creating capacitor.config.json in ios/App/App in 1.10ms
✔ copy ios in 1.95s
✔ Updating iOS plugins in 6.88ms
✔ Updating iOS native dependencies with pod install in 7.37s
✔ update ios in 7.39s
✔ copy web in 678.67μp
✔ update web in 680.88μp
[info] Sync finished in 11.435s

(I get the warning for iOS because I haven't adapted the iOS plugins to capacitor 3)

Install plug-in:

> npm install betta@latest

added 1 package in 2s

127 packages are looking for funding
  run `npm fund` for details

Sync:

> npx cap sync
✔ Copying web assets from www to android/app/src/main/assets/public in 1.73s
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.04ms
✔ copy android in 1.76s
✔ Updating Android plugins in 6.43ms
[info] Found 1 Capacitor plugin for android:
       [email protected]
✖ update android - failed!
[error] Package could not be parsed from Android plugin.
        Location:
        /Users/myUser/myPath/IgnoreSelfSignedCertificateError.java

I have to say that the file which the error shows changes randomly

It sounds like you might have a misconfigured gradle file

You mean the gradle file in the plug-in or the gradle file in the project?

@Ionitron Ionitron removed the needs reply needs reply from the user label Jul 21, 2021
@CarlosNano
Copy link
Author

CarlosNano commented Jul 26, 2021

@thomasvidas any idea what's the problem?

Do you need any other info?

@CarlosNano
Copy link
Author

I've discovered that if I delete the file it's giving problems /Users/myUser/myPath/IgnoreSelfSignedCertificateError.java

npx cap sync android will work:

npx cap sync android
✔ Copying web assets from www to android/app/src/main/assets/public in 1.94s
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.14ms
✔ copy android in 1.96s
✔ Updating Android plugins in 7.39ms
[info] Found 1 Capacitor plugin for android:
       [email protected]
✔ update android in 45.42ms
[info] Sync finished in 2.013s

But if I keep doing npx cap sync android it will complain in the same way about another file after a couple of tries:

npx cap sync android
✔ Copying web assets from www to android/app/src/main/assets/public in 2.22s
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.12ms
✔ copy android in 2.25s
✔ Updating Android plugins in 7.29ms
[info] Found 1 Capacitor plugin for android:
       [email protected]
✖ update android - failed!
[error] Package could not be parsed from Android plugin.
        Location: /Users/myUser/myPath/ssoAndLogin/Sso.kt

@thomasvidas
Copy link
Contributor

Hmm...This sounds like an issue specific to your project and not an issue with capacitor. If you can provide a minimal reproduction the team can take a better look at it to see if it is an issue on our side

@thomasvidas thomasvidas added the needs reproduction needs reproducible example to illustrate the issue label Jul 28, 2021
@Ionitron
Copy link
Collaborator

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks!
Ionitron 💙

@Ionitron Ionitron added the needs reply needs reply from the user label Jul 28, 2021
@CarlosNano
Copy link
Author

Thanks for your answer @thomasvidas

I've created a repo with an example of the problem: https://github.com/CarlosNano/error-importing-capacitor

It's a new project of the plugin in capacitor with a copy of the files we use in our project.

I've emptied the classes so I can make the repo public.

It happens the same problem as described before, but be aware that the error sometimes it's a bit random.

Let me know if you need anything else.

@Ionitron Ionitron removed the needs reply needs reply from the user label Jul 29, 2021
@thomasvidas thomasvidas removed the needs reproduction needs reproducible example to illustrate the issue label Jul 29, 2021
@jcesarmobile
Copy link
Member

Capacitor CLI only expects to have a Plugin class per plugin, but your sample plugin has three plugin classes.

The error is misleading, not sure why that happens, and only happens when you have at least a java plugin and a kotlin plugin. If having multiple java plugins or multiple kotlin plugins alone but not mixed, the error doesn't show up, but still, only one of the plugins will be registered as the CLI only expects to have one plugin per package.

@CarlosNano
Copy link
Author

CarlosNano commented Aug 4, 2021

Thanks a lot for taking a lot to this,

I assume when you talk about plugin classes you are referring to classes with the annotation @CapacitorPlugin.

What's the implication of having multiple classes but only one is registered in the CLI? The other classes won't be accessible from the Javascript Code? or the other classes will be ignored if there is any errors on them?

Is there any plans to adopt having multiple plugin classes per plugin?

Also I assume this is a problem introduced in Capacitor 3 because, I didn't have any errors in Capacitor 2 ( which I guess it would not mean the CLI registered multiple classes either )

@jcesarmobile
Copy link
Member

Capacitor 3 introduced the automatic plugin registration, but it expect to only have a plugin class per package, so in cases like yours where there are more than one, they won't automatically register, so plugin users will still have to add code in their MainActivity.java to register plugins. But since it will randomly register one of them will be hard for users to register the missing ones, not sure what happens if you register one that is already registered by Capacitor.

You can keep the init method in the MainActivity.java so you opt out of the automatic registration, but the CLI will still fail.

About the plans, no idea, we were not aware of anybody doing this and I don't think it's a common thing to do, so we will have to see if it's a common feature to add as it's not a trivial change to do.

@CarlosNano
Copy link
Author

CarlosNano commented Aug 4, 2021

But since it will randomly register one of them will be hard for users to register the missing ones, not sure what happens if you register one that is already registered by Capacitor.

Something worth to try :)

You can keep the init method in the MainActivity.java so you opt out of the automatic registration, but the CLI will still fail.

The CLI will still fail as long as I use Java and Kotlin, if I use only Kotlin, it should be ok, isn't it?

@jcesarmobile
Copy link
Member

the error will be gone, but it will just register one of them, so not 100% ok

@CarlosNano
Copy link
Author

Ok, I will try registering all of them, because as you said, the CLI will randomly register one of them and I assume there is no way to ask which one has been registered already.

Hopefully registering twice to that one plugin class will be ok 🤞

Thanks

@CarlosNano
Copy link
Author

and only happens when you have at least a java plugin and a kotlin plugin.

I've just updated the only class in Java to Kotlin, but I'm still getting the same error in the CLI when I do npx cap sync

(I also tried to remove the plugin, sync, add the plugin and sync )

Is there anything else I need to do so I don't get the error.

[error] Package could not be parsed from Android plugin.

@jcesarmobile
Copy link
Member

then maybe it was because of having different packages and not because of being java/kotlin, if I delete the java class the error goes away, but didn't try adding a kotlin class in that existing package

@CarlosNano
Copy link
Author

Mmm, the error is very random, I'm almost sure if you keep running npx cap sync after deleting the java class, in a couple of attempts you'll get the error again.

So I guess for your answer, it's not possible to have multiple plugin classes in a plugin?

@jcesarmobile
Copy link
Member

I tried multiple times (a lot) and didn't get an error once the java class was gone

@CarlosNano
Copy link
Author

You are right if I remove the class IgnoreSelfSignedCertificateError the error disappears.

But if I add it back converted as Kotlin, the error is back too :_( . Also I put all files in the same package, same error.

I've updated the code if you want to take a look.

@jcesarmobile
Copy link
Member

Looks like the problem is then having different packages per plugin, but I don't have more time to dedicate to this, for now our official stand is that we only support a plugin class per plugin package and that causes the issue. Will leave the issue open in case we add support for multiple plugin classes in the future.

@CarlosNano
Copy link
Author

That's great. Thanks for your help

@sosoba
Copy link
Contributor

sosoba commented Oct 5, 2021

I found this bug:
ae22e97
Regular expressions has not be reset in loop

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 10, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants