'iTextPdf : Android : Proguard issues with fonts , res/font/sfui_semibold.ttf not found as file or resource

While having ProGuard enabled in release build, i can not make PDF, crashing with this:

Caused by java.io.IOException: res/font/sfui_semibold.ttf not found as file or resource.
   at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:25)
   at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:66)
   at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:13)
   at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:15)
   at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:182)
   at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:214)
   at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:8)
   at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:7)
   at in.swipe.app.app.ui.utils.invoice_templates.InvoiceTemplate.getSemiBoldFont(InvoiceTemplate.java:10)
   at in.swipe.app.app.ui.utils.invoice_templates.InvoiceTemplate.initInvoiceHeader(InvoiceTemplate.java:11)
   at in.swipe.app.app.ui.utils.invoice_templates.TemplateBuilder.generateInvoiceTemplate(TemplateBuilder.java:43)
   at in.swipe.app.app.ui.utils.invoice_templates.TemplateBuilder.generateInvoiceTemplate$default(TemplateBuilder.java:3)
   at in.swipe.app.app.ui.invoice.details.InvoiceDetailsFragment.viewBill(InvoiceDetailsFragment.java:3)
   at in.swipe.app.app.ui.invoice.details.InvoiceDetailsFragment.requestPermissionViewBillLauncher$lambda-13(InvoiceDetailsFragment.java:3)
   at in.swipe.app.app.ui.invoice.details.InvoiceDetailsFragment$$InternalSyntheticLambda$0$15aa358d09d613429d28053a6b32a207c72d2528ec90baa1ce8f449e3bd9d3db$0.onActivityResult$bridge(InvoiceDetailsFragment.java:3)
   at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:1)
   at androidx.activity.ComponentActivity.onRequestPermissionsResult(ComponentActivity.java:20)
   at androidx.fragment.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:5)
   at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:8675)
   at android.app.Activity.dispatchActivityResult(Activity.java:8519)
   at android.app.ActivityThread.deliverResults(ActivityThread.java:5139)
   at android.app.ActivityThread.handleSendResult(ActivityThread.java:5187)
   at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:236)
   at android.app.ActivityThread.main(ActivityThread.java:8037)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

I think Font files are getting obfuscated and thats why iTextPdf is not able to find it, so anyone have solution for this ? some way to get reference to ttf file or proguard rule to not obfuscate Font files.



Solution 1:[1]

I suppose you can try keep rule to tell proguard not to obfuscate your library.

Just add this to your proguard-rules.pro file:

-keep <LIBRARY_PACKAGE_NAME>

And if you're not sure which keep rule to use, you can read more about it here:

https://jebware.com/blog/?p=418#:~:text=%2Dkeep%20disables%20all%20of%20ProGuard's,rename%20unused%20classes%2C%20for%20example.

Solution 2:[2]

Have you tried by adding below in ProGuard rules

add below lines in your proguard-rules.pro file: then Do clean and rebuild your project once before generate apk.

   -keep class com.itextpdf.** { *; }
   -dontwarn com.itextpdf.*

Also you can set your own proguard rules.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jimly Asshiddiqy
Solution 2 YuvrajsinhJadeja