Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric test cannot find font file

Tags:

robolectric

It took me couple of days for now but still couldn't find a solution. I got error when running robolectric tests that says font files cannot be found. The followings are the whole stack trace

android.view.InflateException: XML file build/intermediates/res/merged/debug/layout/fragment_users_list.xml line #-1 (sorry, not yet implemented): Error inflating class com. mycustom.common.views.RobotoTextView

// real issue here
Caused by: java.lang.RuntimeException: Font not found at [build/intermediates/bundles/debug/assets/Roboto-Light.ttf]
at org.robolectric.shadows.ShadowTypeface.createFromAsset(ShadowTypeface.java:73)
at android.graphics.Typeface.createFromAsset(Typeface.java)
at com.mycustom.utils.font.RobotoUtil.getRobotoTypeface(RobotoUtil.java:45)
at com.mycustom.common.views.RobotoTextView.setFontType(RobotoTextView.java:46)
at com.mycustom.common.views.RobotoTextView.<init>(RobotoTextView.java:33)
... 89 more

Basically I have xml files that use custom font file, the font file is saved in res folder, when I step up activity instance in Robolectric tests, and the activity is initialized, it will look for the font file in /build/intermediates/res/merged folder , but somehow the robolectric cannot find that font file.

Not sure why that happened. Any advice will be much appreciated!

like image 491
Cheng Avatar asked Mar 11 '23 21:03

Cheng


1 Answers

That is known bug with the upgrade of the Android Gradle plugin to v2.2.0.

As workaround add this to your build.gradle:

applicationVariants.all { variant ->
        def productFlavor = variant.productFlavors[0] != null ? "${variant.productFlavors[0].name.capitalize()}" : ""
        def buildType = "${variant.buildType.name.capitalize()}"
        tasks["compile${productFlavor}${buildType}UnitTestSources"].dependsOn(tasks["merge${productFlavor}${buildType}Assets"])
    }
like image 189
Eugen Martynov Avatar answered Apr 29 '23 20:04

Eugen Martynov