引用:http://blog.163.com/sky522249168@126/blog/static/64636404201111502010307/
一、前言:
诸如layout里的布局及权限文件 (AndroidManifest.xml)已经是一堆乱码了,完全看不懂,想看看别人是怎么布局的都不容易。还有源代码都被编译成了 classes.dex,完全看不出什么线索。基于以上的困惑,笔者给大家分享一下Android中的拿来主义。
二、所需工具(点击各自连接进入下载页面):
1.AXMLPrinter2.jar
2.baksmali.jar
3.smali.jar
资源下载地址:http://code.google.com/p/android4me/downloads/list
三、准备工作
为了方便起见,作者把AXMLPrinter2.jar, 还有baksmali.jar,还有smali.jar(下下来为了方便重命名),放在Android SDK tools文件夹中如下图所示:
四、开始拿来主义1.用 AXMLPrinter2.jar查看apk中的布局xml文件:将ApkInstaller应用生成的ApkInstaller.apk(为了方便起见放到tools目录里)用WinRAR等 工具打开,将res/layout/main.xml解压出来(也还是放在tools目录里哦)打开main.xml文件,内容如下(一堆天文):
这时候AXMLPrinter2.jar派上用场了,打开cmd终端,一直进入到tools目录下,输入如下命令:java -jar AXMLPrinter2.jar main.xml > main.txt.
打开main.txt代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="1"
android:layout_width="-1"
android:layout_height="-1"
>
<WebView
android:id="@7F050000"
android:layout_width="-1"
android:layout_height="-2"
>
</WebView>
</LinearLayout>
为了比对打开源程序中的main.xml代码如下(大家比对一下吧):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/apk_web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
2.用baksmali.jar反编译classes.dex:将ApkInstaller.apk里的classes.dex解压到tools目录里,然后baksmali.jar就派上用场了,在cmd命令行里输入如下命令:java -jar baksmali.jar -o classout/ classes.dex .
下面我们看一下ApkInstaller.smali内容是什么,如以下代码:
.class public Lcom/tutor/apkinstaller/ApkInstaller;
.super Landroid/app/Activity;
.source "ApkInstaller.java"
# instance fields
.field private apkWeb:Landroid/webkit/WebView;
# direct methods
.method public constructor <init>()V
.registers 1
.prologue
.line 8
invoke-direct {p0}, Landroid/app/Activity;-><init>()V
return-void
.end method
# virtual methods
.method public onCreate(Landroid/os/Bundle;)V
.registers 5
.parameter "savedInstanceState"
.prologue
.line 13
invoke-super {p0, p1}, Landroid/app/Activity;->onCreate(Landroid/os/Bundle;)V
.line 14
const/high16 v2, 0x7f03
invoke-virtual {p0, v2}, Lcom/tutor/apkinstaller/ApkInstaller;->setContentView(I)V
.line 15
const/high16 v2, 0x7f05
invoke-virtual {p0, v2}, Lcom/tutor/apkinstaller/ApkInstaller;->findViewById(I)Landroid/view/View;
move-result-object v2
check-cast v2, Landroid/webkit/WebView;
iput-object v2, p0, Lcom/tutor/apkinstaller/ApkInstaller;->apkWeb:Landroid/webkit/WebView;
.line 16
iget-object v2, p0, Lcom/tutor/apkinstaller/ApkInstaller;->apkWeb:Landroid/webkit/WebView;
invoke-virtual {v2}, Landroid/webkit/WebView;->getSettings()Landroid/webkit/WebSettings;
move-result-object v1
.line 17
.local v1, webSettings:Landroid/webkit/WebSettings;
const/4 v2, 0x1
invoke-virtual {v1, v2}, Landroid/webkit/WebSettings;->setJavaScriptEnabled(Z)V
.line 19
const-string v0, "http://frankiewei.net/apk/demos/main/index.html#home"
.line 20
.local v0, apkUrl:Ljava/lang/String;
iget-object v2, p0, Lcom/tutor/apkinstaller/ApkInstaller;->apkWeb:Landroid/webkit/WebView;
invoke-virtual {v2, v0}, Landroid/webkit/WebView;->loadUrl(Ljava/lang/String;)V
.line 21
return-void
.end method
同样为了比对我们看一下ApkInstaller.java的源代码如下:
package com.tutor.apkinstaller;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class ApkInstaller extends Activity {
private WebView apkWeb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
apkWeb = (WebView)findViewById(R.id.apk_web);
WebSettings webSettings = apkWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
String apkUrl = "http://frankiewei.net/apk/demos/main/index.html#home";
apkWeb.loadUrl(apkUrl);
}
}
我相