Skip to content

MainApplication cannot be converted to ReactNativeHost List<ReactPackage> packages = new PackageList(this).getPackages(); #467

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

Closed
erennyuksell opened this issue Jul 3, 2019 · 25 comments · Fixed by #493
Assignees
Labels
bug Something isn't working

Comments

@erennyuksell
Copy link

erennyuksell commented Jul 3, 2019

Environment

System:
OS: Linux 4.15 Linux Mint 19.1 (Tessa)
CPU: (4) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Memory: 269.00 MB / 4.74 GB
Shell: 4.4.19 - /bin/bash
Binaries:
Node: 10.16.0 - /usr/local/bin/node
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
react: 16.8.6 => 16.8.6
react-native: 0.60.0 => 0.60.0
npmGlobalPackages:
react-native-cli: 2.0.1

Description

I think PackageList expects ReactNativeHost but i'am using react-native-navigation
When i run react-native run-android command i get this error
https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
/home/rn/Desktop/denetmenapp/android/app/src/main/java/com/denetmen/MainApplication.java:46: error: incompatible types: MainApplication cannot be converted to ReactNativeHost
List packages = new PackageList(this).getPackages();
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 17s

private ReactNativeHost reactNativeHost;
  public PackageList(ReactNativeHost reactNativeHost) {
    this.reactNativeHost = reactNativeHost;
  }

import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;

public class MainApplication extends NavigationApplication {
    
    @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), 

    protected List<ReactPackage> getPackages() {
    
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
     
        packages.add( new DocumentScannerPackage());
        packages.add( new CodePush("KEY", getApplicationContext(), isDebug()));

        return packages;
       
    }
}

Reproducible Demo

import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;

public class MainApplication extends NavigationApplication {
    
    @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), 

    protected List<ReactPackage> getPackages() {
    
        @SuppressWarnings("UnnecessaryLocalVariable")
        List<ReactPackage> packages = new PackageList(this).getPackages();
     
        packages.add( new DocumentScannerPackage());
        packages.add( new CodePush("some code thing here”, getApplicationContext(), isDebug()));

        return packages;
       
    }
}
@mikehardy
Copy link
Contributor

You can disable auto-link for any given package for android like so (change package name) in root of your app:

https://github.com/mikehardy/rn-androidx-demo/commit/51d1e70fe074af29c1d1f8948d8f03b76e828b9f#diff-c8e33fe90e78d567b713e8dab48e5d87R56

echo "module.exports = { dependencies: { 'react-native-fbsdk': { platforms: { android: undefined } } } };" > react-native.config.js

@erennyuksell
Copy link
Author

@mikehardy i think this line List packages = new PackageList(this).getPackages() should never work because it's expect ReactNativeHost but when we use react-native-navigation this object (MainApplication) not ReactNativeHost because it's extends from NavigationApplication not extends ReactActivity. Not sure maybe i got it wrong. I need to disable it completely for all packages, not disabled for one package so that the problem can be solved.

@erennyuksell
Copy link
Author

erennyuksell commented Jul 4, 2019

PackageList(this).getPackages() this call causing problem. The this object sent is not the ReactNativeHost object. Sorry for my bad english.

@darknblack
Copy link

I knew it, there would be an issue with react native navigation. Hopefully It would get solved quickly.

@thymikee
Copy link
Member

thymikee commented Jul 4, 2019

Cc @michalchudziak @Salakar @krizzu @ferrannp ideas on how to resolve it on our or native navigation side? It's gonna be pretty common with brownfield apps I guess.

@guyca
Copy link

guyca commented Jul 4, 2019

No need to override createReactGateway - just override the method which creates the host:

 @Override
    protected ReactNativeHost createReactNativeHost() {
        return new NavigationReactNativeHost(this) {
            @Override
            protected String getJSMainModuleName() {
                return "index";
            }

            @Override
            protected List<ReactPackage> getPackages() {
                @SuppressWarnings("UnnecessaryLocalVariable")
                List<ReactPackage> packages = new PackageList(this).getPackages();
     
                packages.add( new DocumentScannerPackage());
                packages.add( new CodePush("eenqtOmwlGFR_I1wtLjtWujPyDpzSkjLt9tCV", getApplicationContext(), isDebug()));

                return packages;
            }
        };
    }

@thymikee
Copy link
Member

thymikee commented Jul 4, 2019

Maybe I'm wrong, but it seems that these private methods are not really used (unless getters have side effects I'm not aware of):

private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
}
private Resources getResources() {
return this.getApplication().getResources();
}
private Application getApplication() {
return this.reactNativeHost.getApplication();
}
private Context getApplicationContext() {
return this.getApplication().getApplicationContext();
}

If that's true, we could remove passing the ReactNativeHost entirely.

@erennyuksell
Copy link
Author

erennyuksell commented Jul 4, 2019

@guyca can you remove CodePush key on your comment and i will try it remove override createReactGateway and let you know if the problem is resolved.

@erennyuksell
Copy link
Author

@thymikee maybe using here
new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG), but it's throw error and i remove autolinking for codepush and add manuel

/home/rn/Desktop/denetmenapp/android/app/build/generated/rncli/src/main/java/com/facebook/react/PackageList.java:72: error: cannot find symbol
new CodePush(getResources().getString(R.string.reactNativeCodePush_androidDeploymentKey), getApplicationContext(), BuildConfig.DEBUG),
^
symbol: variable reactNativeCodePush_androidDeploymentKey
location: class string
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors

@erennyuksell
Copy link
Author

@guyca i'am getting this error now
/home/rn/Desktop/denetmenapp/android/app/src/main/java/com/denetmen/MainApplication.java:19: error: MainApplication is not abstract and does not override abstract method createAdditionalReactPackages() in NavigationApplication public class MainApplication extends NavigationApplication {

here is my full MainApplication.java

package com.denetmen;

import android.app.Application;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import androidx.multidex.MultiDex;
import com.documentscanner.DocumentScannerPackage;
import android.content.Context;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;
import java.util.Arrays;
import java.util.List;
import com.microsoft.codepush.react.CodePush;

public class MainApplication extends NavigationApplication {
    @Override
    protected ReactNativeHost createReactNativeHost() {
        return new NavigationReactNativeHost(this) {
            @Override
            public boolean getUseDeveloperSupport() {
                return BuildConfig.DEBUG;
            }

            @Override
            protected String getJSMainModuleName() {
                return "index";
            }

            @javax.annotation.Nullable
            @Override
            protected String getJSBundleFile() {
                return CodePush.getJSBundleFile();
            }

            @Override
            protected List<ReactPackage> getPackages() {
                @SuppressWarnings("UnnecessaryLocalVariable")
                List<ReactPackage> packages = new PackageList(this).getPackages();

                packages.add(new DocumentScannerPackage());
                packages.add(new CodePush("KEY", getApplicationContext(), isDebug()));

                return packages;
            }
        };
    }

    @Override
    public boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}

@Salakar
Copy link
Member

Salakar commented Jul 5, 2019

Maybe I'm wrong, but it seems that these private methods are not really used (unless getters have side effects I'm not aware of):

private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
}
private Resources getResources() {
return this.getApplication().getResources();
}
private Application getApplication() {
return this.reactNativeHost.getApplication();
}
private Context getApplicationContext() {
return this.getApplication().getApplicationContext();
}

If that's true, we could remove passing the ReactNativeHost entirely.

They were added for backwards compatibility as some modules use them, e.g. : https://github.com/microsoft/react-native-code-push/blob/master/package.json#L45

@darknblack
Copy link

Maybe I'm wrong, but it seems that these private methods are not really used (unless getters have side effects I'm not aware of):

private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
}
private Resources getResources() {
return this.getApplication().getResources();
}
private Application getApplication() {
return this.reactNativeHost.getApplication();
}
private Context getApplicationContext() {
return this.getApplication().getApplicationContext();
}

If that's true, we could remove passing the ReactNativeHost entirely.

They were added for backwards compatibility as some modules use them, e.g. : https://github.com/microsoft/react-native-code-push/blob/master/package.json#L45

Hi, just wondering whether you were able to run react native navigation in the latest version or RN (0.6)?

@Salakar
Copy link
Member

Salakar commented Jul 5, 2019

Hey all, could you try this patch locally (change in node_modules):

https://github.com/react-native-community/cli/compare/@salakar/android-al-mainapplication?expand=1#diff-375026a0607eb034a6fc70cca7d74689

If all works I'll submit the PR

@Salakar Salakar self-assigned this Jul 5, 2019
@thymikee
Copy link
Member

thymikee commented Jul 5, 2019

Thanks @Salakar!

@erennyuksell
Copy link
Author

@Salakar I managed to run

@Salakar
Copy link
Member

Salakar commented Jul 6, 2019

@Frekansapp thanks for confirming - PR is now up: #493

@arryanggaputra
Copy link

@thymikee Hi man, did you solve the issue?

@thymikee
Copy link
Member

Yup, update the @react-native-community/cli to the latest version to get the fix.

@vgvishal538
Copy link

@Salakar I am using react-native-community/cli-platform-android "version": "2.8.2" but still facing issue can you please help with this

@vgvishal538
Copy link

@Salakar ```{{ packageImports }}
public class PackageList {
private Application application;
private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}

public PackageList(Application application) {
this.reactNativeHost = null;
this.application = application;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
@@ -31,6 +37,7 @@ public class PackageList {
}
private Application getApplication() {
if (this.reactNativeHost == null) return this.application;
return this.reactNativeHost.getApplication();
}```

@juroMin
Copy link

juroMin commented Aug 13, 2019

the same with @react-native-community/[email protected] and react-native 0.60.4

/android/app/src/main/java/com/emphy/MainApplication.java:7: error: cannot find symbol
import com.facebook.react.PackageList;
                         ^
  symbol:   class PackageList
  location: package com.facebook.react
/Users/irynapolishchuk/mobile/android/app/src/main/java/com/emphy/MainApplication.java:45: error: cannot find symbol
      List<ReactPackage> packages = new PackageList(this).getPackages();
                                        ^
  symbol: class PackageList
2 errors

FAILURE: Build failed with an exception.

@thymikee
Copy link
Member

Have you tried yarn react-native run-android --tasks clean or cd android && ./gradlew clean and then building again?

@Linoa65
Copy link

Linoa65 commented Aug 30, 2019

Hello !

Same here, unable to build on RN 0.60.4 with error 'cannot find symbol PackageList'
I'm currently upgrading my app, it's such a pain to resolve all conflict/dependencies problems...

@mikehardy
Copy link
Contributor

@Linoa65 examine line 6 from the android/app/src/main/java/com/rndiffapp/MainApplication.java file here https://react-native-community.github.io/upgrade-helper/?from=0.59.10&to=0.60.4

I believe you are just missing the import based on your error message, and in general the upgrade helper https://react-native-community.github.io/upgrade-helper/ site is fantastic for upgrades

@adityajangle

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet