Skip to content

Generate dart file containing references to all assets and fonts available to app #18898

Closed as not planned

Description

@fmatosqg

Inspired on R.drawable.image_logo on native android platform.

Based on top of work done on #4890, it's independent of #18897 but highly related.

Activity

  1. changed the title [-]Generate dart file containing references to all assets available to app[/-] [+]Generate dart file containing references to all assets and fonts available to app[/+] on Jun 28, 2018
  2. fmatosqg commented on Jun 28, 2018

    @fmatosqg
    ContributorAuthor

    Copying my own comment from flutter/flutter-intellij#2378 (comment):

    There are some challenges though:
    a) since android restricts the allowed filenames it's much easier to generate field names accessible in java. Flutter accepts any filename which makes it quite tricky to handle for example filenames composed of characters such as emojis, @ or [.
    b) android gracefully handles collisions by overwriting assets when a conflict is found. In flutter we'll have collisions for files coming from different folders, different extensions, and more so for the forbidden characters as mentioned in the point above.

    a is tricky but can be tackled, but b needs a lot of thinking on how the developer will access different files with different filenames that would naively map to the same class+field on generated code.

  3. added
    c: new featureNothing broken; request for a new capability
    toolAffects the "flutter" command-line tool. See also t: labels.
    a: assetsPackaging, accessing, or using assets
    on Aug 27, 2018
  4. fmatosqg commented on Nov 19, 2018

    @fmatosqg
    ContributorAuthor

    I'm still interested in getting this done and I have something basic that will do for most projects. But I need feedback on how to gracefully degrade for all types of conflicts.

    Ideas anyone?

  5. jaumard commented on Jan 3, 2019

    @jaumard
    Contributor

    What do you mean by gracefully degrade ? Generally on Android when you have a conflict it just doesn't compile anymore and give you an error message, why not do the same ?
    I would like to see this append :) can we see your basic solution somewhere ?

  6. mare011 commented on Jan 3, 2019

    @mare011

    I suggest adding folder support like: R.drawable.images.nature and if multiple files with same path, then R.drawable.images.nature.jpg or like a suffix R.drawable.images.nature_jpg.
    With "images" being folder name

  7. zoechi commented on Jan 21, 2019

    @zoechi
    Contributor

    Similar to #11477

  8. szotp commented on Mar 9, 2019

    @szotp

    Or just add better support for build_runner so that people can write plugins to generate asset names, language strings, parsers, etc. and they will be built during flutter run.

  9. fmatosqg commented on Mar 12, 2019

    @fmatosqg
    ContributorAuthor

    @szotp I stopped following the state of things for build_runner, but what you suggest is aligned with what I'd like to have

    @jaumard
    consider the following examples:
    /assets/images/3.jpg
    /assets/images/this.png
    /assets/images/this.jpg
    /assets/images/this.JPG
    /assets/other/this.png

    And assuming pubspec has an entry for assets/images and another for assets/other, how would you disambiguate things?

  10. jonahwilliams commented on Mar 12, 2019

    @jonahwilliams
    Contributor

    FYI I'm integrating build_runner support into the flutter tool, so if you're interested I suggest you follow/thumbs up #27141

  11. jaumard commented on Mar 12, 2019

    @jaumard
    Contributor

    @fmatosqg for me those shouldn't compile and throw an "ambiguous" error for the user to rename those
    consider the following examples:

    /assets/images/3.jpg
    /assets/images/this.png
    /assets/images/this.jpg
    /assets/images/this.JPG
    

    but if they are under another folder like /assets/other/this.png for me should be ok to have something like Assets.other.this and Assets.images.this this is a wrong example as it's a reserved keywords but you get it ^^

  12. fmatosqg commented on Mar 14, 2019

    @fmatosqg
    ContributorAuthor

    @jonahwilliams that's very good news for a whole host of reasons :) I've wanted that since hearing how build_runner works.

  13. szotp commented on Aug 21, 2019

    @szotp

    Anyone started working on this already? Flutter really needs something like https://github.com/mac-cain13/R.swift.

  14. szotp commented on Aug 25, 2019

    @szotp

    Update: I found a package named r_flutter which actually does what this issue asked for, but it didn't use new build config.

    I updated it to use new flutter code generation, and I have a working version on my fork: https://github.com/szotp/r_flutter
    It generates asset names, font names and localization keys (thanks to @timfreiheit original work), and it automatically updates itself on changes.

    EDIT: My changes got merged. r_flutter package does exactly what was asked in this issue, IMO it can be closed.

  15. YK-Unit commented on Feb 26, 2020

    @YK-Unit

    Hi, all:

    I'm a member of Fly-Mix Team from NetEase company. My team did develop a flutter development tool: Flr(Flutter-R) to support the feature that you want.

    Flr is a flutter resource manager tool, which can help flutter developer to auto specify assets in pubspec.yaml and generate r.g.dart file which likes R.java in Android after he changes the flutter project assets. With r.g.dart, flutter developer can apply the asset in code by referencing it's asset ID function, such as:

    import 'package:flutter_r_demo/r.g.dart';
    
    // test_sameName.png
    var normalImageWidget = Image(
      width: 200,
      height: 120,
      image: R.image.test_sameName(),
    );
    
    // test_sameName.gif
    var gifImageWidget = Image(
      image: R.mage.test_sameName_gif(),
    );
    
    // test.svg
    var svgImageWidget = Image(
      width: 100,
      height: 100,
      image: R.svg.test(width: 100, height: 100),
    );
    
    // test.json
    var jsonString = await R.text.test_json();
    
    // test.yaml
    var yamlString = await R.text.test_yaml();

    PS: yes, it is just like the AAPT(Android Asset Packaging Tool) in Android.

    Up to now, Flr has supported Android Studio Plugin, CLI, and VSCode Extension:

    1. Flr Android Studio Plugin Version

    2. Flr CLI Version

    3. Flr VSCode Extension Version

  16. quangson91 commented on Feb 26, 2020

    @quangson91

    @YK-Unit Thank you and your team for the amazing work.
    I will try it.
    Thanks so much!

  17. added
    P3Issues that are less important to the Flutter project
    on Aug 18, 2020
  18. ishaquehassan commented on May 10, 2021

    @ishaquehassan
    Contributor

    @YK-Unit Thank you and your team for the amazing work.
    I will try it.
    Thanks so much!

    I wrote a simple package to do that in simplest way possible... with build_runner 🥳

    Here's a detailed article on medium https://medium.com/@ishaqhassan/indexing-assets-in-a-dart-class-just-like-r-java-flutter-3febf558a2bb

    As well as the package page on pub https://pub.dev/packages/assets_indexer

    Hope it helps the devs ❤️

  19. github-actions commented on Jul 11, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  20. locked as resolved and limited conversation to collaborators on Jul 11, 2025
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

    P3Issues that are less important to the Flutter projecta: assetsPackaging, accessing, or using assetsc: new featureNothing broken; request for a new capabilityteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions