Skip to content
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

Missing JS bundles when using android flavors in release builds #32338

Closed
MichaelBorde opened this issue Oct 5, 2021 · 8 comments
Closed

Missing JS bundles when using android flavors in release builds #32338

MichaelBorde opened this issue Oct 5, 2021 · 8 comments

Comments

@MichaelBorde
Copy link

MichaelBorde commented Oct 5, 2021

Description

Some JS bundles (index.android.bundle) are missing in apks when using android flavors.

There has been this attempt to fix the bug: #32026

I can confirm the bug is still present in React Native 0.66.0.

React Native version:

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 3.84 GB / 15.26 GB
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 12.22.6 - ~/.nvm/versions/node/v12.22.6/bin/node
    Yarn: Not Found
    npm: 6.14.15 - ~/.nvm/versions/node/v12.22.6/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 16, 23, 26, 27, 28, 29, 30
      Build Tools: 23.0.1, 25.0.0, 25.0.2, 26.0.1, 27.0.3, 28.0.2, 28.0.3, 29.0.2, 29.0.3, 30.0.2, 30.0.3
      System Images: android-26 | Google Play Intel x86 Atom, android-29 | Intel x86 Atom_64
      Android NDK: 20.1.5948944
  IDEs:
    Android Studio: Not Found
  Languages:
    Java: 1.8.0_252 - /home/michael/.sdkman/candidates/java/current/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.66.0 => 0.66.0 
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

I have made this repository: https://github.com/MichaelBorde/RN066FlavorBundleIssue.

It is based on standard template (npx react-native init) with minimal modifications in android/app/build.gradle to define flavors.

Just create some release apks:

cd android
./gradlew clean assembleRelease

Inspect both crated apks you will find one with JS bundle and one missing it.

find . -name "*.apk" -exec unzip -l {} \; | grep assets

(Only 1 line will be present)

Expected Results

I expect to get a JS bundle in each created apk.

A potential fix

The following code could fix the issue: https://github.com/MichaelBorde/RN066FlavorBundleIssue/blob/main/android/app/build.gradle#L206.

Such a fix might be fragile because it is based on task name (a string) and such a name or task ordering might be changed in com.android.tools.build:gradle.

Similar issues

This issue is similar: #31157.

But this current issue does not concern proguard at all and targets React Native 0.66.0 which should include a fix (non working in my case).

@react-native-bot react-native-bot added Platform: Android Android applications. Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used. Platform: Linux Building on Linux. labels Oct 5, 2021
@cortinico cortinico added Tool: Gradle and removed Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used. labels Oct 14, 2021
@cortinico
Copy link
Contributor

Steps To Reproduce

Thanks for the reproducer. I was able to reproduce it locally, yet I wasn't able to find the root cause of the problems.

I believe this issue is happening only when you're building a multi-flavor version of an app. And is happening only when you're building more than one flavor due to a race condition on Gradle tasks.

Potentially moving to AGP 7.x and moving that logic to use the Artifact API might solve this issue.
If anyone else has the time to jump on this, feel free to ping me 👍

@MichaelBorde
Copy link
Author

I believe this issue is happening only when you're building a multi-flavor version of an app. And is happening only when you're building more than one flavor due to a race condition on Gradle tasks.

I think these two assumptions are correct from my experience.

@joeyfigaro
Copy link

Also affected by this issue. Am using dev, beta, and prod flavors in a current application, and all *Debug builds (or anything with bundling disabled) fails. Metro is running as expected during my testing/attempts.

@hotaryuzaki
Copy link

maybe you can try this solution here

@lfmatosm
Copy link

Still happens on react-native==0.67.4, with com.android.tools.build:gradle==4.2.2 and gradle==7.2. Proguard is disabled on my app. Hermes is currently enabled, but the same problem happens if you disable it. Also, the problem was simuated on Linux environment. On macOs, this didn't happen.

In my case, the error happens when building a single flavor from an app with multiple flavors configured. We have multiple types on our build stage (dev, hml, etc.). Our android/app/build.gradle looks like this:

/*some stuff*/

android {
    /*more stuff*/

    buildTypes {
        release {
            /*release definition*/
        }

        dev {
            /*dev definition*/
        }

        hml {
            /*hml definition*/
        }
    }

    flavorDimensions 'version'

    productFlavors {
        firstFlavor {
            /*flavor definition*/
        }

        secondFlavor {
            /*flavor definition*/
        }

       /*other flavors*/
    }
}

So, for any flavor, there's actually 3 different APKs. So our assemble command looks like this:

./gradlew assembleFirstFlavorDev assembleFirstFlavorHml assembleFirstFlavorRelease

The above command generates 3 APKs, where some of them randomly doesn't includes the index.android.bundle. I didn't track down which tasks were being executed outside the required order, but that's probably the reason for the problem, as mentioned by @cortinico.

If you build one type at a time, everything works smoothly:

./gradlew assembleFirstFlavorDev
./gradlew assembleFirstFlavorHml
./gradlew assembleFirstFlavorRelease

Is worth to notice that this problem started happening only after upgrading from react-native version 0.63.2 to 0.67.4. On the previous version, the Android Gradle plugin was 3.5.3 and gradle was 6.2.

@9christian9
Copy link

9christian9 commented Feb 9, 2023

Ciao,
I'm writing for all the people who, like me, have experienced this problem.

Affected Versions

  • React Native: 0.67.x
  • Gradle 7.2.x - 7.3.x

The common error

Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.

In short, index.android.bundle is missing.
Common problem when trying to use productFlavors.

The problem
I found that the problem is in a native react gradle task, to be precise in the copy task called currentAssetsCopyTask located in react.gradlefile.
As I understand, this "task" is responsible for copying the assets and in particular index.android.bundle and pasting them into the temporary directories used to generate the APK.

The solution
I created a repo that with a js script solves the problem before building the android app
https://github.com/9christian9/FIX-Android-productFlavors-RN0.67-gradle7.2-7.3

I hope this will help.
With ♥ by Christian

@lucasantarella
Copy link

For those still looking, looks like this report is similar to the issue #31157

@cortinico
Copy link
Contributor

Closing as flavors support has been completely rewritten in React Native 0.71
Please migrate to that version and try again. If the issue persists open a new bug report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants