现在的位置: 首页 > 综合 > 正文

Android Dev Tips (持续更新)

2014年10月17日 ⁄ 综合 ⁄ 共 8208字 ⁄ 字号 评论关闭

Android Dev Tips

  1. 编译整个android source
    如何启动emulator
    export ANDROID_PRODUCT_OUT=~/Android/mydroid/out/target/product/generic/
    ./out/host/linux-x86/bin/emulator -kernel
    ~
    /Android/mydroid/prebuilt/android-arm/kernel/kernel-qemu -ramdisk
    ~
    /Android/mydroid/out/target/product/generic/ramdisk.img -system
    ~
    /Android/mydroid/out/target/product/generic/system.img -partition-size 128 &

    有一点需要注意:在有透明代理或者自设定代理需要验证的环境下, 自编译的模拟器上网功能会出问题(没法弹出验证输入窗口)

  2. 如何分析Android的memory usage

    http://elinux.org/Android_Memory_Usage

  3. android中的libc库,是从openbsd中移植过来的
    一个相当有用的邮件组中提到的:
    http://lists.debian.org/debian-bsd/2003/01/msg00013.html

    关于openbsd的libc的相关概念论述
    http://www.over-yonder.net/~fullermd/rants/bsd4linux/03

  4. 在adb shell下,启动应用程序
    am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity

  5. 某些情况下adb shell会找不到devices那么请使用
    adb kill-server重启所有的server之后,就可以了

  6. linux下有用的工具
    busybox

    http://benno.id.au/blog/2007/11/14/android-busybox

    lsof

    http://code.google.com/p/android-group-korea/updates/list
    关于android
    cmwap通信的研究

  7. 基于原来做车载项目的经验,一般都是先通过ppp连接,表明于Access
    Point联通之后,才会进行tcp的相关通信!

    下面就看看,Android的java层的mobile network的setting,如何跟底层的PPP相交互了。

    Android java层的相关代码

    http://groups.google.com/group/android-porting/browse_thread/thread/5031fb191f2f1a14
    /telephony/java/com/android/internal/telephony/gsm/DataConnectionTracker.java
    /telephony/java/com/android/internal/telephony/gsm/PdpConnection.java
    /telephony/java/com/android/internal/telephony/gsm/PppLink.java
    /core/java/android/net/MobileDataStateTracker.java
    相关的状态
    /sys/class/net/ppp0/operstate

  8. NDK
    debug
    get from Wenlong
    Feng
     wenlong.feng@gmail.com

    share
    my best practice to debug via ndk-gdb

    1. Make sure you obj file is
    compiled with NDK_DEBUG.  I always remove the binary obj before a new compiling to ensure this.

    2.
    Make sure the "gdb.setup" is set well,  two settings are needed before
    ndk-gdb is launching which should looks similar as below italic one.
    During each time ndk-gdb launch, it will append some more parameters to
    this configure file, so if you run it twice, duplicated parameters will
    be appended.

    In
    order to avoid this, I save a good file into someplace such as
    PROJ_DIR, and copy it to the corresponding location each time before I
    launch ndk-gdb.

    "set
    solib-search-path /home/wenl/vms/androidSDK/android-sdk-linux_x86/samples/android-12/ApiDemos/obj/local/armeabi

    directory

    /home/wenl/vms/androidNDK/android-ndk-r5c/platforms/android-9/arch-arm/usr/include

    /home/wenl/vms/androidNDK/android-ndk-r5c/sources/android/native_app_glue

    /home/wenl/vms/androidSDK/android-sdk-linux_x86/samples/android-12/ApiDemos/jni
    /home/wenl/vms/androidNDK/android-ndk-r5c/sources/cxx-stl/system"

    alias
    ddd='cp
    /home/wenl/vms/androidSDK/android-sdk-linux_x86/samples/android-12/ApiDemos/gdb.setup

    /home/wenl/vms/androidSDK/android-sdk-linux_x86/samples/android-12/ApiDemos/obj/local/armeabi/gdb.setup;
    ndk-gdb'
    alias bb='rm -rf /home/wenl/vms/androidSDK/android-sdk-linux_x86/samples/android-12/ApiDemos/obj;
    ndk-build NDK_DEBUG=1 V=1'

     


  9. 在HTC Hero尝试ppp连接
    可以发AT命令
    如:
     
     
    echo -e "AT+HTC_DDTM?\r"
    > /dev/smd0    ---》 判断目前"Dedicated Data Transmission Mode"

     
     
    echo -e "AT+HTC_DDTM=0\r"
    > /dev/smd0   ---》turn off "Dedicated Data Transmission Mode"

    关于CDMA的一些常识
    CDMA can't do data and voice
    at the same time so if the phone is transferring data and you get an incoming call, it will go to straight to voicemail. For WM most people don't notice this because data isn't always on




  10. Android 权限管理
    控制连入的user(console)是否是root权限,由adbd来控制,代码在/system/core/adb/adb.c
    所有的权限在头文件
    /home/luq/Android/mydroid/system/core/include/private/android_filesystem_config.h

    然后就是利用su命令来做需要改变各种权限的事情了。其实就是uid和文件权限的一些关系罢了!(由kernel中的kernel_start启动UID为0的init,来初始化一些事情)
       Cymod中就是利用ROM Manager和superuser来配合完成的。同时,Cymod更改了/system/extras/su/中的su的实现,方便对于root权限的控制.

       由应用程序superuser维护了一个系统中所有应用程序权限的db,当有应用程序尝试调用su这个命令时,会使用上述修改后的su,从而调用system/extra/su/activity.cpp中的send_intent函数发送一个定制的broadcast(这里很关键,可以从中看到如何从native层构建一个有效的intent给java的activity manager来解析), 这个broadcast会被superuser接收到,并根据传递上来的uid,gid等信息查询


    关于Android的权限控制,参看浅析android中的权限管理--用户安装的apk的uid,gid是如何分配的


  11. Toast
    的两种显示时间:Short -> 2 s, Long -> 3.5 s, 具体详见

    frameworks/base/services/java/com/android/server/NotificationManagerService.java
    line: 81,82 
    Android FM hack相关资料
    可以参看CyanogenMod源代码的frameworks/base/core/jni/android_hardware_fm.cpp,jni与kernel的driver模块通信,然后提共java接口给framework/base/core/java/中使用,然后,apps/下的FM通过aidl与framework进行控制及数据通信!!

    Nexus one用的是broadcom的bcm4329radio模块,支持bluetooth,
    wifi, fm。



  12. 如CyanegonMod中expend Status
    bar中可以直接更改wifi, sound, GPS, BT的功能,从它的源代码分析,是通过增加新的叫做

    PowerButton的一个widget(framework/base/services/java/com/android/server/status/widget/PowerButton.java),然后在
    被StatusBarService中所显示和提供相应的功能(framework/base/services/java/com/android/server/status/StatusBarService.java的setupPowerWidget())

  13. 关于Android中的签名
    关于签名:
    build/target/product/security目录中有四组默认签名供Android.mk在编译APK使用:
    1、testkey:普通APK,默认情况下使用。
    2、platform:该APK完成一些系统的核心功能。经过对系统中存在的文件夹的访问测试,这种方式编译出来的APK所在进程的UID为system。
    3、shared:该APK需要和home/contacts进程共享数据。
    4、media:该APK是media/download系统中的一环。
    应用程序的Android.mk中有一个LOCAL_CERTIFICATE字段,由它指定用哪个key签名,未指定的默认用testkey.

  14. Android的用户空间与Linux的用户空间地址分配
    以经典的3:1分配为例
    Android 的用户空间

    普通Linux的用户空间

  15. 如何模拟键盘及touch事件
    参照google project中的screencast的实现
    关键是使用app_process来调用client端的一个agent来解释点击操作,从而在client的agent中通过IWindowManager的一些接口来响应事件!!

    整个工程的组织(server, client的组织, java与android的通信, client的agent的jar包的deployment等), 准备写一个分析的相关文章,名字暂定为<Android之开源项目分析--screencast>

  16. Android的优化
    http://elinux.org/images/0/02/Android_Platform_Optimizations_SNPS_20111027.pdf
  17. 巧用Android系统提供的工具
    在device中, 有一个service命令, 可以看到当前所有的service,同时也可以使用它来往一些activity发送一些信息
    如下所示
    root@android:/ # service                                                       
    Usage: service [-h|-?]
           service list
           service check SERVICE
           service call SERVICE CODE [i32 INT | s16 STR] ...
    Options:
       i32: Write the integer INT into the send parcel.
       s16: Write the UTF-16 string STR into the send parcel.

    root@android:/ # service list                                                  
    Found 61 services:
    0	sip: [android.net.sip.ISipService]
    1	phone: [com.android.internal.telephony.ITelephony]
    2	iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo]
    3	simphonebook: [com.android.internal.telephony.IIccPhoneBook]
    4	isms: [com.android.internal.telephony.ISms]
    5	nfc: [android.nfc.INfcAdapter]
    6	samplingprofiler: []
    7	diskstats: []
    8	appwidget: [com.android.internal.appwidget.IAppWidgetService]
    9	backup: [android.app.backup.IBackupManager]
    10	uimode: [android.app.IUiModeManager]
    11	usb: [android.hardware.usb.IUsbManager]
    12	audio: [android.media.IAudioService]
    13	wallpaper: [android.app.IWallpaperManager]
    14	dropbox: [com.android.internal.os.IDropBoxManagerService]
    15	search: [android.app.ISearchManager]
    16	country_detector: [android.location.ICountryDetector]
    17	location: [android.location.ILocationManager]
    18	devicestoragemonitor: []
    19	notification: [android.app.INotificationManager]
    20	mount: [IMountService]
    21	throttle: [android.net.IThrottleManager]
    22	connectivity: [android.net.IConnectivityManager]
    ......

    root@android:/ # service call phone 2 s16 "123"                                
    Result: Parcel(00000000    '....')

    此时, 就直接拨号了:), 但是这里注意, 紧急号码在这里是不work的.

    下面再来一个用来发短信的

    root@android:/ # service call isms 4 s16 "12345678" s16 "" s16 "hello world!" s16 "" s16 ""

    下面就说一下原理
    大家先找到代码frameworks/base/telephony/java/com/android/internal/telephony/ITelephony.aidl和ISms.aidl,
    这两个文件都是给OEM厂商集成用的, 代码我这里就不贴了,细心的童鞋一眼就能看出来, 上面的"2", "4"就是指定了是哪一个函数
    比如, 2 就是

    /**
         * Place a call to the specified number.
         * @param number the number to be called.
         */
        void call(String number);

    4就是

     /**
         * Send an SMS.
         *
         * @param smsc the SMSC to send the message through, or NULL for the
         *  default SMSC
         * @param text the body of the message to send
         * @param sentIntent if not NULL this <code>PendingIntent</code> is
         *  broadcast when the message is sucessfully sent, or failed.
         *  The result code will be <code>Activity.RESULT_OK<code> for success,
         *  or one of these errors:<br>
         *  <code>RESULT_ERROR_GENERIC_FAILURE</code><br>
         *  <code>RESULT_ERROR_RADIO_OFF</code><br>
         *  <code>RESULT_ERROR_NULL_PDU</code><br>
         *  For <code>RESULT_ERROR_GENERIC_FAILURE</code> the sentIntent may include
         *  the extra "errorCode" containing a radio technology specific value,
         *  generally only useful for troubleshooting.<br>
         *  The per-application based SMS control checks sentIntent. If sentIntent
         *  is NULL the caller will be checked against all unknown applications,
         *  which cause smaller number of SMS to be sent in checking period.
         * @param deliveryIntent if not NULL this <code>PendingIntent</code> is
         *  broadcast when the message is delivered to the recipient.  The
         *  raw pdu of the status report is in the extended data ("pdu").
         */
        void sendText(in String destAddr, in String scAddr, in String text,
                in PendingIntent sentIntent, in PendingIntent deliveryIntent);

    所以, 以后要想在后台发短信,打电话,可以直接调用Java的Runtime Exec来调用service提供的命令, 这样就可以部分绕过framework中的一些java service, 而直接跟更底层的c++/C实现的service直接交互:)

  18. Mac下编译android source
    http://wiki.cyanogenmod.com/wiki/Nexus_One:_Compile_CyanogenMod_(OS_X)

  19. 如何反编译android apk
    使用smali或者asmdex

  20.  
  21.  
  22.  
  23.  

抱歉!评论已关闭.