[Android Software Development] brok85:
How to add a new class to app decompiled with apktool?

Hi,

I am new to apktool, so forgive my inexperience. I need to learn how to decompile an apk, insert some code, build it, and install it.

So far, I have achieved the goal of decompile, build the decompiled code (and signed), and install the generated apk on the device. The app works correctly in this way.

What I need to do now, is understand how to add my own code into it. In particular, I need to include in the onCreate method of the main activity (right before closing the method) the following code:

new MyClass(parameters);

and then create a new java class in the same folder of the main activity that implements MyClass. To do so, my first guess was to first create a new empty app with Android Studio, build it, and decode the apk to get the smali version of the source code. Then, I have added MyClass to the empty app in Android Studio, built it, and decoded the newly generated apk, which now includes the extra code and files introduced by MyClass.

Then, I have copied the extra code and files related to MyClass and pasted it in the calculator smali source code inside the onCreate method of its main activity smali file (right before return-void.end method of onCreate) Accordingly, I have changed all the paths from the empty app to reflect the path of the calculator app, i.e., I have changed all the

Lcom/example/brok85/emptyapp/MyClass

to

Lcom/google/android/calculator/app/MyClass

in all the files, including the MyClass smali files. Then, I have rebuilt the calculator app, signed it and installed it on the device (wear OS emulator). However, the app crashes when I launch it. Looking at the logcat, I have the following exception:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/calculator/app/MyClass;
at com.google.android.calculator.app.CalculatorActivity.onCreate(SourceFile:139)

Why do I get this error? Why is still indicating Lcom/google/android/calculator/app/MyClass instead of com.google.android.calculator.app.MyClass?

Any help would be really appreciated.

Please, find below other useful information, including the complete stack trace of the exception.

### Information

1. **Apktool Version (`apktool -version`)** - 2.3.1

2. **Operating System (Mac, Linux, Windows)** - Mac

3. **APK From? (Playstore, ROM, Other)** - Other

### Stacktrace/Logcat

/com.google.android.calculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.calculator, PID: 5129
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/calculator/app/MyClass;
at com.google.android.calculator.app.CalculatorActivity.onCreate(SourceFile:139)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.calculator.app.MyClass" on path: DexPathList[[zip file "/system/framework/com.google.android.wearable.jar", zip file "/data/app/com.google.android.calculator-TMlvBd9OU5fNf9_vXXnIHw==/base.apk"],nativeLibraryDirectories=[/data/app/com.google.android.calculator-TMlvBd9OU5fNf9_vXXnIHw==/lib/x86, /system/lib, /system/vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.google.android.calculator.app.CalculatorActivity.onCreate(SourceFile:139)*
at android.app.Activity.performCreate(Activity.java:6975)*
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)*
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)*
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)*
at android.app.ActivityThread.-wrap11(Unknown Source:0)*
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)*
at android.os.Handler.dispatchMessage(Handler.java:105)*
at android.os.Looper.loop(Looper.java:164)*
at android.app.ActivityThread.main(ActivityThread.java:6541)*
at java.lang.reflect.Method.invoke(Native Method)*
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)*
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)*

### Frameworks

The apk is the calculator app of Android. I am using it to learn how to decompile, add code, and recompile.

### Questions to ask before submission

1. Have you tried `apktool d`, `apktool b` without changing anything? yes

2. If you are trying to install a modified apk, did you resign it? yes

3. Are you using the latest apktool version? yes