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

Flavors & App Bundle missing index.android.bundle with proguard enabled #31157

Closed
lorenzoangelini opened this issue Mar 14, 2021 · 20 comments
Closed
Labels
Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@lorenzoangelini
Copy link

Issue

We use the flavors into our apps to use different version. We have dev, test and prod. When we have upgraded to 0.63 (same with 0.64), to classpath("com.android.tools.build:gradle:3.5.3") and distributionUrl=https://services.gradle.org/distributions/gradle-6.2-all.zip and proguard enabled, we cannot use the appbundle.
when we run ./gradlew bundleDevRelease -> we cannot find into the app-dev-release.aab the index.android.bundle. This cause a crash when open the app.
If we run this command : ./gradlew bundleRelease -> it build all variants
app-dev-release.aab -> missing
app-test-release.aab -> index.android.bundle present
app-prod-release.aab -> index.android.bundle present

Description

Please provide a clear and concise description of what the bug is. Include screenshots if needed.
Please test using the latest React Native release to make sure your issue has not already been fixed: https://reactnative.dev/docs/upgrading.html

React Native version:

System:
OS: macOS 10.15.7
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 944.16 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.15.4 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.14.10 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.10.1 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 14.0, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
Android SDK:
API Levels: 23, 27, 28, 29
Build Tools: 26.0.2, 27.0.3, 28.0.3, 29.0.2, 29.0.3
System Images: android-19 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom_64, android-27 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 4.1 AI-201.8743.12.41.6953283
Xcode: 12.0/12A7209 - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_201 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.1 => 17.0.1
react-native: 0.64.0 => 0.64.0
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

Steps To Reproduce

1.Create a new project: npx react-native init AppName --template react-native-template-typescript
2. Add to gradle.build :
productFlavors {
prod {
}
dev {
applicationId 'com.appname.dev'
resValue "string", "build_config_package", "com.appname"
}
test {
applicationId 'com.appname.test'
resValue "string", "build_config_package", "com.appname"
}
}
3. Enable Proguard
4. run into android folder : ./gradlew clean && ./gradlew bundleDevRelease

Expected Results

Obtain an .aab file into android/build/outputs/bundle/devRelease. when i open the .aab i expect to find the folder base and into the base the folder assets that contain the index.android.bundle.

@gedeagas
Copy link
Contributor

@lorenzoangelini This issue has now been resolved in 0.64.
#30177

@lorenzoangelini
Copy link
Author

@gedeagas i reproduce this issue when proguard is active

@gedeagas
Copy link
Contributor

@lorenzoangelini Interesting, do you mind providing a git repo of the problem?

@renopp This is probably related to your case ( multiflavors, prod aab dosen't have index.android.bundle, etc ). Let's try to fix this if there is a problem here.

@lorenzoangelini
Copy link
Author

@gedeagas with a new project i reproduce this issue when i using flavors.

@davwheat
Copy link

davwheat commented Mar 26, 2021

Can reproduce on RN 0.64.0. Worked fine before the upgrade on 0.63.x. 🤔

I'd share the git repo, but this app is private for a client.

I don't use productFlavors, only buildTypes.

Debug and prod APKs work fine. Haven't tested a debug AAB.

// build.gradle
buildTypes {
    release {
        signingConfig signingConfigs.release
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        minifyEnabled true
        shrinkResources true
    }
    releaseUnsigned {
        initWith release
        signingConfig null
        // needed to prevent react native dependency code failing
        // to build
        matchingFallbacks =  ['release']
    }
}

// ------------------------------------------------

// proguard-rules.pro
-keep public class com.horcrux.svg.** {*;}

image

react-native info output
info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.19042
    CPU: (6) x64 Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
    Memory: 3.70 GB / 15.94 GB
  Binaries:
    Node: 15.1.0 - ~\AppData\Local\Temp\yarn--1616780838243-0.2835069363144411\node.CMD
    Yarn: 1.22.5 - ~\AppData\Local\Temp\yarn--1616780838243-0.2835069363144411\yarn.CMD
    npm: 7.6.3 - c:\users\david\appdata\roaming\npm\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK:
      API Levels: 23, 24, 25, 26, 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 30.0.3
      System Images: android-26 | Android Wear Intel x86 Atom, android-28 | China version of Wear OS Intel x86 Atom, android-28 | Wear OS Intel x86 Atom, android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
    Windows SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Visual Studio: 15.9.28307.1321 (Visual Studio Community 2017)
  Languages:
    Java: 11.0.7 - c:\program files\adoptopenjdk\jdk-11.0.7.10-hotspot\bin\javac.EXE
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1
    react-native: ^0.64.0 => 0.64.0
    react-native-windows: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

@gedeagas
Copy link
Contributor

gedeagas commented Mar 26, 2021

@davwheat so it's working on RN 0.63 but not 0.64? hmm.. that is strange.
I will be looking at this on Monday. Thanks for the report.

@davwheat
Copy link

It was working before I upgraded on 20th March -- I suppose it could have come in from another change I've made, but I doubt any JS changes would cause it, and I haven't tinkered with my build process at all (except the upgrade process).

If you want me to test anything or give you more info, let me know :)

@lorenzoangelini
Copy link
Author

@davwheat me too, i don't have any problem with 0.62, the app bundle was build correctly.

@gedeagas
Copy link
Contributor

@davwheat do you also have proguard turned on like @lorenzoangelini ?

@davwheat
Copy link

@gedeagas Yes, I use proguard, including a custom rule which I posted above.

@danaelisanicolas
Copy link

Any updates on this? We're also having issues on bundling android releases with proguard enabled

renopp added a commit to renopp/react-native that referenced this issue Mar 31, 2021
This commit fixes Flavors & App Bundle missing index.android.bundle with proguard enabled facebook#31157 (comment)
@renopp
Copy link

renopp commented Mar 31, 2021

@lorenzoangelini @davwheat @danaelisanicolas
I just created a PR to fix the issue, kindly check

@mikehardy
Copy link
Contributor

Apologies for the tangent, but the I was scanning the react-native issues list for a different reason and saw this - I patterned a change to the react-native-elements font-copy task after the changes done here in the react-native repo to handle gradle 4.1+, and it's the same code area - if anyone using react-native-elements notices that for app bundles that use flavors and variants you aren't seeing your fonts copied, it may be that the same sort of change needs to be carried into the react-native-elements font-copy gradle task, c.f. oblador/react-native-vector-icons#1307

@danaelisanicolas
Copy link

danaelisanicolas commented Mar 31, 2021

Hi, Actually mine got fixed when I change from:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://reactnative.dev/docs/signed-apk-android.
        minifyEnabled true
        shrinkResources true
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

to just:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://reactnative.dev/docs/signed-apk-android.
        minifyEnabled enableProguardInReleaseBuilds
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

and for context my proguard file looks like this:

-keep class com.facebook.hermes.unicode.** { *; }
-keep class com.facebook.jni.** { *; }
-keep public class com.horcrux.svg.** {*;}

Please correct me if i'm wrong, but from what I understand, minifyEnabled WITHOUT the proguard reference doesn't keep the files (during minifying and shrinking) that hermes, jni, and svg needs which then gets null reference pointer exception when bundled and ran.

@davwheat
Copy link

I think the bigger issue is that we shouldn't suddenly have builds break for no apparent reason, especially when it's not super easy to test aabs, and when it's not listed in the changelog.

The PR is a simple one line fix (I haven't tested it personally, but I trust that it works) so merging that should be a priority over other workarounds, otherwise people will put off upgrading RN as it breaks their app.

@hotaryuzaki
Copy link

Hi, Actually mine got fixed when I change from:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://reactnative.dev/docs/signed-apk-android.
        minifyEnabled true
        shrinkResources true
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

to just:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        // Caution! In production, you need to generate your own keystore file.
        // see https://reactnative.dev/docs/signed-apk-android.
        minifyEnabled enableProguardInReleaseBuilds
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
}

and for context my proguard file looks like this:

-keep class com.facebook.hermes.unicode.** { *; }
-keep class com.facebook.jni.** { *; }
-keep public class com.horcrux.svg.** {*;}

Please correct me if i'm wrong, but from what I understand, minifyEnabled WITHOUT the proguard reference doesn't keep the files (during minifying and shrinking) that hermes, jni, and svg needs which then gets null reference pointer exception when bundled and ran.

my problem was app would crash when open (AAB), but no issue for debug app & APK.
fixed with remove shrinkResources in build.gradle.
thanks @danaelisanicolas

my error log:

12-10 15:04:34.984 27112 27112 E AndroidRuntime: FATAL EXCEPTION: main
12-10 15:04:34.984 27112 27112 E AndroidRuntime: Process: com.brambang.apps, PID: 27112
12-10 15:04:34.984 27112 27112 E AndroidRuntime: java.lang.AssertionError: APK bundle must contain the expected embedded asset index.android.bundle
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.l.e.a(Unknown Source:147)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.l.e.g(Unknown Source:50)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.l.e.e(Unknown Source:10)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.l.g.x(Unknown Source:64)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.l.g.B(Unknown Source:96)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.f.D(Unknown Source:75)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.f.s(Unknown Source:5)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at expo.modules.updates.UpdatesPackage$a.b(Unknown Source:12)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at f.a.e.createReactInstanceManager(Unknown Source:33)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at com.facebook.react.s.a(Unknown Source:9)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at com.brambang.apps.MainApplication.onCreate(Unknown Source:11)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1193)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6991)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.app.ActivityThread.access$1500(ActivityThread.java:257)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1982)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:236)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:8056)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
12-10 15:04:34.984 27112 27112 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

@staltz
Copy link

staltz commented Feb 4, 2022

I was also missing index.android.bundle after updating RN 0.64.x => 0.66.x and what worked for me was calling

./gradlew assembleMyFlavorRelease

instead of

./gradlew assembleRelease

I was using the latter previously, which builds all flavors at once. Apparently if you assemble each flavor one at a time, it works. Here's my codebase in case anyone wants to explore more details: staltz/manyverse@c84843a

@9christian9
Copy link

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

Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Nov 30, 2023
Copy link

github-actions bot commented Dec 7, 2023

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as completed Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage 🔍 Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants