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

Fix App Bundle/Release build missing index.android.bundle with gradle plugin 4.1.0+/gradle 6.5 #30177

Closed

Conversation

tomoima525
Copy link
Contributor

@tomoima525 tomoima525 commented Oct 14, 2020

Summary

  • This fix resolves Flavors & App Bundle missing index.android.bundle #29398
  • After updating gradle to 6.5+ and android gradle plugin to 4.1.0+(which is recommended in the latest Android Studio 4.1), Running :app:assembleRelease or :app:bundleRelease will not contain index.android.bundle in Apk/AAB. It will be included when the command executed twice.

Screen Shot 2020-10-17 at 11 32 43 PM

  • This is caused by the task ordering update introduced in gradle plugin 4.1.0+/gradle 6.5. mergeResources task runs before currentAssetsCopyTask(copying the bundle asset file to intermediate output directory) which causes generated Apk/AAB not including the bundle file.
  • The fix ensures mergeResources task runs after currentAssetsCopyTask

Changelog

[Android] [Fixed] - Fix App Bundle/Release build missing index.android.bundle with gradle plugin 4.1.0/gradle 6.5

Test Plan

  • Reproducible repository https://github.com/tomoima525/android_build_test_rn

    • This project is generated with create-react-native-app and updated Gradle version to 6.5 and com.android.tools.build:gradle plugin to 4.1
  • Run ./gradlew clean :app:assembleRelease and ./gradlew clean :app:bundleRelease => reproduces the issue

  • After adding the fix above and run ./gradlew clean :app:assembleRelease and ./gradlew clean :app:bundleRelease => The issue is resolved

  • Also confirmed the build works properly with android gradle plugin 3.5.3 and gradle 6.2(the default value of create-react-native-app)

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 14, 2020
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Oct 14, 2020
@analysis-bot
Copy link

analysis-bot commented Oct 14, 2020

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: fb14fd4

@analysis-bot
Copy link

analysis-bot commented Oct 14, 2020

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,389,897 0
android hermes armeabi-v7a 7,017,213 0
android hermes x86 7,832,109 0
android hermes x86_64 7,721,595 0
android jsc arm64-v8a 9,536,198 0
android jsc armeabi-v7a 9,151,226 0
android jsc x86 9,400,874 0
android jsc x86_64 9,982,584 0

Base commit: fb14fd4

@emeraldsanto
Copy link

I've also been experiencing this since updating this morning, reverting to 4.0.1 and 6.3 works until this PR is merged :)

@tomoima525 tomoima525 changed the title Ensure mergeResources task runs after currentAssetsCopyTask on App Bundle/Release build Fix App Bundle/Release build missing index.android.bundle with gradle plugin 4.1.0+/gradle 6.5 Oct 14, 2020
Ensure mergeResources task runs after currentAssetsCopyTask
@tomoima525 tomoima525 force-pushed the android-gradle-plugin-4_1-build branch from c752304 to a6c7049 Compare October 14, 2020 20:54
@tomoima525
Copy link
Contributor Author

Just rebased so that the commit message is more descriptive

@tomoima525
Copy link
Contributor Author

I have the reproducible case and fixed case with this patch

https://github.com/tomoima525/android_build_test_rn

Reproducible case

To reproduce the issue, run the command below

$ cd android
$ ./gradlew clean :app:assembleRelease // or ./gradlew clean :app:releaseBundle

app-release.apk will be generated but it does not have index.android.bundle build

Fixed case

I added the fix in #30177 . Running the command below will apply the patch to react.gradle and you can see the issue is resolved

$ yarn add_patch
$ cd android
$ ./gradlew clean :app:assembleRelease // or ./gradlew clean :app:releaseBundle

(The script is tested on Mac. If the copying does not work on Linux, try directly copying the react-patch.gradle to node_modules/react-native/react.gradle)

@mikehardy
Copy link
Contributor

mikehardy commented Oct 21, 2020

Confirmed for me this fixes the error it says it fixes for RN0.63.3 and android gradle plugin 4.1.0

Here's a patch-package compatible thing you can cut-and-paste

cat patches/react-native+0.63.3.patch

diff --git a/node_modules/react-native/react.gradle b/node_modules/react-native/react.gradle
index 6441d93..c28cbd4 100644
--- a/node_modules/react-native/react.gradle
+++ b/node_modules/react-native/react.gradle
@@ -302,6 +302,11 @@ afterEvaluate {
             enabled(currentBundleTask.enabled)
         }
 
+        // mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+.
+        // This ensures to copy the bundle file before mergeResources task starts
+        def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
+        mergeResourcesTask.dependsOn(currentAssetsCopyTask)
+
         packageTask.dependsOn(currentAssetsCopyTask)
         if (buildPreBundleTask != null) {
             buildPreBundleTask.dependsOn(currentAssetsCopyTask)

Copy link
Contributor

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! 👍

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @tomoima525 in 53f5500.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Oct 26, 2020
kelset pushed a commit that referenced this pull request Nov 27, 2020
… plugin 4.1.0+/gradle 6.5 (#30177)

Summary:
- This fix resolves #29398
- After updating gradle to 6.5+ and android gradle plugin to 4.1.0+(which is recommended in the latest Android Studio 4.1), Running `:app:assembleRelease` or `:app:bundleRelease` will not contain `index.android.bundle` in Apk/AAB. It will be included when the command executed twice.
<img width="949" alt="Screen Shot 2020-10-17 at 11 32 43 PM" src="https://user-images.githubusercontent.com/6277118/96360808-38165c00-10d5-11eb-8b6e-f098517a24c7.png">

- This is caused by the task ordering update introduced in gradle plugin 4.1.0+/gradle 6.5. `mergeResources` task runs before `currentAssetsCopyTask`(copying the bundle asset file to intermediate output directory) which causes generated Apk/AAB not including the bundle file.
- The fix ensures mergeResources task runs after currentAssetsCopyTask

## Changelog

[Android] [Fixed] - Fix App Bundle/Release build missing index.android.bundle with gradle plugin 4.1.0/gradle 6.5

Pull Request resolved: #30177

Test Plan:
- Reproducible repository https://github.com/tomoima525/android_build_test_rn
  -  This project is generated with `create-react-native-app` and updated Gradle version to 6.5 and com.android.tools.build:gradle plugin to 4.1
- Run `./gradlew clean :app:assembleRelease` and `./gradlew clean :app:bundleRelease` => reproduces the issue
- After adding the fix above and run `./gradlew clean :app:assembleRelease` and `./gradlew clean :app:bundleRelease` => The issue is resolved

- Also confirmed the build works properly with android gradle plugin `3.5.3` and `gradle 6.2`(the default value of `create-react-native-app`)

Reviewed By: fkgozali

Differential Revision: D24551605

Pulled By: cpojer

fbshipit-source-id: b0effe2c6ea682748af185061af951e2f2bce722
@John-Konordo
Copy link

John-Konordo commented Dec 12, 2020

Development machine OS: MacOS X
IDE: Ms VSCODE
"react": "^17.0.1",
"react-native": "^0.63.4",
gradle plugin 4.1.1+ / gradle 6.5-all (distributionUrl=https://services.gradle.org/distributions/gradle-6.5-all.zip)

I had also aply the patch from here #29398 (comment)

I found a workarround to publish my apk to play console and don' t get the error index.android.bundle missing

In root directory of your project run the following commands
cd ./android/
./gradlew clean
rm -rf ~/.gradle/caches/*
cd ..
npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
rm -rf ./android/app/src/main/res/drawable-*
rm -rf ./android/app/src/main/res/raw
cd ./android/
./gradlew assembleRelease

Install my apk to any device and works well and upload the apk to play console and the error dissapear too!

cristeahub pushed a commit to HyreAS/react-native that referenced this pull request Dec 21, 2020
… plugin 4.1.0+/gradle 6.5 (facebook#30177)

Summary:
- This fix resolves facebook#29398
- After updating gradle to 6.5+ and android gradle plugin to 4.1.0+(which is recommended in the latest Android Studio 4.1), Running `:app:assembleRelease` or `:app:bundleRelease` will not contain `index.android.bundle` in Apk/AAB. It will be included when the command executed twice.
<img width="949" alt="Screen Shot 2020-10-17 at 11 32 43 PM" src="https://user-images.githubusercontent.com/6277118/96360808-38165c00-10d5-11eb-8b6e-f098517a24c7.png">

- This is caused by the task ordering update introduced in gradle plugin 4.1.0+/gradle 6.5. `mergeResources` task runs before `currentAssetsCopyTask`(copying the bundle asset file to intermediate output directory) which causes generated Apk/AAB not including the bundle file.
- The fix ensures mergeResources task runs after currentAssetsCopyTask

## Changelog

[Android] [Fixed] - Fix App Bundle/Release build missing index.android.bundle with gradle plugin 4.1.0/gradle 6.5

Pull Request resolved: facebook#30177

Test Plan:
- Reproducible repository https://github.com/tomoima525/android_build_test_rn
  -  This project is generated with `create-react-native-app` and updated Gradle version to 6.5 and com.android.tools.build:gradle plugin to 4.1
- Run `./gradlew clean :app:assembleRelease` and `./gradlew clean :app:bundleRelease` => reproduces the issue
- After adding the fix above and run `./gradlew clean :app:assembleRelease` and `./gradlew clean :app:bundleRelease` => The issue is resolved

- Also confirmed the build works properly with android gradle plugin `3.5.3` and `gradle 6.2`(the default value of `create-react-native-app`)

Reviewed By: fkgozali

Differential Revision: D24551605

Pulled By: cpojer

fbshipit-source-id: b0effe2c6ea682748af185061af951e2f2bce722
@pixelknitter
Copy link

Confirmed for me this fixes the error it says it fixes for RN0.63.3 and android gradle plugin 4.1.0

Here's a patch-package compatible thing you can cut-and-paste

cat patches/react-native+0.63.3.patch

diff --git a/node_modules/react-native/react.gradle b/node_modules/react-native/react.gradle
index 6441d93..c28cbd4 100644
--- a/node_modules/react-native/react.gradle
+++ b/node_modules/react-native/react.gradle
@@ -302,6 +302,11 @@ afterEvaluate {
             enabled(currentBundleTask.enabled)
         }
 
+        // mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+.
+        // This ensures to copy the bundle file before mergeResources task starts
+        def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
+        mergeResourcesTask.dependsOn(currentAssetsCopyTask)
+
         packageTask.dependsOn(currentAssetsCopyTask)
         if (buildPreBundleTask != null) {
             buildPreBundleTask.dependsOn(currentAssetsCopyTask)

@mikehardy For those discovering this solution, I thought I'd figure out exact instructions to patch from 0.63.3 as my project is not ready to migrate to 0.64; this seems like a good option. I've not used patch-package before, running patch-package patches/react-native+0.63.3.patch would require me to make the changes to the node_modules myself first, correct?

@mikehardy
Copy link
Contributor

@pixelknitter - nope, yarn add patch-package && mkdir patches drop the patch file into there (named correctly! that's how patch-package figures out what package etc) then yarn patch-package should do it. Or all the equivalent-different-but-same npm commands.

If you are making a new patch (like how I generated this file) to fix some bug where you are the first person to find+fix, then you hack around in node_modules by hand yes, and run yarn patch-package <package name> and it will create a new patch for you with your new change in it for distribution like this patch here

@pixelknitter
Copy link

@pixelknitter - nope, yarn add patch-package && mkdir patches drop the patch file into there (named correctly! that's how patch-package figures out what package etc) then yarn patch-package should do it. Or all the equivalent-different-but-same npm commands.

If you are making a new patch (like how I generated this file) to fix some bug where you are the first person to find+fix, then you hack around in node_modules by hand yes, and run yarn patch-package <package name> and it will create a new patch for you with your new change in it for distribution like this patch here

Thanks for the detailed response!

@mouhsnimohamed
Copy link

if you'r using proguard add an exception to android/app/proguard-rules.pro:

-keep class com.mypackage.BuildConfig { *; }

com.mypackage should match the package value in your app/src/main/AndroidManifest.xml file.

@xczaixian
Copy link

xczaixian commented May 12, 2021

Confirmed for me this fixes the error it says it fixes for RN0.63.3 and android gradle plugin 4.1.0

Here's a patch-package compatible thing you can cut-and-paste

cat patches/react-native+0.63.3.patch

diff --git a/node_modules/react-native/react.gradle b/node_modules/react-native/react.gradle
index 6441d93..c28cbd4 100644
--- a/node_modules/react-native/react.gradle
+++ b/node_modules/react-native/react.gradle
@@ -302,6 +302,11 @@ afterEvaluate {
             enabled(currentBundleTask.enabled)
         }
 
+        // mergeResources task runs before the bundle file is copied to the intermediate asset directory from Android plugin 4.1+.
+        // This ensures to copy the bundle file before mergeResources task starts
+        def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
+        mergeResourcesTask.dependsOn(currentAssetsCopyTask)
+
         packageTask.dependsOn(currentAssetsCopyTask)
         if (buildPreBundleTask != null) {
             buildPreBundleTask.dependsOn(currentAssetsCopyTask)

thank you, but it's not work for me.
i supplement code below to make it work.

def compressAssetsTask = tasks.findByName("compress${targetName}Assets")
compressAssetsTask.dependsOn(currentAssetsCopyTask)

@mikehardy
Copy link
Contributor

@xczaixian this is no longer a problem with current versions (0.64.1) - I'm not carrying this patch any longer as it has been integrated and released. Better to update and get the real fix

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Platform: Android Android applications.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flavors & App Bundle missing index.android.bundle