I am building a component-based application for Android. In short, I would like to load an APK file during runtime and execute code from it.
I used DexClassLoader
with success to load and instantiate some classes, the problem lies in reading the APK's resources.
I would like to create a custom Context
object that I could use to load these resources (with a LayoutInflater
instance for example), similarly to the createPackageContext()
, which does not work for my application since it is only looking into the installed packages.
Drag an APK or app bundle into the Editor window of Android Studio. Switch to the Project perspective in the Project window and then double-click the APK in the default build/output/apks/ directory. Select Build > Analyze APK in the menu bar and then select your APK or app bundle.
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
It is used to return the Context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity Context or “getApplicationContext” to pass the application Context.
InputStream in = getResources()
.openRawResource(R.raw.fileName);
then just read from this stream. This example would open res/raw/fileName.html for reading. I however do not understand why to load classes for execution from your own apk file in such a problematic way. If I misunderstand and you need to load from some other apk file, then - who prevents from opening that file for the input and reading from it? It is just a compresses ZIP archive. I think you could construct your implementation overriding the data access methods and your instance could contain the reference to the file from where to read the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With