andforce

android_usb_devices


有时候android设备链接到电脑的时候会显示error:insufficient permissions for device,解决方法就是在/etc/udev/rules.d/51-android.rules 加入设备的idVenderidProduct,于是就有了下面这个自动添加的脚本.

#!/bin/bash
RULES=/etc/udev/rules.d/51-android.rules
TMP_USB_1=tmp_usb_1
TMP_USB_2=tmp_usb_2
TMP_USB=diff_usb

echo -n "请把手机链接电脑后,按回车:"

read user_input
lsusb > $TMP_USB_1
cat -n $TMP_USB_1

echo -n "请把手机拔掉后,按回车:"
read user_input

lsusb > $TMP_USB_2
cat -n $TMP_USB_2

diff $TMP_USB_1 $TMP_USB_2 > $TMP_USB

diff_usb_str=`sed -n '2p' $TMP_USB`
#echo ${diff_usb_str}

sub_str=${diff_usb_str:25:9}
#echo $sub_str

idVendor=${sub_str:0:4}
idProduct=${sub_str:5:4}

insert='SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"'${idVendor}'\", ATTRS{idProduct}==\"'${idProduct}'\",MODE=\"0666\"'

sudo touch $RULES
sudo sh -c "echo $insert >> $RULES"
sudo chmod a+x $RULES

echo -n "请再次把手机链接电脑,之后按回车:"
read user_input

adb kill-server && adb devices

echo "看到设备表示已经成功了,如果还是没有显示正常,重启一下电脑就好了"

rm $TMP_USB $TMP_USB_1 $TMP_USB_2

使用方法

把上面脚本内容保存到usb.sh中,执行bash usb.sh就行.

gitconfig


配置git,把下面脚本保存到~/.gitconfig即可.

[core]
	autocrlf = false
	excludesfile=~/.gitignore
	editor = vim
[user]
[merge]
	summary = true
	tool = meld
[diff]
	renames = copy
	tool = meld
	wordregex = "[[:alnum:]_]+|[^[:space:]]"
	external = /bin/git-meld.sh
[i18n]
	logoutputencoding = UTF-8
[color]
	diff = auto
	status = true
	branch = auto
	interactive = auto;
	ui = auto;
[showbranch]
	default = --topo-order
	default = heads/*
[status]
	submodulesummary = -1
[format]
	numbered = auto
[push]
	default = tracking

[alias]
	sed = !git ls-files --stage | grep ^100 | awk '{print $4}' | xargs sed -r -s
	slg = shortlog --pretty='format:%h %s'
	locate = !sh -c 'git ls-files | grep --color=auto "$1"' -
	co=checkout
        ci=commit -s -a
        br=branch
        st=status
        cp=cherry-pick
        cr=cherry
        rb=rebase
        fp=format-patch
        hist=log --pretty=format:\"%Cred%h %Cblue%ad %Cred| %Creset%s%d %Cgreen[%an]\" --graph --date=short
        flog=log --pretty=fuller --graph

switch-jdk-version


很多时候,我们在开发的时候,需要不同的jdk,手动切换比较麻烦,因此我写了个脚本,方便快速切换JDK版本.

#!/bin/bash

JAVA_6_HOME=/usr/lib/jvm/java-6-openjdk-amd64/
JAVA_7_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
JAVA_8_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

echo -e "\n输入编号以选择JDK版本:\n\t1. JDK6($JAVA_6_HOME) \n\t2. JDK7($JAVA_7_HOME) \n\t3. JDK8($JAVA_8_HOME)\n"
echo -n "直接回车会直接选择JDK7:"

read jdk_version
case $jdk_version in
    jdk6|1)
 export JAVA_HOME=$JAVA_6_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_6_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_6_HOME/bin/javac /usr/bin/javac
 ;;
    jdk7|2)
 export JAVA_HOME=$JAVA_7_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_7_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_7_HOME/bin/javac /usr/bin/javac
 ;;
    jdk8|3)
 export JAVA_HOME=$JAVA_8_HOME
 sudo rm /usr/bin/java && sudo ln -s $JAVA_8_HOME/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s $JAVA_8_HOME/bin/javac /usr/bin/javac
 ;;
    *)
 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/
 sudo rm /usr/bin/java && sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64/bin/java /usr/bin/java
 sudo rm /usr/bin/javac && sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64/bin/javac /usr/bin/javac
 ;;
esac

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

java -version

android-full-sdk


把framework中@hide的方法或者类打到jar中方便使用

ANDROID_BUILD_TOP=$(pwd)

rm -r sdk/sdk_full
mkdir sdk/sdk_full

echo "start copy jar files"
cp out/target/common/obj/JAVA_LIBRARIES/android.test.runner_intermediates/classes.jar sdk/sdk_full/test-classe.jar
cp out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar sdk/sdk_full/core-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar sdk/sdk_full/ext-classes.jar

cp out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar sdk/sdk_full/framework-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/android-common_intermediates/classes.jar sdk/sdk_full/common-classes.jar

cp out/target/common/obj/JAVA_LIBRARIES/com.android.phone.common_intermediates/classes.jar sdk/sdk_full/phone-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/pm_intermediates/classes.jar sdk/sdk_full/pm-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/com.android.vcard_intermediates/classes.jar sdk/sdk_full/vcard-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/calendar-common_intermediates/classes.jar sdk/sdk_full/calendarcommon-classes.jar
cp out/target/common/obj/JAVA_LIBRARIES/filterfw_intermediates/classes.jar sdk/sdk_full/filterfw-classes.jar

echo "copy jar files ok!!!"

cd $ANDROID_BUILD_TOP/sdk/sdk_full/

echo "start jar xvf all jar"
jar xvf test-classe.jar
jar xvf core-classes.jar
jar xvf ext-classes.jar
jar xvf framework-classes.jar
jar xvf common-classes.jar
jar xvf phone-classes.jar
jar xvf pm-classes.jar
jar xvf vcard-classes.jar
jar xvf calendarcommon-classes.jar
jar xvf filterfw-classes.jar

echo "jar xvf all jar ok!!!"

echo "del all jar"
rm *.jar

echo "start cvf all jar"
jar cvf ./android-full.jar *
echo " create full sdk successfully!!! "

cd $ANDROID_BUILD_TOP

如何使用

把上面的脚本放到源码根目录执行一下就好了

主题Denmo


这是一个连接Demo

一级标题

二级标题

三级标题

四级标题

五级标题
六级标题

有序列表展示

  1. 有序列表
  2. 有序列表
  3. 有序列表

无序列表展示

  • 无序列表
  • 无序列表
  • 无序列表

引用展示

引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用引用

Java代码块

public class KeyboardFrameLayout extends FrameLayout {


private KeyboardHelper mHelper;

public KeyboardFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mHelper = new KeyboardHelper(this);
}

public KeyboardFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mHelper = new KeyboardHelper(this);
}

public KeyboardFrameLayout(Context context) {
super(context);
mHelper = new KeyboardHelper(this);
}

public void setOnKeyboardStateListener(OnKeyboardStateChangeListener listener) {
mHelper.setOnKeyboardStateListener(listener);
}


public KeyboardHelper getKeyBoardHelper() {
return mHelper;
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
mHelper.init();
}

}

Shell代码块

WDYdeMacBook-Pro:vendor andforce$ ls -al
total 24
drwxr-xr-x  5 andforce  staff   170  9 27 10:56 .
drwxr-xr-x  8 andforce  staff   272  9 27 08:43 ..
-rw-r--r--  1 andforce  staff  2321  9 27 08:43 _echo.styl
-rw-r--r--  1 andforce  staff  1053  9 27 08:43 _highlight.styl
-rw-r--r--  1 andforce  staff  2226  9 27 08:43 _normalize.styl

一个程序员的软件


工欲善其事,必先利其器。列举一下我平时开发时候用到的软件。

完整软件

软件插件

Apk签名总汇


开发中,Apk签名是一件不大也不小的事,如果你做过微博相关的开发,你可能遇到如下情景:

用Eclipse或者AS直接点run生成的apk跑在手机上根本不能授权成功,每次需要导出release版本,很烦.

如果你基于系统源码开发或者二次开发系统预置App,你的流程可能是:

mm 或者 mmm 编译出apk,之后再push 到/system/app/ 下,或者用adb install -r 安装到手机.
否根本无法安装,因为签名不一样.

因此我搜集整理了如下内容,解决了以上两个难题,任何app都可以直接再IDE中直接run,并debug.

日常开发

修改Eclips或者Android Studio的默认签名文件

通常情况下,我们debug版本的apk和正式release的apk签名是不一样.特别开发一些有签名限制
的App的时候,就造成了调试上的不方便.

如何修改?

  • Eclipse

    Eclips -> Window -> Preference -> Androi -> Buil.
    Custom debug keystore 指定自己的 debug.keystore 就可以了.(注意签名文件的名字
    不必是debug.keystore).

  • Androi的 Studio

    android {
        signingConfigs {
            debug {
                storeFile file("debug.keystore")
            }
        }
    }

疑问?

如何创建自己的debug.keystore?

keytool -genkey -v -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android -keyalg RSA -validity 14000
// 这样你就创建了一个 debug.keystore

如何把现有签名文件,转换成Eclipse或Android Studio可用的debug.keystore?

假如你现在的签名文件是:my.keystore.

  • 修改my.keystore 的密码为:android
keytool -storepasswd -keystore my.keystore
  • 修改my.keystorealias为:androiddebugkey
keytool -changealias -keystore my.keystore -alias my_name -destalias androiddebugkey
  • 修改alias 的密码为:android
keytool -keypasswd -keystore my.keystore -alias androiddebugkey

执行完上面三步,你的debug.keystore 就创建完成了,替换上去就行.

系统源码开发

源码签名文件在哪?

SOURCE_CODE/build/target/product/security/

$:ls
media.pk8       platform.pk8       README    shared.pk8       signapk.jar  testkey.x509.pem
media.x509.pem  platform.x509.pem  Root.zip  shared.x509.pem  testkey.pk8  update.zip

但如果你在一家手机厂商工作,厂商一般不会直接用而是会进行定制:

SOURCE_CODE/vendor/CUSTOM_VENTER_NAME/security/

$: ls
media.pk8       platform.pk8       releasekey.pk8       shared.pk8       testkey.pk8
media.x509.pem  platform.x509.pem  releasekey.x509.pem  shared.x509.pem  testkey.x509.pem

如何用系统签名文件签名apk?

java -jar platform.x509.pem platform.pk8 UnSign.apk Signed.apk

zipalign优化apk

zipalign -v 4 MyDemo_signed.apk MyDemo_new.apk

是否用zipalign优化过

zipalign -c -v 4 MyDemo.apk

如何把platform.x509.pem platform.pk8 转成debug.keystore?

  • 生成shared.priv.pem
openssl pkcs8 -in platform.pk8 -inform DER -outform PEM -out shared.priv.pem -nocrypt
  • 生成pkcs12
openssl pkcs12 -export -in platform.x509.pem -inkey shared.priv.pem -out shared.pk12 -name androiddebugkey
// 如果生成的`debug.keystore`要用在Eclipse中使用,请输入密码:`android`,否则请输入自己的签名即可.
  • 生成debug.keystore
keytool -importkeystore -deststorepass android -destkeypass android -destkeystore debug.keystore -srckeystore shared.pk12 -srcstoretype PKCS12 -srcstorepass android -alias androiddebugkey