Today I’d like to share with you my findings about how an existing .apk file can be modified.

An .apk file represents the mobile application as it is installed on a mobile device, like smartphone, tablet, wearable, etc.

Such an .apk file is a simple archive that can be opened with any packager like e.g. WinRAR

So you can easily open it and view the files – although viewing most of the files won’t make you happy, because you’ll realize that they’re compiled, in binary format, etc

… but this is a different story.

Anyways, you can open the archive and then modify any resource file and save the modification in the archive.

But if you afterwards try to install the .apk on the smartphone (or tablet or similar), you’ll get an error.

The following screenshot displays the error when installing the modified sample application myApp.apkon an Android device:

The reason is that after the modification, the checksum and the signature are not valid anymore.

Thus, simply changing an .apk file is not possible.

However, there’s still the valid use case to modify or replace files inside an existing .apk.

For example:

– files which are placed in the assets folder

– property files containing configuration data

– images which can be replaced

– styling information resources

and similar.

My personal use case was:

I had created an Android application using SAP Netweaver Gateway Productivity Accelerator.

I had to deliver the application to my users as .apk file.

But there was the requirement that they wanted to modify the ready application (change configuration data).

So I had to figure out how to achieve that: modify the app without having access to the source code.

Below, I’m sharing the required steps with you.

The description is based on the following software and versions:

Android current API 19

Java 7

Windows 7

If you aren’t familiar with Android, but wish to be, you might want to check the documents [1] and [2]

All prerequisites for understanding this blog are explained there.

Note:

In order to execute the commands described below, you need to have Java on your PATH variable of your Windows system (see [1] for an explanation).

Overview

There are 3 steps that need to be followed in order to modify an existing .apk file:

1. Do the actual desired modifications inside the .apk file

2. Sign the .apk

3. Install the .apk on the device

1.  Change the resource in the .apk

Open the .apk file with WinRAR (if that doesn’t work, rename the file extension .apk to .zip)

Change the resource in the archive as desired (packager tools allow to change files without the need to extract the archive)

Once you’re done with your changes, you have to take care about the signature files that are part of the .apk:

Inside the archive, go to folder META-INF

Delete the existing *.RSA and *.SF files

The following screenshot displays the content of the META-INF folder in an .apk file:

Now the archive can be closed.

In case you had changed the file extension before, you now have to change it back to .apk

2. Sign the .apk

Android doesn’t allow to install an application (apk) that isn’t signed.

When developing an app in Eclipse, the ADT (“Android Developer Tools”, the extension to Eclipse that supports development for Android) takes care of signing the app with a default certificate, before installing in on the device.

That’s comfortable, but with the following description, everybody is able to sign an application.

Signing the .apk is done in 2 steps:

a) create the certificate

b) sign the .apk with the created certificate

Both steps are done with commands on the command line

a) Generate a certificate

If you’re working in a Java environment, you have the JDK on your file system.

The JDK comes with a tool to manage certificates: the keytool.

You can find it in the …/bin folder of your JDK installation.

Example:

On my machine it is here:

Now you can generate a certificate using below command.

However, before executing it, please check the notes below, in order to adapt the parameters

keytool.exe -genkey -v -keystore <myKeystore> -alias <myAlias> -sigalg MD5withRSA -keyalg RSA -keysize 2048 -validity 1000

Please note that you have to adapt some of the parameters of the above command to your personal needs:

keystore <myKeystore>

Here, you can provide an arbitrary name for your keystore.

The name that you provide here will be the name of the keystore-file that will be created.

The file will be created in the current directory.

(I haven’t tried it, but probably you can enter the name of an existing keystore file, in order to store the new certificate there)

alias <myAlias>

Here as well, you can provide an arbitrary name for the alias.

It is meant for you to recognize it.

The alias is the human readable name of the certificate which will be created and stored in the keystore.

validity 1000

This is the number of desired days.

You can enter any number you wish.

I think it should be high enough in order to avoid trouble with expiration.

Note that the parameters sigalg and keyalg are required by JDK 7, so it shouldn’t be necessary to add them if you’re using JDK 6

Example:

keytool.exe -genkey -v -keystore mykeystore -alias myAlias -sigalg MD5withRSA -keyalg RSA -keysize 2048 -validity 10000

When executing the command, you’ll get several prompts on the command line, asking for password, username, organization, city, etc

You can enter any arbitrary data here, you only have to make sure to remember the password.

After you’ve executed the command, you’ll see the generated keystore file on your file system in the current directory (from where you’ve executed the command)

Now you can proceed with signing the .apk using the newly created certificate.

b) Sign the apk

Before signing the .apk file, you have to make sure that there are no certificates available in the .apk.

This is described in step 1 above.

For signing an archive, we use the jarsigner tool, which is provided with JDK, and which can be found in the same location like the keytool.

The following  command is used for signing an apk.

jarsigner -verbose  -sigalg MD5withRSA -digestalg SHA1 -keystore <keystoreName> <appName> <alias>

Please note that you have to adapt some of the parameters of the above command to fit your personal needs:

keystore <keystoreName>

Here you have to enter the name that you have given in the previous step a)

In order to keep the command line short, I recommend to temporarily copy the keystore file to the same location where you’re executing the command.

<appName>

Here you have to enter the name of the apk file which you want to sign

In order to keep the command line short, I recommend to temporarily copy the .apk file to the same location where you’re executing the command.

<alias>

Here you have to enter the name of the alias that you’ve provided when generating the certificate

Note that the parameters sigalg and digestalg are required by JDK 7, so it shouldn’t be necessary to add them if you’re using JDK 6

Example:

jarsigner -verbose  -sigalg MD5withRSA -digestalg SHA1 -keystore mykeystore myApp.apk myAlias

After you’ve executed the command, you can check the result inside the .apk file:

Open the archive, go to the folder …/META-INF and check if the files CERT.RSA and CERT.SF have been created.

3. Install the apk on the device

Now that the .apk file is signed, you can install it on your device.

BTW: This procedure is also called side-load.

For Android applications the installation is done on the command line, using the adb command.

adb stands for Android Debug Bridge

adb.exe is a piece of software that connects the PC with the Android device.

It allows access to the device, allows to trigger operations, transfer files, etc.

In order to install the .apk on the device, you have to connect the device to your PC via USB cable,

then execute following command

adb install <appName>

In order to keep the command line short, you can temporarily copy the apk file to the same location where you’re executing the command.

Example:

adb install myApp.apk

The result should be “success” message on command prompt.

If not, any of the previous steps may have failed.

That’s it.

You can find the application in the apps folder of your smartphone.

This procedure worked for me on WIN7 and JDK 7.

It wasn’t required to rebuild the app, nor to generate new checksum or similar.

Links

Please refer to the following documents for lot of information for beginners.

They also contain lots of additional links for further reading.

[1] Getting started with GWPA: Prerequisites: http://scn.sap.com/docs/DOC-52235

[2] Getting started with GWPA: Android Preparation: http://scn.sap.com/docs/DOC-52371

The official docu can be found here: http://developer.android.com/tools/publishing/app-signing.html

转自:https://blogs.sap.com/2014/05/21/how-to-modify-an-apk-file/

How to modify a compiled Android application (.apk file)的更多相关文章

  1. Android Application Fundamentals——Android应用程序基础知识

    Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvid ...

  2. Android 验证APK是否已经签名或是否是Debug签名

    https://source.android.google.cn/ http://www.android-doc.com/tools/publishing/app-signing.html Signi ...

  3. repackage android application

    decompile the application file apktool d -o dianping/ dianping.apk modify the resources / smali asse ...

  4. My First Android Application Project 第一个安卓应用

    一.前言: 安卓(Android):是一种基于Linux的自由及开放源代码的操作系统,主要用在移动设备上,如手机.平板电脑.其他的设备也有使用安卓操作系统,比如:电视机,游戏机.数码相机等等. 二.具 ...

  5. 将HTML5封装成android应用APK文件的几种方法

    越来越多的开发者热衷于使用html5+JavaScript开发移动Web App.不过,HTML5 Web APP的出现能否在未来取代移动应用,就目前来说,还是个未知数.一方面,用户在使用习惯上,不喜 ...

  6. 将HTML5封装成android应用APK文件的几种方法(转载)

    越来越多的开发者热衷于使用html5+JavaScript开发移动Web App.不过,HTML5 Web APP的出现能否在未来取代移动应用,就目前来说,还是个未知数.一方面,用户在使用习惯上,不喜 ...

  7. Android实现apk文件下载并自动安装

    //下载apk程序代码 protected File downLoadFile(String httpUrl) { // TODO Auto-generated method stub final S ...

  8. 将HTML5封装成android应用APK文件的几种方法(转)

    作为下一代的网页语言,HTML5拥有很多让人期待已久的新特性.HTML5的优势之一在于能够实现跨平台游戏编码移植,现在已经有很多公司在移动 设备上使用HTML5技术.随着HTML5跨平台支持的不断增强 ...

  9. Android文件Apk下载变ZIP压缩包解决方案

    [root@ conf]# pwd /alidata/server/nginx/conf [root@ conf]# vi mime.types application/vnd.android.pac ...

随机推荐

  1. 实现Hbase的分页

    作者:R星月 出处:http://www.cnblogs.com/rxingyue 欢迎转载,也请保留这段声明.谢谢! 做一个项目中由于数据量比较大,并且需要定时增量分析,做了hbase的分页.项目中 ...

  2. CDQ分治入门

    前言 \(CDQ\)分治是一个神奇的算法. 它有着广泛的用途,甚至在某些题目中还能取代\(KD-Tree\).树套树等恶心的数据结构成为正解,而且常数还小得多. 不过它也有一定的缺点,如必须离线操作, ...

  3. python换行

    python中如果一行代码太长,看着不方便时,怎么办? 只需要在需要换行的地方添加上符号 \ 就行了.

  4. jQuery支持链式编程,一句话实现左侧table页+常用筛选器总结

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. JavaScript之map与parseInt的陷阱

    问题来源 ​ 这个问题的来源是学习廖雪峰老师JS教程.问题如下:小明希望利用map()把字符串变成整数,他写的代码很简洁: 'use strict'; var arr = ['1', '2', '3' ...

  6. java设计模式1--单例模式

    1:单例模式简介 单例模式是一种常用的软件设计模式,它确保某个类只有一个实例,而且自行实例化并向整个系统提供唯一的实例.总而言之就是在系统中只会存在一个对象,其中的数据是共享的 特点: 单例类只能有一 ...

  7. TP5 发送邮件代码

    发送邮箱邮件方法 /** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param strin ...

  8. pip3 的安装 同时安装lxml和pygame

    ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...

  9. Python基础:输入与输出(I/O)

    来做一个NLP任务 步骤为: 1.读取文件: 2.去除所有标点符号和换行符,并把所有大写变成小写: 3.合并相同的词,统计每个词出现的频率,并按照词频从大到小排序: 4.将结果按行输出到文件 out. ...

  10. Python 列表元素分组,比如 [1,2,3,...20]变成 [[1,2,3],[4,5,6]....](列表生成式解决)

    # 生成一个1到20的列表 a=[x for x in range(1,21)] # 把a列表切片,并赋值给b列表,x为0到20且步长为3的列表,在这里具体为[0,3,6,9,12,15,18] # ...