How to swap method bodies under Android - android

In pure Java, using intrumentation and java agents, there is a way to replace the body of a method at runtime.
Unfrotunately java.lang.instrument.Instrumentation is not available under Android.
I have checked all other alternatives, like the one described here in stackoverflow, which directs to the obsolete javassist-android implementation. Which surprisingly works, but only for new classes.
There's also an article named Hot swapping code in Android which describes how again to load classes at runtime, not how to replace existing methods of classes.
The only article dealing with this issue is an article named Android hacking: replacing system classes methods with your own, which misses the source code and refers to Dalvik, not ART (so it won't work on modern hardware).
So, is there any way to do so?
...
Bottom line: the reason I want this is to trace specific method calls in my application, like just when this method is called (and do some action before it) and just when this method exits (and also do some action on it). These methods are defined at runtime, so there's no way to know them in advance, at compile time.

One of the new features in Android Studio 3.5 is Apply Changes, which basically performs similar logic to what you're looking to achieve:
".. we rely on runtime instrumentation that is supported in Android
8.0 (Oreo) and newer devices and emulators to redefine classes on the fly."
Also, in episode 108 of ADB Podcast Esteban de la Canal mentioned:
".. In Android Oreo (8.0), the platform implemented bytecode
instrumentation on the fly, so we can actually change the dexed class
of a running app.. by attaching, basically, an equivalent of
JVMTI in Java, so we can attach an agent and say: can you swap
this class with this one. "
Start with reading the art/openjdkjvmti sources, i.e. the readme documents the following:
openjdkjvmti plugin
This is a partial implementation of the JVMTI v1.2
interface for the android runtime as a plugin. This allows the use of
agents that can modify the running state of the program by modifying
dex files in memory and performing other operations on the global
runtime state.
Once Android Studio 3.5's sources will be released, see how they used those new APIs in Apply Changes implementation.

Although the j.l.instrument APIs were never added to android the underlying JVMTI apis that were used to implement them were added in Android O. For example here is a test JVMTI agent that will redefine listed classes and add a single nop to the beginning of every method: https://android.googlesource.com/platform/art/+/refs/heads/master/tools/jvmti-agents/simple-force-redefine/

Related

How to use simulcast with webrtc android sdk?

(I am newer of stack overflow user and sorry for my poor English.)
I am using WebRtc Android SDK to create a chat app, and our team decides to use
simulcast to deal with various abilities of participants. However, when I come to the SDK APIs, I can not find a way to use simulcast.
I use the SDK with offical-recommended manner:
implementation 'org.webrtc:google-webrtc:1.0.+'
(The concrete version is 1.0.28513.)
I have googled much and found some code fragment like this:
RtpTransceiver.RtpTransceiverInit transceiverInit =
new RtpTransceiver.RtpTransceiverInit(peerConnectionParameters.transDirection, mediaStreamLabels
/*, encodings*/ // cannot create Encoding instances
);
...
peerConnection.addTransceiver(MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO, transceiverInit);
However, when I try to create Encoding instances,I found the Encoding's constructor cannot be accessed, it is package access limit.
By the way, I have tried using reflection to forcibly create Encoding instances.
But it will cause an error when this code executes as might have been expected:
peerConnection.addTransceiver(MediaStreamTrack.MediaType.MEDIA_TYPE_VIDEO, transceiverInit);
I have also gone through the SDK APIs, and cannot find any other way to set up the simulcast.
So how should I use simulcast with webrtc android SDK?

How to identify incompatible classes in libraries?

Background
For the past few weeks, I've been on a mad quest to build a native Android client for an AWIPS2-EDEX server based on the open-source code for their Eclipse-framework-based Java client.
The specific repo of interest: https://github.com/unidata/awips2-core (and its dependencies, https://github.com/unidata/awips2-core-foss).
The Problem
I finally managed to get enough of awips2-core to build in Android Studio as a Gradle-based module (see my github fork at https://github.com/jskubick/awips2core-android ). After I finished celebrating my victory at getting it to build, I set out to actually make use of it in an actual Android app... then watched the build fail because awips2-core has dependencies on JAXB, which has been exhaustively documented elsewhere as being fundamentally incompatible with Android(*).
I'm now attempting to eliminate the module's dependencies on JAXB, but it got me thinking... how many MORE unexploded mines are out there waiting in the code for me to trip over... things that compile fine as "straight Java", but will blow up the moment something Android-specific references them?
Is there any tool that can scrape through library .jar files, and identify any dependencies within them that can't be satisfied by Android?
Update: my attempted project to use awips2-core directly with Android is dead. Upon further investigation, the implications of awips2-core's extensive use of Apache Camel sank in, and I realized that attempting to use awips2-core directly by Android in its present form is hopeless. As I understand it, Camel dynamically builds and compiles its own classes at runtime... which is completely impossible under Android, but allowed by "regular" Java.
This doesn't mean I've given up on my goal of writing an Android awips2-EDEX client, but it does look like my goal of reusing the existing code in awips2-core to write an Android client for EDEX is a lost cause & I'm going to have to write my own Android EDEX client from scratch.
(*) By "fundamentally incompatible", I mean that there's no known way to build it in a way that allows it to work with unmodified JAXB-using generic Java code built for Android. This is due to at least two problems:
Namespace problems. Think: BouncyCastle-vs-SpongyCastle. Apparently, Android hijacked SOME of JAXB's namespace for its own purposes, and Dalvik/ART's classloader won't allow you to override it with your own code using the same namespace.
Limits to Dalvik/ART's introspection capabilities. Basically, "real" Java has runtime introspection capabilities related to annotations that Dalvik/ART not-really-Java doesn't support... and some functionality with JAXB depends upon it.
From what I've read, various individuals over the past ~7 years have found partial work-arounds for 1 or 2, but the collective one-two punch of both has ultimately led to everyone who's tried to find a solution to either throw in the towel and write off the problem as intractable & hopeless, or hack it just enough to make it work for whatever they originally needed it for & call it a day.

Pure functional programming on android

Are there any advancements in this area? I want to be able to write purely functional code on Android in Haskell or similar languages. I've tried some examples with Scala but it seems to be a pain to get started. Are there any other functional JVM languages which I can use to write Android applications?
Edit: functional languages that write native android applications. My mistake about the JVM.
I doubt that you can find anything mature for writing Haskell-like code for Android. You do need to implement Java abstractions which are required by Android API (implement activity, etc.).
But if you really want to write for Android in a purely functional style you can try to implement your business logic in a pure functional language that compiles to JVM and call it from your Java classes. That approach would be much simpler than trying to implement it entirely in pure functional style.
As your language choice, you can try
Frege, it even has a library for android - froid
Eta lang, it is very new and probably nobody has tried to use it for Android yet
I you want a painless solution in terms of Gradle builds etc., you have only two options: Java and Kotlin, of which of course you should choose Kotlin ;)
Kotlin has most of the things you need to write in functional style:
functions as first class citizen
higher-order functions
immutable collections
var and val like in Scala
if-else as a statement
elements of pattern matching (where statement)
tail recursion
and more...
If you also include funKTionale and kotlinx.collections.immutable, you'll have all the functional goodies like: Option, Try, currying, memoization, persistent data structures and so on...
To start with Kotlin just install the latest Android Studio 3 Preview, which already has built-in Kotlin support.
BTW, don't be so polarized into "pure" functional ;) After all, being 100% "pure" means no side-effects, which means your app can't interact with the user ;)
Hope this helps :)
I've never tried it personally, but you can do F# programming using Xamarin.Android (and, I believe, with Xamarin Forms too). You can see the guide here. (It also includes sample code).
As some background, F# is the .NET Framework's functional language. It is derived from ML; in fact, many ML scripts can be compiled almost "directly" as F# (with the caveat that you may have to do some renaming because F# has some additional keywords that ML doesn't have in order to support several .NET-specific extensions).
Xamarin allows for native development for Android, iOS, and Windows phone. Xamarin Forms allows for a single code-base for all three platforms (it's a competitor to Ionic).
One more quick point: Android does not use the JVM, even if you're writing apps in Java. (In fact, Android does not even support all Java 8 features yet). Through Android 4.4 it used Dalvik; after that, it started using Android Runtime.
You could also try using a JVM language like Scala to create a JAR file and create a bindings library for it.
Please also note that you'll end up using at least somewhat of a mixed paradigm - for example, things like Activities are objects, and the XML files used to define an Android screen is, for all practical purposes, declarative. Edit: This last point is slightly debatable - see the comments.
One final possibility: I haven't checked this out too closely, but try also this link for a site claiming you can do Scala in Android.
Between Pure functional and Java, there is a way which is IMO Pragmatic functional. For instance Redux achieves that in the React arena.
My goal is to write an app (Activity) having an immutable state that advanced as a result of interactions is functional.
In the browser you can see that done with elm (Haskel like language which is also web platform)
Since we want an Android app, I opened the Android Studio used the wizard to create and app with Navigaton Drawer Action bar (with Drawer, FloatingActionBar),
Then converted it to use Elm concepts of immutable model functional approach into a working POC based on a small ElmBase class and idioms.
The code is written in Kotlin (JetBrains tool of choice for the JVM).
You can find the app at my GitHub https://github.com/saffih/ElmDroid The sweet spot of that approach is that it leverages Kotlin being strongly typed and the editor does code completions very well,
making lot's of the code completed for me in a way I have never seen before - amazing experience (But it require using idioms like the sealed class and when properly).

How are your experiences with NativeScript?

Does anyone have experience with NativeScript and can compare it to developing native apps, especially for Android?
I have read all these articles:
FIRST THOUGHTS ON NATIVESCRIPT
SECOND THOUGHTS ON NATIVESCRIPT
Introduction to Native Script – Is It Worth Your Time?
My Experience Developing with Telerik NativeScript
I know especially three of them may be outdated. But I want to ask all of you developers:
How is your experience with NativeScript?
Are there any Android-Components you cannot use? Which are these ones?
Is styling really so limited?
Do apps really look so different at runtime as in the mockup as in the pictures of the first article referenced above?
Does loading of native Android objects into JavaScript Code always work correctly?
Does NativeScript generate Java-Code for Android-Platform out of the NativeScript code I write?
Is it possible to modifiy this code if I want to use some native-only features? What if I want to make UI changes then? Do I have to regenerate the code and do I miss my native extensions then?
Very glad to see that you are evaluating NativeScript to eventually use it in present and future projects.
I'll try to condense answers to a few of the questions into one, as they really are mostly related.
Skipped.*
That depends on what has already been exposed through a custom view/plugin or module. The core-modules that every NativeScript app comes with contains the most basic of wrappers for both Android and iOS under a common API. There are plugins (nativescript npm modules) that provide additional wrappers on native android views (nativescript-telerik-ui for one, nativescript-carousel), most of which are created by the NS community.
As RexSplode mentioned before me - it's mostly the platform that imposes certain limitations. NS uses CSS to declare style, but you can also access the native components and manage their style and appearance programatically if what you need isn't readily available out of the box.
First I'd like to note that the first 3 articles you've linked are over a year old now, and trust me, NativeScript has evolved a lot since then. With all the available components (remember the npm modules I mentioned earlier?) there's a good chance that you will get a close to 1:1 similarity to a well-styled native Android mockup.
At build time metadata is generated for the Android/Java public API used in the project. When the JavaScript Engine (V8) fires up, that metadata is loaded into memory, prototype chains are constructed, and callbacks are attached, so that when you call new android.widget.Button(); in your JavaScript code, the proper virtual machine instructions will be called, and a native button will be created. Static methods are accessed similarly, check out the official docs to get a better understanding of how it all works.https://docs.nativescript.org/runtimes/android/advanced-topics/execution-flow
and 7., and a cont. of 2. Java code, or rather compiled Java code is generated whenever you wish to extend a native Android class that isn't available already in a module or in the native Framework. Extending classes is very similar to how you would do it in Java - you extend a class, and create new implementations of interfaces. That means that you won't have to open Android Studio to create a new class, build it into a native plugin and then add it to your project, since you can do it all in your NativeScript code using JavaScript/TypeScript. https://docs.nativescript.org/runtimes/android/generator/extend-class-interface
Disclaimer: I am on the NativeScript Engineering team
I investigated the Native Script a little and my colleague at work writes an app with it, so I can offer you a bit of information that I have.
1. skipped
There are limited amount of components you can use with native script out of the box. However, if you have a native-java developer who can write a wrapper for you - you can use everything.
It is limited to the platform you are using. Android itself has a lot of style limitations which cannot be easily overwhelmed.
don't know
It works a little different. Your JS object, or rather widgets are translated to java code. So with the items from the box - yes, they are okey. If you write a wrapper for your custom component, then all is up to you.
Yes it does.
No, the code is generated, how are you going to modify it? Changes will be undone on the next build. However, you can write a native module for your application and use any features you want. It is like defining an interface, which you can use in JS code afterwards.

Can the Eventful java client library be used for Android?

I am creating an app showing local events for android. I was hoping to use the Eventful API, since that came with its own java-based client library. However, I'm not sure if it's fit for Android, since I know a lot of these java based client libraries use stuff Android doesn't support.
So, does anybody know if it works?
My entire project is available # github if you want to check it out for yourself.
The API is found here.
Android does not have have issues with Java client libraries. It is build on top of standard Java, and can use all of the framework features.
Furthermore, it looks like this API offers a RESTful interface, which is for sure supported by Android.
Bottom line, I do think you can use this API in Android without issue.
I'd say the easiest way is to compile and run an application that embeds the library and tests a few methods.
Typically, you may have issues with the way the networking is handled. There are 2 main ways in android to do HTTP, the Java and the Apache way, I think the Java URL API is fully supported and very close to the actual Java version, but the Apache has some hidden differences.
The main risks you'll have are A/ that it uses classes or packages that are not present on Android. B/ that a class does not behave as expected, which does happen from time to time, as the Android implementation is entirely specific.
Apparently you have already tried to run an android app with the library included? Did you encounter a specific error? If you, can you post the stacktrace?

Categories

Resources