这是indexloc提供的服务,不要输入任何密码
Skip to content

Migrate from tensorflow-lite to litert #11204

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samer-stripe
Copy link
Collaborator

@samer-stripe samer-stripe commented Jul 24, 2025

Summary

Migrate from tensorflow-lite to litert

Motivation

litert is where most TFLite development for Android has moved. Same APIs and has 16kb support.

Note: This doesn't fix 16kb page size support for other architectures for stripecardscan. We either need to rebuild the smaller AARs with litert or migrate completely away from them to the full library.

Testing

  • Added tests
  • Modified tests
  • Manually verified

Copy link
Contributor

github-actions bot commented Jul 24, 2025

Diffuse output:

OLD: paymentsheet-example-release-master.apk (signature: V1, V2)
NEW: paymentsheet-example-release-pr.apk (signature: V1, V2)

          │         compressed          │         uncompressed         
          ├──────────┬──────────┬───────┼───────────┬───────────┬──────
 APK      │ old      │ new      │ diff  │ old       │ new       │ diff 
──────────┼──────────┼──────────┼───────┼───────────┼───────────┼──────
      dex │  4.7 MiB │  4.7 MiB │   0 B │  10.4 MiB │  10.4 MiB │  0 B 
     arsc │  2.5 MiB │  2.5 MiB │   0 B │   2.5 MiB │   2.5 MiB │  0 B 
 manifest │  5.8 KiB │  5.8 KiB │   0 B │  30.1 KiB │  30.1 KiB │  0 B 
      res │  918 KiB │  918 KiB │   0 B │   1.5 MiB │   1.5 MiB │  0 B 
   native │  3.5 MiB │  3.5 MiB │   0 B │   8.5 MiB │   8.5 MiB │  0 B 
    asset │  1.6 MiB │  1.6 MiB │   0 B │   1.6 MiB │   1.6 MiB │  0 B 
    other │  198 KiB │  198 KiB │ -12 B │ 373.9 KiB │ 373.9 KiB │  0 B 
──────────┼──────────┼──────────┼───────┼───────────┼───────────┼──────
    total │ 13.5 MiB │ 13.5 MiB │ -12 B │  24.9 MiB │  24.9 MiB │  0 B 

         │         raw          │          unique           
         ├───────┬───────┬──────┼───────┬───────┬───────────
 DEX     │ old   │ new   │ diff │ old   │ new   │ diff      
─────────┼───────┼───────┼──────┼───────┼───────┼───────────
   files │     2 │     2 │    0 │       │       │           
 strings │ 53170 │ 53170 │    0 │ 49313 │ 49313 │ 0 (+0 -0) 
   types │ 19099 │ 19099 │    0 │ 17178 │ 17178 │ 0 (+0 -0) 
 classes │ 14516 │ 14516 │    0 │ 14516 │ 14516 │ 0 (+0 -0) 
 methods │ 73933 │ 73933 │    0 │ 70996 │ 70996 │ 0 (+0 -0) 
  fields │ 48170 │ 48170 │    0 │ 47046 │ 47046 │ 0 (+0 -0) 

 ARSC    │ old  │ new  │ diff 
─────────┼──────┼──────┼──────
 configs │  242 │  242 │  0   
 entries │ 6329 │ 6329 │  0
APK
    compressed     │   uncompressed   │                        
───────────┬───────┼───────────┬──────┤                        
 size      │ diff  │ size      │ diff │ path                   
───────────┼───────┼───────────┼──────┼────────────────────────
  54.8 KiB │ -12 B │ 121.7 KiB │  0 B │ ∆ META-INF/CERT.SF     
   1.2 KiB │  -1 B │   1.2 KiB │  0 B │ ∆ META-INF/CERT.RSA    
  51.4 KiB │  +1 B │ 121.7 KiB │  0 B │ ∆ META-INF/MANIFEST.MF 
───────────┼───────┼───────────┼──────┼────────────────────────
 107.4 KiB │ -12 B │ 244.6 KiB │  0 B │ (total)

@samer-stripe samer-stripe marked this pull request as ready for review July 24, 2025 17:11
@samer-stripe samer-stripe requested review from a team as code owners July 24, 2025 17:11
Copy link

emerge-tools bot commented Jul 24, 2025

2 builds increased size, 1 build decreased size

Name Version Download Change Install Change Approval
⚠️ Stripe Identity Example
com.stripe.android.identity.example.theme1
21.21.0-theme1 (20) 4.2 MB ⬆️ 325.4 kB (8.29%) 9.9 MB ⬆️ 650.6 kB (7.06%) N/A
Financial Connections Example
com.stripe.android.financialconnections.example
21.21.0 (212100) 4.5 MB ⬆️ 2 B 10.0 MB - N/A
PaymentSheet Example
com.stripe.android.paymentsheet.example
21.21.0 (11) 8.3 MB ⬇️ 1 B 17.3 MB - N/A

Stripe Identity Example 21.21.0-theme1 (20)
com.stripe.android.identity.example.theme1

⚖️ Compare build
⏱️ Analyze build performance

Total install size change: ⬆️ 650.6 kB (7.06%)
Total download size change: ⬆️ 325.4 kB (8.29%)

Largest size changes

Item Install Size Change Download Size Change
libtensorflowlite_jni.so ⬆️ 651.6 kB ⬆️ 327.0 kB
org.tensorflow.lite.support.tensorbuffer.TensorBufferFloat ⬆️ 3.1 kB ⬆️ 1.5 kB
📝 androidx.compose.runtime.ComposerImpl$$ExternalSyntheticOutline0 ⬆️ 2.4 kB ⬆️ 1.1 kB
🗑 androidx.compose.runtime.ComposerImpl$$ExternalSyntheticOutline1 ⬇️ -2.2 kB ⬇️ -1.1 kB
🗑 org.tensorflow.lite.support.tensorbuffer.TensorBuffer ⬇️ -2.1 kB ⬇️ -1.0 kB
View Treemap

Image of diff

Financial Connections Example 21.21.0 (212100)
com.stripe.android.financialconnections.example

⚖️ Compare build
⏱️ Analyze build performance

Total install size change: 0 B
Total download size change: ⬆️ 2 B

Largest size changes

Item Install Size Change Download Size Change
Other ⬆️ 0 B ⬆️ 2 B

PaymentSheet Example 21.21.0 (11)
com.stripe.android.paymentsheet.example

⚖️ Compare build
⏱️ Analyze build performance

Total install size change: 0 B
Total download size change: ⬇️ 1 B

Largest size changes

Item Install Size Change Download Size Change
Other ⬇️ 0 B ⬇️ -1 B

🛸 Powered by Emerge Tools

Comment trigger: Size diff threshold of 100.00kB exceeded

@samer-stripe
Copy link
Collaborator Author

samer-stripe commented Jul 24, 2025

Pretty big size increase for the Identity example app with the tflite.jni update. This can be optimized by merchants if they pull in the support via Google Play.

## Use TFLite in Google play to reduce binary size

CardScan Android SDK uses a portable TFLite runtime to execute machine learning models, if your application is released through Google play, you could instead use the Google play runtime, this would reduce the SDK size by ~400kb.

To do so, configure your app's dependency on stripecardscan as follows.
------
dependencies {
...
    implementation('com.stripe:stripecardscan:$stripeVersion') {
      exclude group: 'com.stripe', module: 'ml-core-cardscan' // exclude the cardscan-specific portable tflite runtime
    }
    implementation('com.stripe:ml-core-googleplay:$stripeVersion') // include the google play tflite runtime
...
}
------

No need to upgrade our play services libraries for TFLite since we are already on 16.4.0 (see here):

LiteRT in the Play Services runtime continues to use the play-services-tflite dependency. If your app is already using the Play Services runtime with TensorFlow Lite, you don't need to make any code changes.

To use LiteRT in Play Services, add the following to your build.gradle dependencies:

------
dependencies {
...
    // LiteRT dependencies for Google Play services
    implementation 'com.google.android.gms:play-services-tflite-java:16.4.0'
    // Optional: include LiteRT Support Library
    implementation 'com.google.android.gms:play-services-tflite-support:16.4.0'
...
}
-------

@samer-stripe samer-stripe force-pushed the samer/migrate-to-litert branch from 6e265ce to 85de622 Compare July 25, 2025 18:04
@samer-stripe samer-stripe requested review from a team as code owners July 25, 2025 18:04
@samer-stripe samer-stripe force-pushed the samer/migrate-to-litert branch from 85de622 to b96d0c0 Compare July 25, 2025 18:14
@samer-stripe samer-stripe force-pushed the samer/migrate-to-litert branch from b96d0c0 to fd9cb04 Compare July 25, 2025 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants