Using Java-Insipired Language in Android App - java

Here is my situation.
I am using a custom programming language thats syntax is based on Java.
In order to compile this *.custom file I use the java classpath to locate the .jar that contains the .class files. This converts the .custom to .java from there I compile down to Java Byte Code and run on JVM. Now I am looking to use this custom language on Android. Is there a way that I can incorporate a .custom file into android and compile into .java and then to Dalvik Byte Code to run? Looking for any suggestions to run .custom on Android
Thanks

The Android dev tools are ultimately operating on Java .class files. If you can compile your code into something that will run on the JVM with the class-library restrictions of Android, you can compile the .class into a .dex. Keep in mind that you'll need to be referring to the Android API in your programs so the Android installer can link them to the Android runtime.

You can either compile to .class and then convert that to .dex using the dx tool. Or you can use DexMaker to go right to .dex from source.

Related

Using external non-Android Java library in Android: couldn't find .so file and so on

I'm trying to build a template Android app using Grammatical Framework's Java bindings (however, I don't think what Java code I'm using in particular actually matters. Let's just say I'm trying to use some external non-Android Java code in an Android app).
After some trial and error, I got to successfully build an app using this external code by following these steps:
generating a .jar file for the library I want to use (it's called jpgf.jar
copying it to app/libs
adding it as a library and verifying that the build.gradle gets updated with
implementation files('libs/jpgf.jar')
importing it in my app's MainActivity and writing some code that uses it
However, nothing can be easy in Android development, ever. So my app, after nicely compiling, crashes on startup with the following error:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/bho.harisont.gfdemo-8c870hpW06uEnMQHV1ILjw==/base.apk"],nativeLibraryDirectories=[/data/app/bho.harisont.gfdemo-8c870hpW06uEnMQHV1ILjw==/lib/x86, /system/lib, /system/product/lib]]] couldn't find "libjpgf.so"
In short, some .so file can't be found. Now, I'm not Java or .so file expert, but I do know that when I installed jpgf system-wide on my machine using the dedicated Makefile, a .so file was generated and copied to some location. My probably naive attempt was to take that .so file and move it to the appropriate subfolder of jniLibs. I know I placed it in the correct folder and all, because in this way I managed to change the error message, but not to solve the problem: at this point the complaint is that libjpgf.so is 64 instead of 32 bits. You bet, my machine is 64 bits.
So, to help me you can answer one both of these questions:
how do I compile a 32 bit version of the .so file on a 64 bit machine? I tried some gcc options but it's not like that helped
do I actually need that prebuilt .so file? I see other people just importing JARs. Can I not just do that (or even import the source code somehow), and have Android Studio generate all the assets it wants the way it wants?

Modify Android Java source code and repackage to APK file

I decompiled an Android APK using Jadx-GUI to view the Java source code. I want to modify the application, but the only way to do that is to make changes to the smali code and repackage the files into an APK via apktool.
I currently have made some changes to the Java code for a particular file. I now want to make the changes to the corresponding smali and then repackage the app. I am confused on how to do this.
Convert .Java file to .Smali file
From this post:
"You can compile the java classes using a normal java compiler, and then use Android's 'dx' utility to convert the compiled .class files to a dex file. And then run baksmali on the dex file to produce the smali files."
I'm not sure what this means. Do I need to just compile the one file I made the changes to? Or do I compile all the files of the project together? Since smali is android assembly, I would imagine that I would need to compile everything together so that registers are allocated properly, etc.
Lastly, if I use a normal java compiler, won't the compiler throw an error when it encounters Android specific imports?
Eg. import android.graphics.drawable

How to change a file generated by J2Objc

Hello I have been trying for a while to changue the url from a file .m generated by the tool j2objc but xCode seems to not notice it, when I recompile the whole project it's still pointing to the url generated from the java file.
Could you help me to figure out how could i make this change, I have been looking for hours but i have couldn't.
After several intents I find out that this is not possible, the reason is simple, when you compile the Java code with J2Objc the api generates .o files and .m & .h files just for reference, but the files that your iOS app is going to use are already compiled (*.o) so although you modify .m and .h will not take effect because xCode is using the .o files generated by j2Objc.
So the solution is recompile again the changes with j2objc.

Convert a String into Java Source code in Android

Does anyone know if it's possible to convert a String/file into java source which can be compiled at run time using something like JavaCompiler. It looks like this is possible with Java 6, but I haven't seen anyone say that JavaCompiler is available in Android.
Basically my main goal is to turn a String or file text into source code in Android. Does anyone know how that can be done?
Thanks!
Android runs Dalvik not Java 6. JavaCompiler is not included in standard Dalvik distribution, so you cannot use it. Dalvik runtime is designed for embedded system as such it is less dynamic, compiling code on the fly is one of the things that it is not supposed to do.
Try what Hyangelo suggested, or Google for other scripting libraries. Clojure for example. ;)
You could do something advanced and setup a web service to compile source for you. This service would accept java source, compile it into a dalvik compiled class and return it as a binary.
This binary could then be added to a custom class loader as described here: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
Although you are not compiling on the phone, this would be compiled during runtime and, once inserted into your classloader, available for execution.
Technically possible, but not easy. If you look at Terminal IDE they package in all the tools to compile Android byte code from source on the device. You could take a similar approach by writing out the string to a file on disk, compiling it, and then use DexClassLoader to load the classes from the compiled JAR file or APK.

Does there exist a universal or "fat" class file for Java?

A fat binary is a binary that can be run on more than one architecture. Basically, it consists of a program compiled twice, once for each architecture, then written to the same file. Probably the best known example is Apple's "universal" binaries, allowing programs to be compiled for both Intel and Power PC architectures, and run from the same executable file.
This was never an issue for Java, since Java runs on the JVM, allowing it to be run from any JVM-supported computer. However, Android is now very popular, and Android's VM (Dalvik), is not compatible with the JVM. Is there some way of compiling the code twice, and creating a class file that can be executed by both the JVM and Dalvik? And if not, is this even possible?
Answer: Yes.
You can create a universal .jar file that contains both JVM-friendly .class files and an Android-friendly classes.dex file. The dx tool included in the Android SDK will emit such files if you use the --keep-classes command line option.
Note that although such .jar files can be consumed on JVMs and on Android, packaging code in this way is not very useful. Android applications are packaged as .apk files include an Android manifest XML file. They use Android-specific APIs like Activity that are not available on the JVM.
A universal .jar file would mostly be useful if you wanted to do runtime class loading of a library.

Categories

Resources