B3nedikt / restring

Restring is a android library to replace string resources dynamically
Apache License 2.0
317 stars 31 forks source link

@abdelmalek-roshdi This is not a problem, you just need to set the package correctly. Something like this should do it: #140

Closed MirkoWu closed 11 months ago

MirkoWu commented 11 months ago
          @abdelmalek-roshdi This is not a problem, you just need to set the package correctly. Something like this should do it:
@file:Suppress("PackageDirectoryMismatch")

package android.content.res;

class RestringTypedArray : TypedArray() {

    override fun getText(index: Int): CharSequence {
        return super.getText(index)
    }
}

In short the java compiler doesn't check if your package directive matches the actual file location. You can use this "feature" in all kinds of ways, many other java/kotlin features are based on this fact.

Originally posted by @B3nedikt in https://github.com/B3nedikt/restring/issues/111#issuecomment-1300535042

MirkoWu commented 11 months ago

on Android 12,crash by: java.lang.NoSuchMethodError: No direct method <init>()V in class Landroid/content/res/TypedArray; or its super classes (declaration of 'android.content.res.TypedArray' appears in /system/framework/framework.jar) I cannot instantiate RestringTypedArray。 log:Accessing hidden method Landroid/content/res/TypedArray;-><init>()V (blocked, linking, denied)

B3nedikt commented 11 months ago

You can fix this error by including the following library in your project: FreeReflection.

I would not really recommend using TypedArrays with Restring though, it will be a bit hacky and hard to implement. You can use the Reword ViewTransformer API instead, see here how to use it for custom views. Or just use getString(...) as usual if you have access to the source code of the custom view you want to use with Restring.

B3nedikt commented 11 months ago

Solved I guess, feel free to reopen if you have further questions :)