简体   繁体   中英

Debugging Android in custom libraries

I have just created a react-native library using react-native-create-library and imported it into my master react-native project.

There are some issues I'm having because (honestly) I lack the knowledge. The problem is that there are no errors (using logcat) and I don't know how I can debug the android part of my imported library.

Example

public class RNZappsCameraModule extends ReactContextBaseJavaModule
  implements ActivityEventListener {

  @ReactMethod
  public void myJavascriptMethod() {
    // I want a breakpoint here
    // cameraIntent initialization happens here

    try
    {
      currentActivity.startActivityForResult(cameraIntent, requestCode);
    }
    catch (ActivityNotFoundException e)
    {
      e.printStackTrace();
    }
  }

  @Override
  public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
  {
    // I want a breakpoint here
  }
}

The camera intent starts fine, but I believe onActivityResult is never hit. I know I could log everything and read it, but that won't explain why the result is never returned to my app since there are no errors in the first place.

Google and the official RN documentation are not being my friend at the moment, so please put me on the right track.

Found it. No rocket science here. I don't know how I managed to do it in the end...

Anyheeew, to give this question a reasonable answer for passers-by... First off, you need a react-native (master) project in order to actually run your library in a react-native context.

So create it and import your library. The easiest way to do this is by pushing your library into a git repository and adding your library in the package.json of you master project like this:

"react-native-your-package": "git+ https://your-git-url-here ",

Now install it: npm install react-native-your-package

In order to debug your library:

  • Open the android project of your react-native project in Android Studio
  • In menu => view => Tool window, click Build Variants
  • The new window displays the build types for you project and loaded modules
  • Click the Build Variant dropdown next to the module you want to debug and select 'debug'
  • Debug the master Android project
  • In the projects view, you can expand your module and place breakpoints where ever you like
  • Click the debug button and fix errors you never head of

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM