since.2006  

The connection to adb is down, and a severe error has occured.
You must restart adb and Eclipse.
Please ensure that adb is correctly located at "xxx" and can be executed.


ADT突然无法调试,如果试了其它方法无法解决,查看下任务管理器中是否有tadt.exe这个进程,有的话杀掉重启ADT试试。:-)

新版本QQ中加入了Android设备连接电脑提醒功能,启动了自己的一个adt.exe造成ADT无法调试,害得俺找了N久才找到问题原因。

Posted by hee at 08:07 AM | Permalink | 评论(0)

> 资源文件反编译(使用winzip):

将apk文件改名为zip文件并解压缩,进入解压缩目录下res\drawable和assets目录可以提取出图片等资源。

> 源代码反编译(使用dex2jar, jd-gui):

Android源代码最终会被编译成dex(Dalvik VM executes)格式,它和Java本身的classes格式有些区别,所以先要将dex格式用dex2jar转换成Java的字节码格式,然后再用jd-gui转换成源代码。

dex2jar下载:http://code.google.com/p/dex2jar/

jd-gui下载:http://java.decompiler.free.fr/?q=jdgui

# -f 覆盖已存在的jar文件,如果有的话
# -o 指定输出的jar文件名
d2j-dex2jar.bat -f -o apk.jar apkName.apk

然后使用jd-gui打开输出的apk.jar即可看到源码。

阅读全文 "Android资源文件、界面布局及源代码反编译" »

Posted by hee at 22:05 PM | Permalink | 评论(0)

通常使用WebView访问网络时在AndroidManifest.xml中申请"android.permission.INTERNET"权限就行了,以前也一直是这么做的(绝大部分Android设备都没问题)。

今天发现在Galaxy Note(Android 2.3.6)中,使用webView.loadUrl方法打开页面一直提示:"无效的URL","The page could not be opened because the URL is invalid"。

调试了N久发现Galaxy Note下还需要在AndroidManifest.xml中多添加2个权限项才行,这种不一致性感觉很让人蛋疼,尤其是没这么多设备的Android个人开发者。

这两个权限项是:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Posted by hee at 10:05 AM | Permalink | 评论(0)

Android程序运行时可以以编程方式动态创建一个程序快捷方式在home screen上,但并不是每种类型的程序都适合在桌面上创建快捷方式。

比如记帐类程序就比较适合自动创建一个快捷方式在home screen上,方便每天对消费进行记录,游戏类型的游戏不是那么的适合。

创建步骤为:

1.在AndroidManifest.xml中添加权限声明

<!-- AndroidManifest.xml -->


<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />


<!-- 新增创建快捷方式权限声明 -->

2.调用下面类进行添加

package com.since2006.helper;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

public class ShortcutHelper {

 private static final String TAG = "ShortcutHelper";

 private static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

 /**
  * 添加一个程序快捷方式到桌面
  *
  * 调用此方法,程序需要有"com.android.launcher.permission.INSTALL_SHORTCUT"权限
  *
  * className: 点击快捷方式要执行的类
  */
 public static void addShortcutToHomeScreen(Context context, String packageName, String className, int shortcutIcon, int shortcutName) {
  
  Parcelable iconRes = Intent.ShortcutIconResource.fromContext(context, shortcutIcon);
  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
  shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  shortcutIntent.setClassName(packageName, className);

  Intent intent = new Intent(ACTION_ADD_SHORTCUT);
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getText(shortcutName));
  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

  context.sendBroadcast(intent);
 }

}

调用示例:

/**
 * context
 * packageName
 * className: 点击快捷方式调用的类
 * shortcutIcon: 快捷方式图标
 * shortcutName: 快捷方式名称
 */
ShortcutHelper.addShortcutToHomeScreen(context, getPackageName(), ".ui.Login", R.drawable.logo, R.string.app_name);
Posted by hee at 09:07 AM | Permalink | 评论(0)

目前android下反编译只要用dex2jar,jd-gui简单几步就能反编译绝大部分代码,新版android sdk中集成了Proguard来混淆代码。使用比较简单:http://developer.android.com/guide/developing/tools/proguard.html

老项目从任一一个新建的项目中复制proguard.cfg到项目中,再配置一下default.properties,加入一行:proguard.config=proguard.cfg,用adt在release项目时,会自动使用默认的混淆配置来混淆代码。

工具虽好,但每次使用时都碰到些小问题,比如自动混淆时“过多”的混淆了代码,导致调用出错。

混淆时要注意的地方(以后使用中碰到问题慢慢添加):

  • 项目中使用webview和html页面交互,html代码中的js回调了java方法,这个类需要跳过混淆。

 

Posted by hee at 15:04 PM | Permalink | 评论(2)
有时应用需要访问本地应用以调试一些功能,在模拟器中使用127.0.0.1或localhost访问的是模拟器本身。如果需要访问本地应用可以使用特定IP:10.0.2.2

Posted by hee at 10:02 AM | Permalink | 评论(0)