Running native java on android

How to run Java app in Android

Is it possible to run java app (jar) in my android application? Because I need to create PDF, the problem is if I generate PDF in android, only can show with small image, if it contains large image in many pages, it will be error. So I think, I can generate PDF in java and then included to android app.

is it possible to run java app (jar) in my android application , no Android does not support Java. Sorry.

Java programs run everywhere where the JVM is installed. However android uses different VM — Dalvik. My guess would be you can do it only through emulator.

6 Answers 6

Concernig the mentiones app JBED: Well honestly, I could not find any credible source for this tools JBED, so I would really be very cautious (e.g. who is the developer?)

In the manifestfile (in Androidmanifest.xml, where every app has to state what rights it needs to run, see How to view AndroidManifest.xml from APK file?) there are many rights mentioned (what could be necessary, as the app wants to run as an emulator), so a java application might want to send an SMS, record audio, take pictures and place calls — so the emulator would need those rights as well.

But then the app also registers the «android.intent.action.BOOT_COMPLETED» event (i.e. autostart after boot) and this would go against every description of the tool.

Ah yeah and giveaway: The apk has a folder «certs» that has some (root-)certificates. But those are not the real certificates of the authorities, e.g. Versign. If one installs the app and by that those certificates the trust you might have in https-connections is lost because those who made the fake certificates can create own, false certificates that your phone would trust.

I assume (or am pretty sure) this is a spy tool, but I could be wrong. The (rare) testimonials that claim the tool ran perfectly will probably be the same person that posted the tool under a different name.

Источник

How to run Java programs directly on Android (without creating an APK)

A step by step instruction for compiling a Java program into an Android executable and using ADB to run it.

Читайте также:  Запуск скрипта php каждый час

When you want to create a system / commandline tool for Android, you have to write it in C(++)… or do you? TLDR; here’s the final proof of concept. Sticking with Java would have the benefit of avoiding all of the native ABI hassle and also being able to call into the Android runtime. So how do we do that?

A (not so) simple Hello World program

Let’s start with the Java program we want to run. In order to make it a bit more interesting (and because any useful program has dependencies), it won’t just print the obligatory “Hello World” message, but also use the Apache Commons CLI library to parse its commandline arguments:

package com.example; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class HelloWorld  public static void main(String[] args) throws ParseException  Option version = new Option("v", "Print version"); Option help = new Option("h", "Print help"); Options options = new Options(); options.addOption(help); options.addOption(version); for (Option opt : new DefaultParser().parse(options, args).getOptions())  if (opt.equals(version))  String os = System.getProperty("os.arch"); System.out.println("Hello World (" + os + ") v0.1"); > if (opt.equals(help))  new HelpFormatter().printHelp("Hello World", options); > > > >

Setting up the working directory

We will have to manually run several commandline tools in the next step, assuming the following final directory structure:

. ├── android-sdk-linux │ └── build-tools │ └── 23.0.2 │ └── dx ├── bin │ └── com │ └── example │ └── HelloWorld.class ├── lib │ └── commons-cli-1.3.1.jar ├── src │ └── com │ └── example │ └── HelloWorld.java ├── helloworld.jar └── helloworld.sh
  • Android SDK (either via Android Studio or the SDK Manager). NOTE: If you are an Android developer, you’ll have the Android SDK already installed. In that case, you don’t actually need to copy it to the working directory as long as you know the path to the dx tool.
  • Apache Commons CLI library v1.3.1

Afterwards copy&paste the HelloWorld code from above into the source folder. You might also find my semantic version parser class useful later on (not required here, though).

Compiling and dexing the Java class

Next step is to compile the java class (keep in mind that Android is stuck with Java 7 — bytecode for later versions won’t work). In case you are not used to doing this outside of an IDE, here’s the command:

javac -source 1.7 -target 1.7 -d bin -cp lib/commons-cli-1.3.1.jar src/com/example/HelloWorld.java

Make sure the program compiled properly:

java -cp lib/commons-cli-1.3.1.jar:bin com.example.HelloWorld -h usage: Hello world -h Print help -v Print version

Android cannot run Java class files directly. They have to be converted to Dalvik’s DEX format first (yes, even if you are using ART):

./android-sdk-linux/build-tools/23.0.2/dx --output=helloworld.jar --dex ./bin lib/commons-cli-1.3.1.jar

NOTE: Android Build Tools v28.0.2 and later contain a dx upgrade, called d8 . The d8 tool can process Java 8 class files. I’ll stick with dx for backwards compatibility reasons here.

Creating the startup shellscript

Android does not have a (normal) JRE, so JAR files cannot be started the same way as on a PC. You need a shellscript wrapper to do this. Copy&paste the one below to the workspace.

base=/data/local/tmp/helloworld export CLASSPATH=$base/helloworld.jar export ANDROID_DATA=$base mkdir -p $base/dalvik-cache exec app_process $base com.example.HelloWorld "$@"

NOTE: DEX files can also be started directly using the dalvikvm command, but going through app_process gives us a pre-warmed VM from the Zygote process (it is also the method employed by framework commands like pm and am ).

Installing and running the (non-) app

Time to push everything to the device:

adb shell mkdir -p /data/local/tmp/helloworld adb push helloworld.jar /data/local/tmp/helloworld adb push helloworld.sh /data/local/tmp/helloworld adb shell chmod 777 /data/local/tmp/helloworld/helloworld.sh

Moment of truth (fingers crossed):

adb shell /data/local/tmp/helloworld/helloworld.sh -v Hello World (armv7l) v0.1

NOTE: Since nothing was installed into the system, getting rid of the program is simply done by deleting the directory again.

It works, but how do I get a Context?!

Contexts represent an environment that is associated with an app (which we explicitly did not build) and are also device dependant. They can only be created by the ActivityThread class (a hidden system class that you cannot instantiate). If you want to interact with the Android runtime, you have to talk to the system services directly through their Binder interfaces. But that’s a topic for another article.

Follow up articles

Источник

Introduction

The following readings are recommended to better understand what we are talking here:

What’s Java Native Interface?

As the above web pages explained, the JNI is a way of writing Native code, say C/C++, inside routines, methods, classes and others of Java source code. This allows the reuse of already-coded libraries into the Virtual Machine of Java. While its usage is not recommended by Google (not completely true), it provides a proper way to handle stuff that otherwise would be impossible with such as a high-level language like Java.

How Android uses JNI?

Android has a set of tools that allows the usage of the JNI, it is called Native Development Kit (NDK) and has a wide variety of helpful tools, like toolchains, libraries, and so others. In Android Studio the Android NDK is easily integrated by downloading it and setting the path of it inside Studio.

Mobile Framework and the JNI

The framework we have created uses JNI in almost every part of the code. This was needed as the media client we had in mind was only possible using GStreamer. As you may already know, GStreamer is written in C and therefore the need of using the JNI.

JNI Set up for the Mobile Framework

The set up of the JNI may be a little tricky. It was difficult when Eclipse IDE was used, and it became even more difficult on Android Studio because of the Gradle files. The good news is that once it is configured, almost none further interaction is required.

In any kind of application, the build.gradle files must be modified when native code is intended to be built within the IDE. On the Mobile Framework we tried to separate the build of the native code and the Java one, nevertheless it was not possible, therefore we build everything within Android Studio.

Gradle File Modifications to allow JNI build

Once you have downloaded the NDK, determine the path of it and have at hand.

On the build.gradle of the application module, usually at $APPLICATION/app/build.gradle, you must set up the directory where to look at the sources coded in C. That is accomplished by writing the next line:

Once that is set, then a new ‘task’ must be written stating what kind of action it would do. For instance when trying to build a C code using the NDK something like this must be added:

task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') < def ndkDir = '/home/rjimenez/Android-NDK/android-ndk-r11b' commandLine "$ndkDir/ndk-build", 'NDK_PROJECT_PATH=build/intermediates/ndk', 'NDK_LIBS_OUT=src/main/jniLibs', 'APP_BUILD_SCRIPT=src/main/jni/Android.mk', 'NDK_APPLICATION_MK=src/main/jni/Application.mk' >

What this does is to define a task called ‘ndkBuild’ which is an execution, that will look at the path ‘ndkDir’ and defines a command called ‘commandLine’ which is the concatenation of all the variables the are next to it. To simplify it, the resulting ‘commandLine’ would be:

commandLine = "/home/rjimenez/Android-NDK/android-ndk-r11b/ndk-build", 'NDK_PROJECT_PATH=build/intermediates/ndk', 'NDK_LIBS_OUT=src/main/jniLibs', 'APP_BUILD_SCRIPT=src/main/jni/Android.mk', 'NDK_APPLICATION_MK=src/main/jni/Application.mk

All of those macros are needed to properly by the NDK build system to properly configure and build the native code.

The above command must be executed at some place during the Java compilation, therefore we add the next routine on the same file:

tasks.withType(JavaCompile) < compileTask ->compileTask.dependsOn ndkBuild >

This tells the Java Compilation that there is a task that must be executed, and that task depends on the proper definition of ‘ndkBuild’. By default, the C sources are searched under:

With all of these defined, then on the source code side the Android.mk and the Application.mk makefiles shall be created, this is on the jni folder described above.

Источник

Оцените статью