Cli: [Android] Wrong packageName for multiple app modules

Created on 30 Jan 2020  路  8Comments  路  Source: react-native-community/cli

Environment

System:
OS: macOS 10.15.2
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 81.31 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 12.4.0 - /usr/local/bin/node
Yarn: 1.17.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5977832
Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.5 => 0.61.5
npmGlobalPackages:
create-react-native-module: 0.11.0
react-native-cli: 2.0.1

Description

On an Android Project which has multiple app modules, the generated PackageList (presumably for auto-linking) attempts to import the wrong BuildConfig and R resources for any module which is not first (alphabetically).
So, assuming you go to build the second app module, the CLI will attempt to import the module's BuildConfig and R resources under the package name of the first module.

Reproducible Demo

  • Create a new React Native project using the CLI
  • in the android folder, copy the app directory into another new directory ("bapp")
  • add the new module in the settings.gradle for the project
  • rename the package id to be different from the app module.
  • attempt to build the bapp module

Expected behavior: bapp should build.
Observed behavior: bapp fails to build as generated PackageList file attempts to import BuildConfig & R using package id from app module.

autolink bug android

All 8 comments

Yes, I know that I can use react-native.config.js to configure packageName. By doing this, I can set the value to the module I want to build and it will build that module. As I'm looking to build multiple, the hack that I have to use is to have my module's build.gradle file modify the config each time I look to build.
Alternatively, I'm sure I could have both use the same package id, and then just have the release variant specify an application id suffix that would seperate each app. Again, that feels like a hack.

Hey there! We try to partially tackle this with https://github.com/react-native-community/cli/pull/871, can you have a look?

@thymikee #871 will resolve this issue. Thanks!

Edit: Misreported, further testing shows issue not resolved

We're working on merging it soon. Thanks for opening issue and providing additional context!

Unfortunately, this PR is not fixing your issue.

Yes, I know that I can use react-native.config.js to configure packageName. By doing this, I can set the value to the module I want to build and it will build that module. As I'm looking to build multiple, the hack that I have to use is to have my module's build.gradle file modify the config each time I look to build.

Currently, it is a CLI limitation that we support a SINGLE module, not an array. We will need to change the core of the CLI to support it.

It's on our backlog.

To make this work, we would have to get rid of packageName while generating PackageList class (because it's just a single value, where we have an array of different packages).

Instead, change this:

List packages = new PackageList(this).getPackages();

to

List packages = new PackageList(this, <>).getPackages();

and then, encourage users to use some helpers, such as:

new MyModule(getBuildProperty("name"))

so that we can define that later, not while generating the PackageList file.

Another option is to detect multiple packages and generate PackageList for each of them, e.g. PackageListAppA, PackageListAppB, depending on what you want to use.

This is non-breaking.

This would require we convert packageName from string to string[] at a configuration level. We will have to do that anyway for run-android.

Unsure if this is related, but I've been struggling to get auto inking to work on Android since upgrading:

react-native: 0.61.5 => 0.62.2
@react-native-community/cli: 3.0.0 => 4.8.0

The issue lies with attempting to autolink a library that is contained in our repository as a submodule directory. This submodule also happens to contain a runnable React Native app (which is why I suspect the issue occurs).

Prior to the version upgrades listed above, the autolinking worked as expected. Now it fails and presents this error:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not resolve project :companycam_companycam-camera.
     Required by:
         project :app
      > Unable to find a matching configuration of project :companycam_companycam-camera:
          - None of the consumable configurations have attributes.

Ultimately the only solution I could get to work was to disable autolinking on Android for that dependency and revert to manual linking, which then works as expected.

This is an acceptable solution for me, but curious if there is a bigger issue or way to resolve this for others that may have the came experience.

Was this page helpful?
0 / 5 - 0 ratings