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

feat: automatically import Android plugins #3788

Merged
merged 48 commits into from
Dec 2, 2020
Merged

feat: automatically import Android plugins #3788

merged 48 commits into from
Dec 2, 2020

Conversation

imhoffd
Copy link
Contributor

@imhoffd imhoffd commented Nov 10, 2020

This is an implementation of the Android Plugin Auto-registration Proposal.

TODO:

  • Support adding additional classes during initialization (e.g. plugins from the app itself)
  • Make it backwards compatible with @NativePlugin (?)
  • Think about error handling when reflection fails

Internally, the CLI creates a capacitor.plugins.json file inside android/app/src/main/assets (alongside capacitor.config.json) which is meant to be shipped in the final Android bundled app.

This file contains a list of classpaths to the Capacitor plugins it finds during npx cap update android. It looks like this:

[
        {
                "pkg": "@capacitor/action-sheet",
                "classpath": "com.capacitorjs.plugins.actionsheet.ActionSheetPlugin"
        },

        ...
]

Then, in Java, we use Reflection to load classes via Class.forName().

This simplifies the app developer workflow because they no longer need to use add() to add the class names to the list. MainActivity.java in most apps can look like this:

public class MainActivity extends BridgeActivity { }

If they want to add app plugins, they need to do something like this:

public class MainActivity extends BridgeActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    registerPlugin(CustomPlugin.class);
  }
}

depends on #3866
depends on #3762
resolves #3266

@imhoffd imhoffd added this to the 3.0.0 milestone Nov 10, 2020
@imhoffd imhoffd marked this pull request as ready for review December 1, 2020 00:10
@imhoffd imhoffd merged commit aa1e1c6 into main Dec 2, 2020
@imhoffd imhoffd deleted the auto-android branch December 2, 2020 00:00
@imhoffd imhoffd mentioned this pull request Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically import Android plugins
3 participants