Code injection for Android - android

I want to write an ORM for Android as my final project.
My first idea is to inject code for each get and set.
Unfortunately I found a lot of posts saying there is no way to inject code in Android.
On the other hand I found "Dexmaker" to generate code on runtime.
My question is: Is it possible to somehow inject code in Android (by "Dexmaker" or anything else)?
If not how to do it ?
I also thought about reflections but I am afraid that it will be to slow.
Thanks in advance.
Edit
Simon:
Yes by ORM I ment object relational mapping.
I want to create a general framework. How else I could do this than not by code injection ?

You can inject code using Dexmaker. The project site has a HelloWorldMaker example that generates a class and then loads it.
You may find the performance of runtime code generation to be unsatisfying however. Projects like Dagger have had better success metaprogramming with code generation.

Related

Where is the code generated by #Compose located in an android project folder structure?

i'm studying jatpack Compose and trying to read some source code of it. Noticed the Compose annotation is critical in this framework so i want to read the code generated by it to check what is actually done within it. However i cant find where the code locate, and since it's a quit new stuff, nothing could be found through out the internet. Beg for your idea, thanks!
That is a big topic.
The #Composable annotation is not processed by an Annotation Processor that generates source code. Instead Google had built a Kotlin Compiler Plugin that processes the annotation and weaves its magic into the compiled code directly.
Leland Richardson is the engineer that works on this and has explained a lot of what happens behind the curtains. For example, start here:
http://intelligiblebabble.com/compose-from-first-principles/

Implement camera lib with Kotlin multiplatform

I'm trying to understand what structure should have a multiplatform library. Checking on the Internet I've seen a huge number of examples explaining how to make a log or a "hello world" but there's a lack of complex examples, even in the official documentation (important to note that I'm only interested in mobile platform, iOS and Android).
So I want to create an example that simply opens the camera (as a lib, not as a multiplatform app) just to have an idea of how to work with a real feature which, also, is native. Right now I have created a project following the official example, so it has a common module (using expect) and one for Android and one for iOS (using actual), and now these are my doubts:
I've seen that the iOS module is also in Kotlin, Kotlin/Native as I understand. Should my project have also an wrapper in Swift, or will the library have no Swift code? And if it should, where should it be in the project structure?
Also in the Android module I've noticed I cannot import the class "Activity" nor the "Intent", which I will need to open the camera, why? is this code restricted to Java without the Android libs? Should it also have a wrapper to Android? If so, how can I configure this wrappers?
I know I can use the "expect" key when creating classes but, as I understand, the common and the native modules will always be separated classes. I mean, if I create a class in the common module, can I define methods of this class using "expect" and define them later in the native?
Can my lib have a Manifest?
Finally, does anyone knows a real example that really explain a more complex situation?
Thanks
Okay, let's go through your questions one-by-one.
I would recommend you to have a look at this example
The
iOS module produces an Objective-C framework as a result. It can be utilized by the Xcode project the same way as any other framework with non-Kotlin origins.
It looks like the unavailability to use
Android SDK is the result of using jvm("android") target instead
of android() one. To use the android target, one has to apply the android Gradle plugin in addition to kotlin-multiplatform one.
I
think you want to do something like that: just ordinary class
declaration in the common and extension function for it with an
expect modifier. And then actualize it in the platform-specific
code.
I think so.
I'd also recommend you to have a look at
this and this, maybe these examples will be complex enough for you.😁

is it safe to migrate whole android project in kotlin at once?

We have one mobile project fully developed in native android using android sdk.
we want to migrate the code base to kotlin completely. So wanted to know what are
main things to keep in mind while migrating to Kotlin.
Before you convert your project from Java to Kotlin keep things in your mind.
Converting from Java to Kotlin will may cause your git track. Refer to avoid this problem
Android studio provide easy way to convert Java code into Kotlin in a automate way [Press Ctrl+Alt+Shift+K]. But it may arise some conflicts like "Some code in the rest of your project may require corrections after performing this conversion. Do you want to find such code and correct it too?". For example in your java file contains static variables. Because kotlin didn't support static. In this case you have to manually correct it. It took some time.
While developing you may maintain many branches. If you converted one file from Java to Kotlin will affect other branch.
I don't recommend to switch full code automatically to Kotlin.
The tool for converting to Kotlin is great but you have to review all code generated by the tool. Sometimes code generated are not really readable.
Java and Kotlin can be use together. You can convert files one by one. You have time to check and clean the converted code.

Android (Kotlin) code coverage anomalies

Code coverage (Jacoco) in Android connected tests is a very useful way to determine what methods/functions need some TLC. Now that I am switching to Kotlin over Java I have discovered some anomalies that I cannot explain, as this screen shot illustrates:
The methods starting with _$... are internal to Kotlin or Android, I strongly suspect. My questions are: 1) does anyone have any insight into why these methods are included in the Jacoco code coverage report, and 2) is there a way to exclude them?
Those methods are added when using the synthetic properties via Kotlin Android Extensions. Each Kotlin Activity using synthetic properties will have those methods added.
Kotlin Android Extensions is a plugin for the Kotlin compiler, and it does two things:
Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn't increase the size of APK much.
Replaces each synthetic property call with a function call.
Explanation on the official docs:
https://kotlinlang.org/docs/tutorials/android-plugin.html#under-the-hood
This articles does a pretty good job going into detail:
https://antonioleiva.com/kotlin-android-extensions/
In large part due to the answer from #triad, I was able to come up with a solution that is a work-around to what appears to be a Kotlin bug. The work-around was documented in the Antonio Leiva post referenced by #triad. In a nutshell the solution was to turn off caching in MainActivity. The complete solution is up on GitHub.
What I do not completely understand is the cost of this solution so I will hold off checking this answer in the hopes that a better solution is provided.

How frameworks generate code and add to class path

I apologies for my ignorance. Lately I have been working on Android framework. The developer said it is fastest framework built to the date because it does not uses reflection and generates code while building apps. I just want to know how these frameworks generate code and how they include this code in class path. What's there approach ? I want a general concept because I have few ideas and I want to implement them like this.

Categories

Resources