devendroid / VectorChildFinder

VectorChildFinder helps to find inner elements of vector drawable like path and group.
Apache License 2.0
202 stars 41 forks source link

Can't use this anymore? #18

Open AndroidDeveloperLB opened 1 year ago

AndroidDeveloperLB commented 1 year ago

I tried to add this dependency, and I got this error when trying to build&run:

e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class com.devs.vectorchildfinder.VectorDrawableCommon, unresolved supertypes: android.support.v4.graphics.drawable.TintAwareDrawable
Adding -Xextended-compiler-checks argument might provide additional information.

Any way to fix this?

VectorDrawableChangerTest.zip

https://github.com/devendroid/VectorChildFinder/files/11108955/studio64_2023-03-30_10-26-26.zip

micstart commented 1 year ago

This can be done through Java reflection. First reflect call VectorDrawable.getTargetByName or VectorDrawableCompat.getTargetByName method to get VFullPath: Object path = ReflectUtil.invoke(vectorDrawable, "getTargetByName", new Class[]{String.class}, new Object[]{pathName}); Then Reflect calls the VFullPath.setFillColor method to set the color: ReflectUtil.invoke(path, "setFillColor", new Class[]{int.class}, new Object[]{color});

AndroidDeveloperLB commented 1 year ago

@micstart You can't use reflection on VectorDrawable, which the VectorDrawableCompat generates. It will crash because it's blocked.

micstart commented 1 year ago

@micstart You can't use reflection on VectorDrawable, which the VectorDrawableCompat generates. It will crash because it's blocked.

You can see VectorImage also use reflection.

AndroidDeveloperLB commented 1 year ago

@micstart If you check its code, it uses reflection on the VectorDrawableCompat itself, similar to here on this library. Not on VectorDrawable. It does it nicely though.

Here it's safer, does it directly, and allows you to modify it. In any case, I've made a fork for it and published it, together with some other features: https://github.com/AndroidDeveloperLB/VectorChildFinder

Here's the proof that what you've mentioned uses reflection on VectorDrawableCompat:

    private fun createDrawable(
        context: Context,
        @DrawableRes vectorResId: Int
    ): VectorDrawableCompat? {
        try {
            @SuppressLint("ResourceType") val parser: XmlPullParser =
                context.resources.getXml(vectorResId)
            val attrs = Xml.asAttributeSet(parser)
            var type: Int
            while (parser.next().also { type = it } != 2 && type != 1) {
            }
            if (type != 2) {
                throw XmlPullParserException("No start tag found")
            }
            return VectorDrawableCompat.createFromXmlInner(context.resources, parser, attrs, null)
        } catch (var6: XmlPullParserException) {
            Log.e("VectorDrawableCompat", "parser error", var6)
        } catch (var7: IOException) {
            Log.e("VectorDrawableCompat", "parser error", var7)
        }
        return null
    }
devmike01 commented 1 year ago

Hello, please check the latest code changes. Reflection has been removed and you can now continue using the library. Use the latest version https://github.com/devendroid/VectorChildFinder/releases/tag/1.0.1

AndroidDeveloperLB commented 1 year ago

@devmike01 Thank you. Can you please compare to the fork I've made, which has a few extra features, such as more finding functions, iterations (including deep iteration), and clicking ? I don't remember if I used reflection though.

devmike01 commented 1 year ago

Your source code is a zip file. Could you put it on a public remote repository so that I can take a look? Thanks.

AndroidDeveloperLB commented 1 year ago

@devmike01 I can't find the ZIP file. I've provided a repository, which is public: https://github.com/AndroidDeveloperLB/VectorChildFinder You can't access it?

devmike01 commented 1 year ago

I have seen the source code. Would take a look at it. Thanks