react-native-community / cli

The React Native Community CLI - command line tools to help you build RN apps
MIT License
2.42k stars 906 forks source link

On Android, app isn't opened automatically if applicationId is changed #2194

Closed pnthach95 closed 11 months ago

pnthach95 commented 12 months ago

Description

I change applicationId but keep package ID. I run yarn android --appId new.app.id but it throws Error type 3 and app isn't opened.

It doesn't happen on older RN versions.

Log on v0.72.7, notice Starting: Intent line and compare it to error log below

yarn run v1.22.21
$ react-native run-android --appId com.foo.barr
info JS server already running.
info Installing the app...

> Task :app:installDebug
Installing APK 'app-debug.apk' on 'Pixel_5_API_30(AVD) - 11' for :app:debug
Installed on 1 device.

BUILD SUCCESSFUL in 2s
38 actionable tasks: 2 executed, 36 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.foo.bar/com.foobar.MainActivity }

Steps to reproduce

  1. Run npx react-native init foobar
  2. Open android/app/build.gradle, change applicationId "com.foobar" to applicationId "com.foo.bar"
  3. Run yarn android --appId com.foo.bar
  4. It throws Error: Activity class {com.foo.bar/com.foo.bar.MainActivity} does not exist. and doesn't start app

React Native Version

0.73.0

Affected Platforms

Runtime - Android, Build - MacOS

Output of npx react-native info

System:
  OS: macOS 14.1.2
  CPU: (8) arm64 Apple M1
  Memory: 110.70 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.18.2
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.21
    path: /opt/homebrew/bin/yarn
  npm:
    version: 9.8.1
    path: /usr/local/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/pnthach/.rvm/rubies/ruby-2.7.5/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.0
      - iOS 17.0
      - macOS 14.0
      - tvOS 17.0
      - watchOS 10.0
  Android SDK:
    API Levels:
      - "30"
      - "31"
      - "33"
      - "34"
    Build Tools:
      - 28.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 33.0.0
      - 33.0.1
      - 33.0.2
      - 34.0.0
    System Images:
      - android-26 | Google APIs ARM 64 v8a
      - android-30 | Google APIs ARM 64 v8a
      - android-33 | Google APIs ARM 64 v8a
      - android-33 | Google Play ARM 64 v8a
      - android-34 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11076708
  Xcode:
    version: 15.0.1/15A507
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.9
    path: /usr/bin/javac
  Ruby:
    version: 2.7.5
    path: /Users/pnthach/.rvm/rubies/ruby-2.7.5/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.0
    wanted: 0.73.0
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

yarn run v1.22.21
$ react-native run-android
info Installing the app...

> Task :app:installDebug
Installing APK 'app-debug.apk' on 'Pixel_5_API_30(AVD) - 11' for :app:debug
Installed on 1 device.

BUILD SUCCESSFUL in 5s
43 actionable tasks: 14 executed, 29 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.foo.bar/.MainActivity }
Error type 3
Error: Activity class {com.foo.bar/com.foo.bar.MainActivity} does not exist.
✨  Done in 6.71s.

Reproducer

https://github.com/pnthach95/applicationIdError

thymikee commented 12 months ago

Yeah I can repro that. Seems related to #1951 and https://github.com/react-native-community/cli/issues/1884. Would you like to help us pin-point the issue and fix it?

pnthach95 commented 12 months ago

Hi @thymikee

cli-platform-android@11.3.10 in react-native@0.72.7

console.log(args.mainActivity) in tryLaunchAppOnDevice, result is MainActivity https://github.com/react-native-community/cli/blob/59e4dac7e56fb05f33508ff804c0eac7448c16a8/packages/cli-platform-android/src/commands/runAndroid/tryLaunchAppOnDevice.ts#L23-L27

cli-platform-android@12.1.1 in react-native@0.73.0

console.log(mainActivity) in tryLaunchAppOnDevice, result is .MainActivity https://github.com/react-native-community/cli/blob/a45a2d92c3d53dcb0cf7ba8be4860ad75ff06702/packages/cli-platform-android/src/commands/runAndroid/tryLaunchAppOnDevice.ts#L26-L30

So, mainActivity always includes . in 12.1.1, it doesn't in older versions

thymikee commented 11 months ago

This was addressed in #2195 and will hopefully land with RN 73.1

7dp commented 10 months ago

Hi Mike! Could you please provide some info on when the resolution for this issue get landed? Because I tried on RN 0.73.2 but the app still didn't get launched automatically 🥲.

woowalker commented 9 months ago

@thymikee need help when applicationId is diff from namespace, or applicationId must match namespace?

geydson commented 8 months ago

@thymikee need help when applicationId is diff from namespace, or applicationId must match namespace?

I have the same problem, I have an application in version 0.64.4, and now I'm updating it to version 0.73.4, with this new "namespace" variable, if I leave the "applicationId" different I get the error below:

CommandError: Couldn't open Android app with activity "br.com.integralservices.development.mob/.MainActivity" on device "Pixel_4_API_34". The app might not be installed, try installing it with: npx expo run:android -d Pixel_4_API_34

Code in current version 0.73.4:

namespace "com.integralservices" defaultConfig { applicationId "br.com.integralservices.development.mob" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0"

     buildConfigField("boolean", "REACT_NATIVE_UNSTABLE_USE_RUNTIME_SCHEDULER_ALWAYS", (findProperty("reactNative.unstable_useRuntimeSchedulerAlways") ?: true).toString())
 }

 flavorDimensions "env"
 productFlavors {
     production {
         dimension "env"
         applicationId "br.com.integralservices.production.mob"
     }
     development {
         dimension "env"
         applicationId "br.com.integralservices.development.mob"
     }
     staging {
         dimension "env"
         applicationId "br.com.integralservices.staging.mob"
     }
 }

Code in old version 0.64.4:

 defaultConfig {
     applicationId "br.com.integralservices.development.mob"
     minSdkVersion rootProject.ext.minSdkVersion
     targetSdkVersion rootProject.ext.targetSdkVersion
     versionCode 1
     versionName "1.0"
 }

 flavorDimensions "env"
 productFlavors {
     production {
         dimension "env"
         applicationId 'br.com.integralservices.production.mob'
     }
     development {
         dimension "env"
         applicationId 'br.com.integralservices.development.mob'
     }
     staging {
         dimension "env"
         applicationId 'br.com.integralservices.staging.mob'
     }
 }

In the old version it runs without problems in the emulator, but in this one I couldn't solve this, did you find any solution?

geydson commented 8 months ago

@thymikee need help when applicationId is diff from namespace, or applicationId must match namespace?

Could @thymikee answer this question for us?

TMisiukiewicz commented 8 months ago

@woowalker @geydson applicationId doesn't have to match namespace . You can have different applicationIds for multiple versions of your app as shown on the snippet above. Make sure the namespace matches the package name in MainActivity.kt.

When using product flavors, you can try using --mode flag to indicate the flavor, e.g. yarn android --mode developmentDebug. You can also list all available tasks by using --interactive flag.

@geydson

CommandError: Couldn't open Android app with activity "br.com.integralservices.development.mob/.MainActivity" on device "Pixel_4_API_34". The app might not be installed, try installing it with: npx expo run:android -d Pixel_4_API_34

your output indicates you are using Expo, if the problem still occurs, I'd suggest to open an issue in Expo repository.

7dp commented 7 months ago

Hi @TMisiukiewicz at what version this fix landed?

7dp commented 7 months ago

Upgrading my app to 0.73.7 doesn't solve this issue 🥲

pnthach95 commented 7 months ago

Upgrading my app to 0.73.7 doesn't solve this issue 🥲

If it doesn't work on yours, show your error log

7dp commented 7 months ago

My error log is similar like yours @pnthach95 :

Error: Activity class {com.foo.bar/com.foo.bar.MainActivity} does not exist.
pnthach95 commented 7 months ago

My error log is similar like yours @pnthach95 :

Error: Activity class {com.foo.bar/com.foo.bar.MainActivity} does not exist.

I upgrade my reproducer to 0.73.7 and it can launch app without any problems

https://github.com/react-native-community/cli/assets/31266357/81d10c18-c5ac-407b-af80-18268c3412da

If you believe this issue wasn't resolved, show your reproducer as evidence, don't copy my log