run-android gives error when connected to a device

半腔热情 提交于 2019-12-10 10:32:49

问题


I added a splash screen,https://medium.com/handlebar-labs/how-to-add-a-splash-screen-to-a-react-native-app-ios-and-android-30a3cec835ae, to the AwesomeProject example to Getting Started ( https://facebook.github.io/react-native/docs/getting-started.html).

I connected my nexus 6p and ran react-native run-android and I am getting this error

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html
Running /home/Android/Sdk/platform-tools/adb -s 84B7N15A10013562 reverse tcp:8081 tcp:8081
Starting the app on 84B7N15A10013562 (/home/Android/Sdk/platform-tools/adb -s 84B7N15A10013562 shell am start -n com.testprj1/.MainActivity)...
Starting: Intent { cmp=com.testprj1/.MainActivity }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.testprj1/.MainActivity } from null (pid=23823, uid=2000) not exported from uid 10161
        at android.os.Parcel.readException(Parcel.java:1684)
        at android.os.Parcel.readException(Parcel.java:1637)
        at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:3137)
        at com.android.commands.am.Am.runStart(Am.java:643)
        at com.android.commands.am.Am.onRun(Am.java:394)
        at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
        at com.android.commands.am.Am.main(Am.java:124)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:262)

But, if i run same on an emulator there is no errors


回答1:


According to the tutorial you posted, your new launcher activity is SplashActivity and not the MainActivity. But by default react-native cli tries to launch MainActivity which causes this permission error. Read the code in runAndroid.js file at this path; your_project/node_modules/react-native/local-cli/runAndroid.js. Also check these commands which you can use while launching the app.

module.exports = {
  name: 'run-android',
  description: 'builds your app and starts it on a connected Android 
  emulator or device',
  func: runAndroid,
  options: [{
       command: '--install-debug',
    },
    {
      command: '--root [string]',
      description: 'Override the root directory for the android build (which contains the android directory)',
      default: '',
    },
    {
      command: '--flavor [string]',
      description: '--flavor has been deprecated. Use --variant instead',
    },
    {
      command: '--variant [string]',
    },
    {
      command: '--main-activity [string]',
      description: 'Name of the activity to start',
      default: 'MainActivity',
    },
    {
      command: '--deviceId [string]',
      description: 'builds your app and starts it on a specific device/simulator with the given device id (listed by running "adb devices" on the command line).',
    },
    {
      command: '--no-packager',
      description: 'Do not launch packager while building',
  }],
};

It should not have worked on emulator as well. Try uninstalling the app from emulator and run again, it will not work.

Now to solve the issue run the following command in terminal

react-native run-android --main-activity SplashActivity

Notice that we are adding activity name to launch in the end using "--main-activity" command



来源:https://stackoverflow.com/questions/44209189/run-android-gives-error-when-connected-to-a-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!