When I analyze an APK, I see classes.dex and it supposed to contain my Java code and it does, why it also contains Java standard Library (java.io.) and the SDK platform (android.app.) which I used to compile the project.
Can anybody explain why are they included?
Basically, when we are writing any program we need to import the classes which are needed. So, likewise to run the program, it needs those code. The java.io or android.support are nothing but some codes written.
Related
Im new to app development and was wondering if it would be possible to include another jdk. For example java 17 and run a jar that needs to be run in this exact version? I think termux has an api that you may use for these purposes but is there another possibility? These jars are only console based and without any gui.
I first tried to use the jar as a android library inside the app, which did not work since it was compiled in java version 17. I tried decompiling the jar and recompiling it with the version I was using. The problem is that all the libraries used are on other versions than I need them to be and so I could not recompile it. I also thought about writing the application in c# but I am not used to it at all, which would throw many new problems that I would be willing to take if it would be a possibility to work out my project.
Q: Is loading the code into your Android application an option?
A: No.
Android loads code from ".dex" files not ".class" files.
The ".class" files would need to be translated using dx.
The Android dx command doesn't understand Java 17 ".class" file format.
Also the code in the JAR is likely to depend on classes in the Java SE class library that the Android doesn't provide.
Q: What about running it in a separate Android VM?
A: No.
An Android VM requires ".dex" files; see above.
Also, the Java SE class library issue; see above.
Q: What about launching an OpenJDK or Oracle Java 17 JVM on the Android device to run the JAR?
A: In theory Yes, but in practice No. As far as I am aware, there is no port of OpenJDK Java SE to the Android OS platform.
Q: What about using Termux?
A: OK ... that might work. See Is it possible to install the JDK on an android device?.
I have no experience with this, and don't know what problems you may run into doing this. But I suspect that you wouldn't be able to distribute something that relies on Termux via the Google Playstore.
Another option is to download the source code1 for the application and try to build it in your Android dev environment
If the code uses Java classes / packages / libraries that are not available for Android, recode the relevant parts of the application to use Android equivalents instead.
Ditto if the code uses Java language features that are not yet supported in Android Java.
It probably won't be easy. It may turn out to be impractical.
1 - You said in a comment that the code your are trying to use is "open source". So the "download source and build it" option is available to you. I'm puzzled why you tried to decompile and recompile it instead ...
I'm using a library (https://github.com/PhilJay/MPAndroidChart) for plotting data in an android app. When app-release.apk is created by the program it is ready to be installed on the tablet I use for testing.
What is puzzling to me is how the parts of the library, which i use, follow with the release. In other scenarios, for example in Visual Studio and c# - program being installed on Window machine, libraries require dll files to be installed and registered on each targeted machine. In my scenario the library is written specifically for Android, but if I somehow managed to include a c++ or a c# library in my Android app using tools like libstdc++ or MONO, would it work the same way when it comes down to app-realease.apk?
Are all classes in a library included in the app-release.apk or just the parts that I use?
Thanks in advance and please let me know if the question is unclear before downvoting it!
Normally, when you build your APK, all the libs you have imported (jars) are included and transformed to dex files, as the rest of your code. So, yes all the classes are included, even if you don't use them.
You can use Proguard to remove them from the APK. Look at this post :
Use Proguard for stripping unused Support lib classes
I have a task of fixing a program written in java, since it was crashing on start. I discovered that the problem was related to a discontinued class in the java library no longer available in 1.8.
Eventually I will find an alternative to the library but for right now I need to ship the program with a reference to an old java version NOT installed on the clients computer. What would be the most appropriate way of doing so?
Obviously you want minimal impact on the rest of the code base where you don't want to have to go modifying the imports that may be numerous in your code. So, if the Java version is not installed on the clients computer you'll need to include the class in the packaging of your application.
I would suggest that you create the package structure of wherever the missing class file currently resides such as java.io.x.y.z so you wont have to modify any of the import statements
e.g. import java.io.x.y.z.ClassName;
Then test test test to ensure it functions as you expect.
The JRE may be distributed with programs. So repackage your program to bundle a JRE and use the java binary from there to start your program.
If you use a wrapper script, this would be where to fix it.
You can use the new javapackager tool and specify the runtime option to include (bundle) a specific JRE.
I am new to java and I need to manipulate java bytecode for some purposes (see this). Java bytecode manipulation need following imports:
org.objectweb.asm
java.lang.instrument
I resolved org.objectweb.asm by downloading ASM package from asm website and related imports have been resolved.
I don't know how to resolve java.lang.instrument, My default ADT bundle hasn't it:
How do I resolve this import? Should I download any library? from where?
any help would be appreciated.
Thanks.
The java.lang.instrument package isn't available for Android. Just like AWT and Swing. Have a look at this question:
Android & Dalvik - Get the size of an object
But it makes sense. Android apps are written at the source level in Java, but they don't run on the JVM -- they run on the dalvik VM. There's no contract there that says they have to support the standard Java library.
Sorry :(
You're out of luck – find a way that does not rely on java.lang.instrument. java.lang.instrument is part of Java SE, but is not available on Android because of fundamental limitations of Dalvik.
The java.lang.instrument package was removed from dalvik core library, because this package makes a fundamental assumption that the execution format being used by the VM is a .class file. .class files do not appear on Android at all.
https://groups.google.com/forum/#!topic/android-developers/MR4W2roQ3Xw
Javassist is another tools to manipulate java bytecode. There is already someone who tried to use javassist in android. You might want to try it. As far as I know, bytecode manipulation on android runtime isn't possible, except in instrumentation (usually for testing). Manipulation on compile time is a different story, because java .class file generated first before converted to Dalvik bytecode. So if you modify .class file before being dexed, the dexed classes will be the modified one.
This article also worth reading, because it noted of ASMDEX which claim can manipulate DEX bytecode.
The java.lang.instrument package was removed. So you can't perform bytecode manipulation at runtime. However you can perform bytecode manipulation at build time with javaassist or ASM.
This sample project performs bytecode manipulation. It's usage is discussed here.
Could someone please tell me how to implement the Asprise OCR library into my java program.
I'm using Eclipse and I know how to import the .jar but as soon as I want to run my program it gives me errors about a .dll file that's missing.
I fixed it, I just had to copy the .dll that came with the .jar to System32.
If you don't want to add them to System 32 just add them to your workspace and use
'System.load("dll_name_without_.dll_at_the_end");'
Just FYI, Asprise OCR library for Java automatically handles the native library files since version 5.0.
Java offers two methods to load libraries and a good explanation is here
Difference between System.load() and System.loadLibrary in Java