getsentry / sentry-capacitor

The official Sentry SDK for Capacitor
https://sentry.io
MIT License
116 stars 31 forks source link

Android: cannot find symbol method 'init' #689

Closed chris-si closed 4 weeks ago

chris-si commented 1 month ago

Environment

"@capacitor/*": "^6.0.0", "@sentry/capacitor": "^0.18.0", "@sentry/vue": "^7.114.0", "vue": "^3.4.31",

Steps to Reproduce

  1. Clone this repo
  2. Install dependencies via npm install
  3. (Rename .env.sample to .env and add the DSN)
  4. Run ionic cap build android
  5. Try to run the app via Android Studio

Expected Result

The code in MainActivity.java should work without any problems.

package io.ionic.starter.capacitor_sentry_test;

import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;
import io.sentry.capacitor.SentryCapacitor;
import java.util.ArrayList;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // sentry-capacitor plugin
      add(SentryCapacitor.class);
    }});
  }
}

Actual Result

This issue somehow seems related to #62, whose issue I also experienced but which was resolved after manually syncing the project with the Gradle files.

But now I get the following error for the code in MainActivity.java.

'init()' has private access in 'androidx.fragment.app.FragmentActivity'
.../capacitor-sentry-test/android/app/src/main/java/io/ionic/starter/capacitor_sentry_test/MainActivity.java:14: error: cannot find symbol
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
        ^
  symbol: method init(Bundle,<anonymous ArrayList<Class<? extends Plugin>>>)
lucas-zimerman commented 1 month ago

Thank you for the repro, we will investigate what is going on with the build error.

lucas-zimerman commented 4 weeks ago

Hi, the MainActivity code shown is only compatible with Capacitor 2, Capacitor 3 or newer automatically initializes capacitor, all you need is to use the following snippet:

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

    // Initializes the Bridge with the desired plugins
    this.registerPlugin(SentryCapacitor.class);
  }
}

If in your specific case the initialization of Capacitor is required, you can also add this.registerPlugin(SentryCapacitor.class); after super.onCreate

chris-si commented 4 weeks ago

Thanks, that solves this issue. Maybe I followed the docs a little too well in this case since it only mentions capacitor 3 but not capacitor 3 or newer. ;)