How to Execute the Dex File in Android with Command

How to execute the dex file in android with command?

Let's say you have a the following code in file HelloWorld.java:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

To run it on an android device:

javac HelloWorld.java
dx --dex --output=classes.dex HelloWorld.class
zip HelloWorld.zip classes.dex
adb push HelloWorld.zip /sdcard/

For GB or earlier, you should be able to simply do:

adb shell dalvikvm -cp /sdcard/HelloWorld.zip HelloWorld

For ICS+:

adb shell mkdir /sdcard/dalvik-cache
adb shell ANDROID_DATA=/sdcard dalvikvm -cp /sdcard/HelloWorld.zip HelloWorld

Can Android execute a classes.dex (or .jar converted from classes.dex) without a manifest, drawables, etc?

It is not possible to run an Android application like that. It must be packaged into an apk with an AndroidManifest.xml, etc.

Using dalvikvm with a dex/jar from the terminal like what you describe only works for a public void main(String[] args) style command-line java application.

How to run a DalvikVM application with more than 1 DEX files?

Setting aside bigger questions, the Dalvik classpath is a colon-separated list of filenames (e.g. /sdcard/foo1.jar:/sdcard/foo2.jar). Each jar/apk contains a single classes.dex. For an APK you'd work around this with multidex, but for command-line invocation you just need to put each classes.dex into its own jarfile.

How to merge two dex files from command line

Finally after days of research I found a way...

I juts used d8 as dx has been depreciated.

d8 dexfile1.dex dexfile2.dex

Output would be classes.dex

r8 does not create output dex file

The problem is that the only rule you are passing to R8 is -dontwarn java.lang.Object. There is no single -keep rule, which means that the output will be empty as no entry points are kept.

What is missing is two set of keep rules:

  1. General keep rules for all Android apps
  2. Specific rules for the concrete app with all entry points (reflective access from the platform for e.g. view inflation), which can be generated with aapt2.

For the first set of rules Android Studio (or more precisely AGP) bundles this rules file, which is passed to all builds as a result of getDefaultProguardFile('proguard-android-optimize.txt').

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with
# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and
# will be ignored by new version of the Android plugin for Gradle.

# Optimizations: If you don't want to optimize, use the proguard-android.txt configuration file
# instead of this one, which turns off the optimization flags.
# Adding optimization introduces certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik. The following flags turn off various optimizations
# known to have issues, but the list may not be complete or up to date. (The "arithmetic"
# optimization can be used if you are only targeting Android 2.0 or later.) Make sure you test
# thoroughly if you go this route.
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Preserve some attributes that may be required for reflection.
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class com.google.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.google.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames,includedescriptorclasses class * {
native <methods>;
}

# Keep setters in Views so that animations can still work.
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick.
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
public static final ** CREATOR;
}

# Preserve annotated Javascript interface methods.
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}

# The support libraries contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**

# This class is deprecated, but remains for backward compatibility.
-dontwarn android.util.FloatMath

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep class androidx.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}
-keep @androidx.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
@androidx.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
@androidx.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}

-keepclasseswithmembers class * {
@androidx.annotation.Keep <init>(...);
}

# These classes are duplicated between android.jar and org.apache.http.legacy.jar.
-dontnote org.apache.http.**
-dontnote android.net.http.**

# These classes are duplicated between android.jar and core-lambda-stubs.jar.
-dontnote java.lang.invoke.**

For the second part use aapt2 link with the --proguard option. That will generate additional required rules like:

-keep class com.example.app.MainActivity { <init>(); }

Both sets of rules will have to be passed to R8 (it can take several --pg-conf options) together with additional application specific rules.

When using Android Studio (again more precisely AGP) this is handled automatically. For reference these rules can be inspected in app/build/intermediates/proguard-files. See Shrink, obfuscate, and optimize your app for more details.

What are .dex files in Android?

About the .dex File :

One of the most remarkable features of the Dalvik Virtual Machine (the workhorse under the Android system) is that it does not use Java bytecode. Instead, a homegrown format called DEX was introduced and not even the bytecode instructions are the same as Java bytecode instructions.

Compiled Android application code file.

Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single .apk file on the device. .dex files can be created by automatically translating compiled applications written in the Java programming language.

Dex file format:

 1. File Header
2. String Table
3. Class List
4. Field Table
5. Method Table
6. Class Definition Table
7. Field List
8. Method List
9. Code Header
10. Local Variable List

Android has documentation on the Dalvik Executable Format (.dex files). You can find out more over at the official docs: Dex File Format

.dex files are similar to java class files, but they were run under the Dalkvik Virtual Machine (DVM) on older Android versions, and compiled at install time on the device to native code with ART on newer Android versions.

You can decompile .dex using the dexdump tool which is provided in android-sdk.

There are also some Reverse Engineering Techniques to make a jar file or java class file from a .dex file.



Related Topics



Leave a reply



Submit