Dexer 1.0.0

dotnet add package Dexer --version 1.0.0                
NuGet\Install-Package Dexer -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dexer" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dexer --version 1.0.0                
#r "nuget: Dexer, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dexer as a Cake Addin
#addin nuget:?package=Dexer&version=1.0.0

// Install Dexer as a Cake Tool
#tool nuget:?package=Dexer&version=1.0.0                

Dexer

Build status NuGet

Dexer is an open source framework, written in C#, that reads and writes .DEX files (Dalvik Executable Format) used by the Android Open Source Project.

Usage:

Let's work on the following Android application:

package dexer.poc;
 
import android.app.Activity;
import android.os.Bundle;
 
public class MainActivity extends Activity {
 
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
       
                int a = 4;
                int b = 5;
                int result = a*b;
       
                setTitle("This demo rocks: " + result);
        }
}

image

And here is the code of the main method using the Dexer object model: image

Now let’s go back to C# to play a little with this application by changing a string constant and an opcode (adding instead of multiplying):

using System;
using Dexer.Core;
using Dexer.Instructions;
 
namespace Dexer.Debug
{
    class Program
    {
        static void Main(string[] args)
        {
            Dex dex = Dex.Load("classes.dex");
            MethodDefinition method = dex.GetClass("dexer.poc.MainActivity").GetMethod("onCreate");
 
            method.Body.Instructions[5].OpCode = OpCodes.Add_int;
            method.Body.Instructions[7].Operand = "Dexer rocks! ";
 
            dex.Write("output.dex");
            Console.ReadLine();
        }
    }
}

image

Here is the result:

image

Now let’s call a method to change the title color:

using System;
using Dexer.Core;
using Dexer.Instructions;
 
namespace Dexer.Debug
{
    class Program
    {
        static void Main(string[] args)
        {
            Dex dex = Dex.Load("classes.dex");
            MethodDefinition method = dex.GetClass("dexer.poc.MainActivity").GetMethod("onCreate");
 
            method.Body.Instructions[5].OpCode = OpCodes.Add_int;
            method.Body.Instructions[7].Operand = "Dexer rocks! ";
 
            int color; unchecked { color = (int)0xFFFF00FF; }
 
            // Declare a new method reference with prototype
            Prototype prototype = new Prototype(PrimitiveType.Void, new Parameter(PrimitiveType.Int));
            MethodReference setTitleColor = dex.Import(new MethodReference(method.Owner, "setTitleColor", prototype));
 
            // Load the color in a register (n°1) then invoke the method (register n°5 is 'this' in our case)
            var regs = method.Body.Registers;
            Instruction iconst = new Instruction(OpCodes.Const, color, regs[1]);
            method.Body.Instructions.Insert(14, iconst);
 
            Instruction iinvoke = new Instruction(OpCodes.Invoke_virtual, setTitleColor, regs[5], regs[1]);
            method.Body.Instructions.Insert(15, iinvoke);
 
            dex.Write("output.dex");
            Console.ReadLine();
        }
    }
}

image

Here is the result:

image

As you can see, altering DEX files is quite easy with Dexer. In order to rebuild APK packages, I’ve used ApkTool and JarSigner (with the default debug key generated by the Android SDK).

apktool d -s -f DexerPOC.apk output
I: Copying raw classes.dex file...
I: Loading resource table...
I: Decoding resources...
I: Copying assets and libs...

apktool b output DexerPOC.new.apk
I: Copying classes.dex file...
I: Checking whether resources has changed...
I: Building resources...
I: Building apk file...

jarsigner -keystore .\.android\debug.keystore -storepass android -keypass android DexerPOC.new.apk androiddebugkey

adb install DexerPOC.new.apk
586 KB/s (12609 bytes in 0.021s)
        pkg: /data/local/tmp/DexerPOC.new.apk
Success.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 254 11/23/2023
0.8.0 414 9/28/2022
0.7.0 879 12/17/2019