Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context load from an APK file in Android

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.

like image 794
Donatien Avatar asked Jun 09 '11 12:06

Donatien


People also ask

How do I analyze an APK file?

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.

How do I find application context?

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.

How do you pass context?

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.


1 Answers

            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.

like image 117
Audrius Meškauskas Avatar answered Sep 22 '22 23:09

Audrius Meškauskas