Skip to content

Reduce native files size #196

Description

@dougg0k

Hi,

I was testing out ferric-cli, just with the ferric-example it seems to generate a big .so file just for android. In this case it were 6.1mb.

I suppose this can negatively affect the final build size of RN apps.

Is there a way or if possible, could you add rust flags to the CLI, or whatever that it can be done to help reduce size of the native generated file(s), .so or otherwise?

https://github.com/johnthagen/min-sized-rust

Thanks.

Activity

  1. changed the title [-]Reduce native .so files size[/-] [+]Reduce native files size[/+] on Jul 23, 2025
  2. kraenhansen commented on Jul 24, 2025

    @kraenhansen
    Collaborator

    I'd be happy to add anything to reduce the file size - got any concrete ideas in mind?

    Notice that the current default --configuration is "Debug" and you'll likely get a smaller binary if you pass --configuration Release to ferric.

    I know that there are also possibilities of shaving off size with link-time optimization (LTO) when building the final app.

  3. dougg0k commented on Jul 24, 2025

    @dougg0k
    ContributorAuthor

    I suggested the repo which already should contain all the possible ways. Whichever you choose and works.

    Maybe leave Release as default?

  4. dougg0k commented on Jul 24, 2025

    @dougg0k
    ContributorAuthor
    > ferric build --configuration release
    
    ╭─────────────────────────╮
    │░█▀▀░█▀▀░█▀▄░█▀▄░▀█▀░█▀▀░│
    │░█▀▀░█▀▀░█▀▄░█▀▄░░█░░█░░░│
    │░▀░░░▀▀▀░▀░▀░▀░▀░▀▀▀░▀▀▀░│
    ╰─────────────────────────╯
    ℹ Using default targets, pass --android, --apple or individual --target options, to avoid this.
    Using cargo 1.88.0 (873a06493 2025-05-10)
    ✖ Failed to build: Expected /react-native-node-api/packages/ferric-example/target/x86_64-linux-android/release to exist
    

    Could be that the bug is that you are passing release and not --release to cargo build, so it's only generating the debug folder.

    ❯ ll target/x86_64-linux-android/                                                                                                                                                                                                                                                                                                                                                   
    Octal Permissions Links Size User    Group   Date Modified Git Name
    0755  drwxr-xr-x      1    - user user 24 jul 12:40   -I  debug/
    0644  .rw-r--r--      1  177 user user 24 jul 12:40   -I  CACHEDIR.TAG
    

    It seems encapsulated, perhaps if you had a flag to show all the actual commands being run in the background, through logging.

    Also add a .toLowerCase() because, Release do not work.

  5. shirakaba commented on Jul 24, 2025

    @shirakaba
    Collaborator

    Various common tricks for reducing binary size include these Cargo build settings:

    [profile.release]
    lto = true
    strip = "symbols"
    opt-level = "z"
    codegen-units = 1
    panic = "abort"

    With those settings, I was able to get a Hello World Node-API module down to 304 KB for iOS or macOS (can't remember). Can't be sure what size that might mean for Android, however.

  6. dougg0k commented on Jul 24, 2025

    @dougg0k
    ContributorAuthor

    I checked, for android, it was reduced to ~330kb.


    In packages/ferric-example

    • Run npm run build
    • Run cargo build --release
    • Run mv target/release target/x86_64-linux-android/
    • Run npm run build if the script has --configuration release
  7. dougg0k commented on Jul 24, 2025

    @dougg0k
    ContributorAuthor

    Maybe ferric-example should not exist. But be generated by the ferric-cli, that way Cargo.toml and other files could be better managed.


    Then suggest

    npm install --save-dev ferric-cli
    
    "scripts": {
       "generate:project": "ferric generate folder_name",
       "build": "ferric build folder_name",
       "build:release": "npm run build --configuration release",
    },
    

    #195

  8. dougg0k commented on Jul 24, 2025

    @dougg0k
    ContributorAuthor

    If I am able to finish this PR, there should be plenty of improvements added.

    #200 - List inside.

  9. dougg0k commented on Jul 26, 2025

    @dougg0k
    ContributorAuthor

    In the init/generate command, do you prefer that it provide a package.json to be a standalone lib or can it be as the idea I had, you suggest the scripts as in the previous comment, then that package import the lib from the dist/ in the actual project?

    #206

  10. shirakaba commented on Jul 26, 2025

    @shirakaba
    Collaborator

    In the init/generate command, do you prefer that it provide a package.json to be a standalone lib or can it be as the idea I had, you suggest the scripts as in the previous comment, then that package import the lib from the dist/ in the actual project?

    Sorry, not quite clear what you mean. Could you illustrate the two alternatives?

  11. dougg0k commented on Jul 26, 2025

    @dougg0k
    ContributorAuthor

    1- Not a standalone package, without a package.json. But, managed by the package.json root of the app. Where the root package as suggested #196 (comment) managed as if it were an additional like android or ios.

    project_name
    │   Cargo.toml
    │   build.rs
    │   .gitignore
    └───src
    │   │   lib.rs
    └───dist (after build)
    │   │   index.js
    │   │   index.d.ts
    │   │   native_android_folder
    │   │   native_ios_folder

    2- With a package.json to be a standalone lib.

  12. dougg0k commented on Jul 27, 2025

    @dougg0k
    ContributorAuthor

    I took the with second approach. Let me know if you preferred the first.

    I think I am done with the PRs, feel free to review them.

    I will probably wait for them to maybe get merged and released before using the tool.


    I will close this issue as done. Added through one of the PRs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions