likethesalad / android-buddy Goto Github PK
View Code? Open in Web Editor NEWTransform Android project classes with Byte Buddy at compile time
License: MIT License
Transform Android project classes with Byte Buddy at compile time
License: MIT License
I am running into an issue where an incremental build fails and I have to do a clean build (which succeeds). I am running into this quite often. Did I mess something up during the installation or is this a limitation or bug of this plugin?
I am using the Aaper library so I am not using android-buddy directly.
How we could change the returned value of a function?
Let's assume that we have the following class:
class Foo : Bar() {
override fun barMethod(): Baz {
return super.barMethod()
}
}
What we want to do is to wrap the result and return the wrapper instead. Something like that:
class Foo : Bar() {
override fun barMethod(): Baz {
return BazDelegate(super.barMethod())
}
}
class BazDelegate: Baz() {}
I 've created the interceptor, but the originalMethodCall
as described in the example is a Runnable
, which means that it does not return anything.
object MyInterceptor {
@JvmStatic
fun intercept(
@SuperCall originalMethodCall: Runnable
) {
originalMethodCall.run()
}
}
I'm trying to configure environment for using android-buddy. I found External Libraries has 'com.likethesalad.android:android-buddy-tools:1.1.1' but I got error when insert the [ id ' android-buddy']. Is it changed the method for configure the gradle?
below image : build.gradle(app)
I am trying to instrument a rather large application written in Kotlin and ran into some problems with bytecode generated by the Kotlin compiler that is not valid for byte-buddy. For reference, the issue is very similar to this: raphw/byte-buddy#764
The proposed solution is to disable validation using: new ByteBuddy().with(TypeValidation.DISABLED)
.
But I did not find a way to register a custom EntryPoint
or a custom ByteBuddy
instance. It would be very helpful to expose an interface where a user can overwrite the configuration of ByteBuddy
.
A possible solution might be to expose a new decorator @EntryPoint
, allowing android-buddy to find the EntryPoint
defined by the user, which might look like this:
@EntryPoint
public class MyEntryPoint implements EntryPoint {
@Override
public ByteBuddy byteBuddy(ClassFileVersion classFileVersion) {
return new ByteBuddy().with(TypeValidation.DISABLED);
}
@Override
public DynamicType.Builder<?> transform(TypeDescription typeDescription, ByteBuddy byteBuddy, ClassFileLocator classFileLocator, MethodNameTransformer methodNameTransformer) {
return byteBuddy.rebase(typeDescription, classFileLocator, methodNameTransformer);
}
}
If such an EntryPoint
is found, android-buddy
could then use an instance of it, instead of the default EntryPoint (as currently done in
BTW awesome project :)
Suppose I have a student class like below
`class Student {
String firstName;
String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
}`
Now I have created the transformation to intercept getFullName method call like below
`@Transformation
class StudentTransformation(
private val logger: Logger
) :
Plugin {
override fun apply(
builder: DynamicType.Builder<*>,
typeDescription: TypeDescription,
classFileLocator: ClassFileLocator
): DynamicType.Builder<*> {
logger.lifecycle("Transforming ${typeDescription.name} with StudentTransformation")
return builder.method(ElementMatchers.named("getFullName"))
.intercept(MethodDelegation.to(StudentClassInterceptor::class.java))
}
override fun matches(target: TypeDescription): Boolean {
return true
}
override fun close() {
// Nothing to close
}
}`
and here is the interceptor
`object StudentClassInterceptor {
@JvmStatic
fun intercept(@Origin m: Method, @SuperCall originalMethodCall: Runnable): String {
Log.i("myTag", "Intercepted Student Class getFullName ${m.name}")
return "John Doe"
}
}`
It is working fine when I am calling getFullName on a student object and I am getting "John Doe" as it has been intercepted.
But same thing I want to do for android View.class. Inside View class there is a method named "getHeight()" which return the height. I want to intercept this method call. When I call this method on a button I want to return a fixed value. But it is not intercepting. Here is the code for ViewTransformation
`@Transformation
class ViewTransformation(private val logger: Logger) : Plugin {
override fun apply(
builder: DynamicType.Builder<*>,
typeDescription: TypeDescription,
classFileLocator: ClassFileLocator
): DynamicType.Builder<*> {
logger.lifecycle("Transforming ${typeDescription.name} with ViewTransformation")
return builder.method(ElementMatchers.named("getHeight"))
.intercept(MethodDelegation.to(ViewMethodInterceptor::class.java))
}
override fun matches(target: TypeDescription): Boolean {
return target.isAssignableTo(View::class.java)
}
override fun close() {
// Nothing to close
}
}`
Here is the interceptor
`object ViewMethodInterceptor {
@JvmStatic
fun intercept(@Origin m: Method, @SuperCall originalMethodCall: Runnable): Int {
Log.i("myTag", "Intercepted View Class getHeight")
return 500;
}
}`
Now I am calling getHeight() inside onCreate of MainActivity likhe this
btn.getHeight()
But getHeight is not intercepting. What's I am doing wrong? @LikeTheSalad
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.