Editing an APK file's resources from Android - android

I want to edit the resources of an APK file from WITHIN Android. I know how to do it with ApkTool in windows/linux, but I'm having problems doing it in an Android app itself.
In particular, I need to add a few files to the resouces, edit the AndroidManifest.xml and put the files back in the APK.
I know APKs are simply zip files, but the resources and manifest inside them are "compiled" in binary format so they cannot be edited directly, so I need some kind of library or tool for Android that can decompile the resources and then put them back in the APK.
I know this can be done, since apps like APK Editor do it (and without needing root!) but I can't find any documentation on how to do it myself.

I used this library for parsing an APK.

Related

Include files with different prefixes in apk

I have a project with native libraries that I want to use, files with this format: lib<name>.so do get included into apk. But files with <name>.so format does not.
Is there a way to include the later type into apk in lib directory?
If not, is there a way to include the files into a directory inside apk, where I can load it from my native code?
The short answer is "no". The native binaries will only be packed into APK, and extracted to executable files upon installation, if their names follow the lib….so pattern.
Note that these libraries will be extracted to files according to the ABI of the target system. The installer does not check the actual properties of the file. The decision is based on the name of the folder under lib in the APK structure.
If you add the attribute extractNativeLibs=false to the application tag in AndroidManifest.xml of your APK, the installer (on Android Nougat and higher) will not extract the native libraries.
You can trick the system and have files that don't follow the above rule to the lib folder of APK, but there is very little sense in it, because they will never be extracted by the loader (it may also extract file gdbserver if the file is there).
The common practice is to put the arbitrary files in the assets folder of your APK, and extract them programmatically when the app runs for the first time after install. You cannot extract these files to the secured location where the usual native libraries go. You should not extract the native libraries to sdcard (e.g. getExternalFilesDir()), because the system may not allow execution of the files there, regardless of the execute access flag on the file. Make sure that you use the correct ABI flavour.
You can peek at the source code of Nougat native lib loader that can load native libraries from the APK without extraction, and use it to load your custom libraries directly from the assets folder of your APK.

How to compile AndroidManifest.xml programmatically on Android?

I want to know how to implement a apk editor which can edit app name and version.
A way I can figure out takes several steps:
unzip apk file
modify app name and version in AndroidManifest.xml
replace the AndroidManifest.xml with the modified one
compress them into a new apk file
sign and align it
I can do all the steps above except Step 2. The AndroidManifest.xml file is compiled and cannot be parsed as normal xml file. I know apktool can compile and decompile it but I cannot find a easy way to run apktool on Android after several hours searching. I also cannot find any open-source apk editors(what I need is not only a apk editor, I want to develop another app based on it).
Finally I find some projects for reading and writing AXML(Android XML) and resources.arsc:
ArscEditor
axml
So I can wirte an apk builder with them. For more information, see my repo Auto.js-ApkBuilder.

Separate .so libraries from .apk

I have an enterprise app which I am deploying manually (no Google Play) which uses a number of .so libraries for mapping (ArcGIS). However, the .so files (arm, armv7a, x86) in the libs folder blow the .apk size out from 3mb to 21mb. I dont particularly want to remove one of the .so files (removing support for that architecture), or mess around with one .apk per architecture.
Can anyone think of a way I can update my app without including the .so files in each update .apk?
Yes, you can have the Java portion of your app manually download the appropriate .so files into your app's internal storage folder and mark them executable.
You will then have to load them with System.load() and the full pathname of the .so file, rather than System.loadLibrary() and the trimmed library name.
The biggest issue here is that you are now responsible for matching the ABI's yourself, and more importantly, providing your own protection against being tricked into installing a modified or imposter library which might do something nefarious in the name of your app and using it's permissions.
Of course you have to make sure not to try to call any of the native methods before you have installed them.
You could also consider delivering the .so files as binary assets each in its own skeleton .apk having a shared user id (and matching certificate) as your main .apk
Or you could simply make platform-specific .apk's for each target, containing only one .so, and have your distribution system pick the right ones (though that doesn't help with the upgrade problem).

Replace .so file in android apk

is it possible to replace .so file in android app without source code?
I am creating app which use some .so files under LGPL license v2.1 http://www.gnu.org/licenses/lgpl-2.1.html
License says: "...you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it..."
Is it possible to replace .so file and recompile app without source code e.g. in existing apk? I can't give user source codes. Or some option to give user .class files so he can create apk?
Is it even possible and if so, what are options?
Thank you!
Yes, you can replace a .so file in an apk. Just use 7zip to open the apk replace the file and you are done.
About gpl: you should have 2 libraries, one so for your code and one for the code you borrowed, this way the user can replace the so for the library, but your code cannot be changed. You can also put a check for the libs version and just exit if it was changed.

Is there a way to view the contents of a manifest file from an APK?

I saw here that someone listed the contents of the manifest of the official Facebook app's APK. Is there a way to decompile that, or is that info available elsewhere?
An APK file is just a JAR - change the extension to .JAR, and use a decompression tool to decompress it.
If you're using windows, you could just use WinRAR - that should decompress JARs.
In addition to that, a JAR is just a regular ZIP file - so you could technically just change the APK extension to .ZIP instead, and open it up using pretty much any decompression tool (as mentioned by Peter).
Here's some more info on JARs:
http://en.wikipedia.org/wiki/JAR_file

Categories

Resources