Run a jar file on Android from code - java

Is it possible to run/execute a .jar file on Android from code especially with Android's shift to ART instead of Dalvik?

The older versions of Android used to come with DVM (Dalvic Virtual Machine), and the newer versions are used to come with ART (Android Run Time).
Both of them execute the .dex (Dalvic Executable).
You cannot run .jar files directly in Android, but you can try some apps or VMs.

Related

Building OpenCV for Java on Windows x64 with Cmake

I am building OpenCV 420 for Java on Windows using cmake including the comtributed (extra) modules. I am successful in doing the cmake processes to generate the VS 2017 project. The VS project, set for x86 target, compiles and generates opencv-420.jar and opencv_420.dll. The opencv_420.dll is 37158kb in size. It contains all of the dependent libraries (dlls) created by the compile process. This jar and dll pair work correctly with the two files giving full access from Java to all opencv functions including the contributed ones. This shows that the varions dependant dlls are being merged into the single library opencv_420.dll.
Next I created a folder for x64 target, copied the x86 folder I used for the above x86 project and set the platform to x64. I run cmake successfully, generate the VS project and it compiles successfully as far as I can tell (there is a lot going I don't fully understand). This generates an x64 opencv-420.jar and an opencv_420.dll as expected. However, the opencv_420.dll is small at 2621kb and when I try to run the application it fails with can't find dependent libraries. I have determined this dll does not contain the the dependent libraries like the x86 dll does.
I have been over the cmake files and the VS project and can't seem to locate the difference between the x86 and x64 projects that causes the dependent dlls to not be merged into the opencv_420.dll.
I answered this one myself. Talk about not seeing the forest for the trees! There was a difference between the x86 and x64 cmake options that I just did not see until some reading I was doing today triggered the aha moment. I needed the Java Fat Jar option enabled in the x64 config as it was in the x86 config. All good now.

Android Studio build to jar

Is there a way to build Android app from Android studio to a jar file that could be run on Windows/Mac/Ubuntu? If not then why?
Despite both is Java syntax, Oracle Java and Android Java have little in common. Gradle can certainly package JAR, but the Android classes are unknown to the regular JRE. Better use the plain IntelliJ IDEA instead, which is rather suitable for desktop Java applications - else you could only run Android applications on an emulator or Anbox, which isn't too portable.
Tl;DR No you cannot run Android apps on Windows/Mac/Linux. Only emulators and Android devices can run them.
You should check the steps that Gradle performs during building before running it on the destination device.
To run on a PC (I mean Windows/Mac/Linux), you need a jar. And the closest thing that the build step comes to making jar is javac.
After javac, it performs steps to transform the java .class files into an Android readable format or the dex format and then packages the dexes into an .apk.
Android's execution environment understands dex and not .class (if it did then Android apps could have been run on all Java supported systems).
Let's say that you managed to stop after the javac and then managed to build a jar. Once you try to run it on anything other than an Android environment, you will get a ton of NoClassDefFoundErros because all Android specific classes are not there in the libraries in the usual JVMs (non-Android JVMs).
You should check this about the legal dispute between Oracle and Google. You will find that Android's Java implementation is very different that what OpenJDK offered at that time.

Cannot compile jar for windows

I have coded a really nice application for me and my friends. I compiled it and created an executable jar. It works perfectly on my mac. However, when I shared the program with my friend who owns a windows computer, he couldn't open the file. I had similar problems with my other programs so this it's not about my code.
Java by it's nature should be OS independent.
with that said there's some pretty good reasons why you can't run a .jar file.
The system doesn't have Java installed.
The Jar file was compiled using an incompatible version compared to the system Java installation.
The jar file uses native libraries (JNI).
The jar file isn't a native executable.

Can I develop Android apps without an IDE?

Is it possible to develop Android apps using only the Android SDK, without any IDE like Android Studio?
Yes, see the Google SDK documentation.
However, with the current toolchain and documentation it'll be an uphill struggle. I'm trying to do this too (for a dev who lives in Vim and Unix tools, Android Studio is needlessly slow and bloated).
The main issues I've found so far:
The SDK documentation gives instructions to create an Ant-driven project, but the SDK is now geared towards using Gradle. If you're working through the Google documentation in a linear fashion, you'll find subsequent lessons have you issue Gradle build instructions. For your Ant-built project.
There is very little documentation on how to actually create a Gradle-built project from the command line. The command line I use is:
android create project --target android-22 --name MyProjectName --path my_project/ --activity MyProject --package com.example.android.myproject --gradle --gradle-version 1.2.2
The --gradle-version actually refers to the Gradle Android plugin version, not the version of Gradle itself. Finding this out wasn't easy.
Even if you get it to create a project properly, it probably won't build without further manipulation. One of the generated files (project/build.gradle) has an invalid directive name (runProguard - I'm guessing it's now deprecated). That must be changed to minifyEnabled before the project will build. And using the Gradle plugin 1.2.2, the file project/gradle/wrapper/gradle-wrapper.properties has the Gradle distribution incorrectly listed as gradle-1.2.2-all.zip. This should be gradle-2.2.1-all.zip.
These are files generated by the SDK with errors.
The documentation is focussed on IDE-based development. Once you get past the initial few pages on creating and managing a project using the command line, it's very IDE-focussed.
Instructions on things like changing an app Theme are difficult or impossible to follow as they omit steps that the IDE performs for you.
In general, fully IDE-less development for Android (at least in Java, using the official SDK) is very painful. And my personal opinion is that IDE-based development is equally painful (slow, bloated, ugly on high-DPI screens under Linux and evidently full of magic that's a pain to replicate on the command line).
Edit: I should add that the above refers to using SDK tools v24.2, SDK Platform-tools v22, SDK Build-tools v22.01 and Android API 22 (5.1.1).
For android the basic debugging environments are:
ADB
DDMS
Java Debugger
You can try with them.
More details are here : http://developer.android.com/tools/debugging/debugging-projects-cmdline.html
Writing an Android app on Notepad is what I do on my Windows Laptop.
First you configure your laptop as follows:
Download development Kits: Download a Java Development Kit 1.6 for Windows and an Android Development Kit . Similarly try downloading older version of Android kit GUI version of Kit Manager so that you can download essentials. Using kit Manager download Android-23 platform build tools. Android Studio may have slower performance, so try to avoid it.
Configure Kits: Set Windows PATH variable by right clicking My Computer -> Properties -> Advanced System Settings or change the appropriate settings in your Control Panel. PATH should include the Java's compiler executable
file in Java directory, and the android.bat file of Android Kit.
Download Build-Tool: (recommended, but Ant can be used too.) These
programs configure the command-line tools for easy usage for our
convenience. I downloaded version 2.2.1 as it can use old
Java & old Android.
I've also written about these instructions on my GitHub Page.
For the development of Android apps one doesn't even need a PC, and not even the Android SDK. One can develop them completely on a mobile Android device, so an emulator isn't necessary either.
For that one needs to the app Termux on the device, and the toolchain script apkbuilder, which stitches together all APK building programs that come with the Termux packages aapt, apksigner, clang, cmake, d8, ecj and unzip, including the OpenJDK.

Why the JAR file compiled by Java SE, able to execute in Android

In Android OS, I am able to run several Java JAR files compiled under desktop environment. For instance, jtransforms
May I know why is it so? I thought the JVM from Oracle and Dalvik VM from Google uses two different byte code formats? Why the JAR file compiled by Java SE able to execute in Android?
I assume you are simply including the jar file in your android project? Then it works, as during the build process it is converted (the same is done for your own class files!) to dex format. This is done with the dex (dx) tool. Using ADT in eclipse it is automatically done for you. It's available for manual use in the android sdk of course too:
Under android-sdk-windows\platform-tools\dx.bat if on windows.
According to wikipedia, dx is used to convert compiled .class files into the .dex format.
1. Java SE - Java (Swing + AWT) = Android
2.In 2005, when Java was undergoing changes, Android creator didnt wanted to base Android
totally on Java. So they included JVM as the 1st compilation unit, thats the reason
JRuby , Jython, etc can be used to write Android apps. And 2nd Compilation unit as Dalvik
Compiler.
3. So its quite natural that a jar file compiled under desktop environment works fine on Android.

Categories