https://kotlinlang.org logo
#android
Title
# android
r

rahul_lohra

07/23/2020, 4:45 AM
Hello, I needed some help in using
java agent
in Android, basically there is a
premain function
and I need this function to run before the Android's main function bus I was unsuccessful. So I am looking for some advice, what can go wrong? Also I am not getting anything on the log cat so it very difficult to debug The is the
manifest
of the jar
Copy code
Manifest-Version: 1.0
Premain-Class: com.rahullohra.myagent.MyPremain
Launcher-Agent-Class: com.rahullohra.myagent.MyPremain
Boot-Class-Path: /home/rahulkumarlohra-xps/AndroidStudioProjects/Lab/m
 yagent/build/libs/myagent.jar
In
gradle.properties
I have passed this jar as
Copy code
org.gradle.jvmargs=-javaagent:'/home/rahulkumarlohra-xps/AndroidStudioProjects/Lab/myagent/build/libs/myagent.jar'
a

Adam Powell

07/23/2020, 1:32 PM
This doesn't exist in Android. Android app startup happens in a very different way. What are you trying to do? What code do you want to run before other app code at startup and why?
r

rahul_lohra

07/23/2020, 4:14 PM
Hi Adam, I want this feature to be available only for
DEBUG
variant TL;DR - I want to inject bytecode/java code some classes during compile time at specific places. I do not want to use any annotations. Full story: What I want & Why: - Two things First - I want to track our image loading performance in our app. Now assume my team is using Glide to load images and lot of people have already written code like below
Copy code
Glide.with(context).load(url).into(imageView)
Now during
compile time I want to  attach listener
to above code and it will now look like this -
Copy code
val glideListener = {....some logic...}
Glide.with(context).load(url).addListener(glideListener).into(imageView)
Now I know anyone will say that one should use a common class to load images, so that all the image loading calls will go through there and I totally agree with this. But I just want to explore what are the other possible ways to achieve this thing Second thing - I want to avoid exceptions. So I want to execute some instructions just before my actual content function will start to execute (This is very similar to aspectJ)
a

Adam Powell

07/23/2020, 5:17 PM
A pre-main is by definition runtime as opposed to compile time, you shouldn't need it if you are doing compile-time transformations.
If you do have runtime startup code you might find this useful: https://developer.android.com/topic/libraries/app-startup
but based on your description it sounds like this isn't really what you want either
r

rahul_lohra

07/23/2020, 7:23 PM
Thank you for correcting me Adam regarding compile-time transformations & compile-time transformations. 👍 And yes app-startup will not help me. Thanks to you, now at least I know where not to look.
For run-time class transformation - I think I should read on how the Android OS works and starts an application, I think from there I can get some hint For compile-time transformation - I think if I just write a simple script that will just modify the code the way I want. Then I can get what i want. Once again thanks a lot Adam👍
👍 1
j

Justin Tullgren

07/24/2020, 3:58 PM
Hi, I have a couple of suggestions if you want: 1. Use debug/release source sets. Pull the code you want to augment into a top level kotlin function. Declare it in both release and debug source sets but not in the main source set. In the debug add the listener/extra code. The release is the normal code. Then in main call the extracted top level function. 2. If you truly need byte code level you can use AOP byte code weaving using this plugin : https://github.com/Archinamon/android-gradle-aspectj and write aspects for your code in aspect j syntax, no annotations, where you can define your pointcuts. This happens at compile time in during the AGP transform step.
r

rahul_lohra

07/27/2020, 5:45 AM
Thanks Justin, I will try this library
10 Views