準(zhǔn)備工作:
一、拆包。(Mac環(huán)境)
1.下載apktool,我用的是apktool_2.1.1.jar。
2.需要拆包的apk。
用到的命令:
解包:
java -jar apktool_2.1.1.jar d app-test.apk
打包:
java -jar apktool_2.1.1.jar b app-test/
簽名:
jarsigner -verbose -keystore moonlighting.jks(證書) -storepass [密鑰] -keypass [密鑰] -signedjar Thinkdrive_signed.apk(簽名完后的apk) app-test/dist/app-debug.apk(上一步中產(chǎn)生的apk) moonlighting(證書名)
操作步驟:
1.打開Terminal,進(jìn)入apktool的工作路徑:

執(zhí)行命令:java -jar apktool_2.1.1.jar d app-test.apk

操作成功后會(huì)產(chǎn)生一個(gè)與apk文件名相同的一個(gè)文件夾,結(jié)構(gòu)如下:

二、修改。
從上圖文件結(jié)構(gòu)可以看到。所有的xml文件都是可以在這里找到的,資源文件在名為res的文件下,而且沒有變化。而之前的java代碼在這里變成了.smali文件在名為smali的文件夾中。
1.(簡(jiǎn)單修改)因?yàn)橘Y源文件沒有變化,可以直接修改,比如修改一些字符串的對(duì)應(yīng)值,樣式、顏色等。布局文件也沒有變化,可以直接對(duì)布局中的控件的位置、長(zhǎng)寬、背景顏色等直接做修改。
2.(加入新的頁面)舉個(gè)例子,我現(xiàn)在要在當(dāng)前程序再加一個(gè)前導(dǎo)頁:
2.1.首先創(chuàng)建一個(gè)工程,工程只包含一個(gè)Activity,為了排除干擾,我將Test目錄刪除,去除工程其他的依賴包:

LauncherActivity.xml:
package com.mrtian.project.launcherapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LauncherActivity extends Activity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
textView = (TextView) findViewById(R.id.zz_tv_main);
button = (Button) findViewById(R.id.zz_btn_start);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("start!!!");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent("com.parttime.happytime.start");
startActivity(intent);
}
},6000);
}
});
}
}
activity_launcher.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mrtian.project.launcherapplication.LauncherActivity">
<TextView
android:id="@+id/zz_tv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/zz_btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點(diǎn)擊進(jìn)入"/>
</LinearLayout>
將前導(dǎo)頁工程編譯產(chǎn)生apk,用一種同樣的方法解包:

將程序的入口改成新加入的Activity,將原來的入口添加action為com.zhuanfa.money.lanch,方便我們的隱式調(diào)用。

工程中引用的資源都需要在這邊添加(res),res/layout中的activity_launcher.xml布局加入,
還有一個(gè)比較重要的地方,就是添加的控件id必須在res/values文件下的ids.xml和public.xml中添加(布局只需要在public.xml添加id,而控件ids.xml和public.xml都需要添加):
ids.xml:

public.xml:
值得一說的是public.xml內(nèi)部的id值是16進(jìn)制數(shù)編號(hào),我們只需要在對(duì)應(yīng)的type最后按順序添加就可以了。


將前導(dǎo)頁工程smali下的文件添加到本工程的com包下(AndroidManifest.xml注冊(cè)組件時(shí)對(duì)應(yīng)包名):

因?yàn)閟mali文件都是之前產(chǎn)生的,合并到本工程時(shí)id需要改為現(xiàn)在修改的id:
在目錄里面找到LauncherActivity.smali文件并打開,
所有資源的id對(duì)需要改為剛才我們?cè)趐ublic.xml中給資源添加的id值:
布局:

控件:


如果需要const/high16這樣的聲明,指的是對(duì)這個(gè)id值舍入,修改id值時(shí)將其改成const就行了,否則之后打包會(huì)報(bào)錯(cuò)
三、重新打包并簽名(不簽名不能安裝)。
經(jīng)過上面的步驟,我們的工程算是合并完了,打開終端,輸入命令:
java -jar apktool_2.1.1.jar b app-test/
成功執(zhí)行后會(huì)在app-test/dist目錄下產(chǎn)生一個(gè)apk
在對(duì)該apk進(jìn)行簽名操作:
jarsigner -verbose -keystore moonlighting.jks(證書) -storepass [密鑰] -keypass [密鑰] -signedjar Thinkdrive_signed.apk(簽名完后的apk) app-test/dist/app-debug.apk(上一步中產(chǎn)生的apk) moonlighting(證書名)