Solution 1 :
I’ve managed to solve the long compilation time problem and just in case anyone might stumble upon this post, I’ll post my solution here.
Apparently, 9000+ AnimatedVectorDrawable wasn’t the problem. The problem was that I was using Inline complex XML resources method to create my AnimatedVectorDrawable. Inside each of my AnimatedVectorDrawable contains multiple VectorDrawables and ObjectAnimators. This means that even if there is only 1 AnimatedVectorDrawable XML file, the AAPT tool will create multiple XMLs for the VectorDrawables and ObjectAnimators which also means that I ended up with way more than 9000+ AnimatedVectorDrawables. This was the cause of the long compilation time.
So, I ended up creating VectorDrawables instead and created a Class to handle the ObjectAnimators at runtime. However, to achieve this, I had to use Java reflection techniques to access the private getTargetByName function in VectorDrawable to get the Target object for the ObjectAnimators and use the invalidateSelf function of the VectorDrawable in a Thread to update the VectorDrawable at runtime.
Problem :
I am making a Chinese dictionary app that can show users the Chinese characters stroke order animation. I am using MakeMeAHanzi as the source which can be used to generate animated SVGs for the stroke order animation. One SVG for one Chinese character. Using the same concept, I generated animated vector drawables instead.
Currently my android app contains 9000+ animated vector drawables and it is taking a long time to build. I am also getting this error that only occurs when I add in the 9000+ animated vector drawables:
Waited 10 minutes (plus 203200 nanoseconds delay) for com.googl[email protected][status=PENDING]
I’ve considered using GIFs and load it from the expansion file instead but I would really like to have the advantage of resolution-independence. I’ve tried loading the animated vector drawables from the expansion file but it seems that I can’t load .xml from the file system as drawables unless I precompile it as binary XML and I think this will be my last resort as I am still not sure how to achieve this yet since I am unable to build the APK (I can extract precompiled binary XML from the APK).
May I know if there are more efficient ways of handling large amount of vector drawables or other alternatives to handling this situation? Thank you.
Comments
Comment posted by asky
Can I ask why you have so many animated vector drawables? the fact that you have over 9000 of them is a bit of a code smell.
Comment posted by CommonsWare
You could try creating a library module in your project, putting these resources in the library module, and having your app module depend on the library module. The initial build will take the time that your are used to, but it may help with future builds. Basically, you want the build system to not have to mess with these resources very often. But, beyond that, I’ll echo asky’s question: why do you have 9000+ animated vector drawables in the first place? We cannot advise you on “other alternatives to handling this situation” until you explain in detail what you are doing with them.
Comment posted by MakeMeAHanzi
I’m actually making a Chinese dictionary that shows the stroke order animations. The source that I used is from
Comment posted by Edwin
@asky I do agree that my approach is problematic. I am still looking for a proper way to handle it.
Comment posted by asky
The use case you described seems like a good reason to have 9000 SVGs. There’s probably no way to reduce that number and have the app serve its intended purpose. I have never run into this problem before, so all I can think of is that if you expect the user to only look at a few drawables simultaneously, you could generate drawables from SVGs at runtime. I don’t know of a good choice of library to do this (many android svg libraries appear to be unmaintained), so this option might not work in the end.