stripe / stripe-react-native

React Native library for Stripe.
https://stripe.dev/stripe-react-native
MIT License
1.27k stars 263 forks source link

Android bundle size #527

Closed acatalina closed 3 years ago

acatalina commented 3 years ago

Describe the bug Not really a bug but the bundle size of our android app has almost doubled since 0.2.0. Wondering if anyone else found this issue?

Guessing is stripe android SDK dependant? Not sure if the issue will be mitigated on 17.1.1? Tried modifying but it errored on build

Additional context React Native 0.64.2 buildToolsVersion 30.0.2 compileSdkVersion 30 targetSdkVersion 30 Gradle 4.1.0

thorsten-stripe commented 3 years ago

I wonder if it's because of https://github.com/stripe/stripe-react-native/pull/441/commits/7b5ef7be948f0985ef4475ef69bf9ae1cccfa4ed

We'll need to investigate.

souhe commented 3 years ago

@acatalina can you tell me the exact size difference you noticed?

In version 0.2.0 we also introduced google pay with a new resources to the GP button

acatalina commented 3 years ago

Yes, sorry @souhe, I should have given clear sizes. So rounding numbers, the app is about 13Mb with 0.1.5 and went to 21Mb with 0.2.0.

Just created a sample project with create-react-native-app to see the differences. Not implemented any code at all.

arekkubaczkowski commented 3 years ago

@acatalina I've checked that and turns out that this increase comes from stripe-android library and this significant difference was introduced in v16.10.0 I believe that were mandatory changes and cannot be reverted.

@michelleb-stripe fyi

michelleb-stripe commented 3 years ago

@acatalina In the latest version of the stripe-android native library we are using Jetpack Compose. With this change the apk should turn on minify in order to remove unused code. Unfortunately there is no way for us to do this from our library.

If you do not already have proguard enabled in your app, here are a few steps to enable it for the first time in your app. Effectively we will enable minifyEnabled and configure it to keep everything except androidx, and not to obfuscate anything (not even androidx).

android {
...
    buildTypes {
        release {
            // This will enable the use of proguard-rules.pro and remove unused code
            minifyEnabled true  
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            ...
        }
    }
}

and in the proguard-rules.pro file:

\# Depending on the libraries you have included in your app you might need to include other classes to keep.   After building the apk take a look at the usage.txt file generated.  If you see other package names besides androidx, add them into the proguard-rules.pro file. 

\# This is helpful to identify which packages are being shrunk
-printusage usage.txt

\# This disables obfuscation, it is not needed to shrink code 
-dontobfuscate

# Packages listed here with `-keep` will not have minification applied to their classes, member, or subclasses.  Notice androidx is purposefully not included in the list so used code from androidx is removed/shrunk/minified. Packages listed here and not included in your apk will have no effect and can be removed if desired.
-keep class android.** { *; }
-keep class com.** { *; }
-keep class java.** { *; }
-keep class javax.** { *; }
-keep class org.** { *; }
-keep class kotlin.** { *; }
-keep class kotlinx.** { *; }
-keep class okio.** { *; }
-keep class okhttp3.** { *; }
-keep class dagger.** { *; }
-keep class sun.** { *; }
-keep class dalvik.** { *; }
-keep class bolts.** { *; }

In doing this we have actually decreased the size of the resulting apk back to sizes from the 0.1.5 release, and in some cases it is even smaller.

ciccilleju commented 2 years ago

ok, with this flag i can Save up to 2,5 mb. For the future (since not everybody uses all the Stripe Payment Features) do you think is possible to "select" with a flag or an option, what to load?

For example, if I use the payment Sheet (which uses the Jetpack compose), I load that library, if i use the wallet payment only (google pay, apple pay) i don't load it, maybe it would be useful to decrease the apk size

michelleb-stripe commented 2 years ago

This is something that we do have on our backlog.

ciccilleju commented 2 years ago

This is something that we do have on our backlog.

great! :)

JeroenJochems commented 2 years ago

This is something that we do have on our backlog.

Any updates on this?