6

I'm trying to create native Java package for my simple React Native application.

I created project with react-native init, added some screens and react-navigation.

After that I followed The Toast Module.

My phone is connected via USB and every React code runs perfectly but Java code not.

In one screen in render() method I added simple

import React, { Component } from "react"
import { NativeModules } from "react-native"

export default class TestScreen extends Component {
    render() {
        console.log(NativeModules, NativeModules.ToastModule)

        return (
            // something
        )
    }
}

In result of this code I got object with all native modules provided by React Native and undefined value for my personal module.

I go to file android/app/src/main/java/com/myproject/MainApplication.java and replace getPackages() method into this

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList( // Disable all modules for example
        // new MainReactPackage(),
        // new ManagerPackage()
    );
}

Normally both packages are uncommented.

After that I tried to run application on my phone

$ react-native run-android --deviceId MYDEVICEID

Some part of console output is

Building the app...

BUILD SUCCESSFUL in 8s
52 actionable tasks: 2 executed, 50 up-to-date

Application starts but I got same result for console.log(...) - object and undefined values.

When I executed

$ cd android/
$ ./gradlew clean
$ cd ..
$ react-native run-android --deviceId ...

I got error that file under app/build/outputs/apk/app-debug.apk not exists.

When I followed this answer I also had to copy android/app/build/outputs/apk/debug/app-debug.apk file into android/app/build/outputs/apk/app-debug.apk.

It's not solves my problem - my native module is not working when I uncomment my package in MainApplication.java file and also I cannot disable MainReactPackage.

My question is: how to successfully can I rebuild my project (including Java files for Android build)?

ventaquil
  • 2,780
  • 3
  • 23
  • 48
  • 2
    Too bad this question does not have an answer. I am going through the same crap right now. So frustrating. Followed the tutorial to the key from the React Native site and can't get anything to work. – Dan Zuzevich Sep 29 '18 at 13:07

0 Answers0