android 命令行模式启动模拟器

启动模拟器需要两个步骤:

1.创建AVD(Android Virtual Device)

2.启动emulator

一般使用Eclipse开发时,开启一个模拟器就是这么一个过程,其实命令行模式下也是这样。刚开始不知道如何命令行启动模拟器的时候就输入了一个emulator,报错了,出现如下提示:

  1. emulator: ERROR: You did not provide the name of an Android Virtual Device  
  2. with the '-avd <name>' option. Read -help-avd for more information.  
  3.    
  4. If you *really* want to *NOT* run an AVD, consider using '-data <file>'  
  5. to specify a data partition image file (I hope you know what you're doing).  

1.创建AVD

根据提示信息说明,需要先有一个AVD,即一个android的虚拟设备,在命令行输入android create avd,当然前提是在linux中配置好了环境变量,否则会出现找不到命令的错误提示的。如果环境变量配置正确会出现了错误提示信息:

  1. Error: The parameters --name, --target must be defined for action 'create avd'  
  2.   
  3. Usage:  
  4.   android [global options] create avd [action options]  
  5.   
  6. Global options:  
  7.   -s --silent   Silent mode: only errors are printed out.  
  8.   -h --help     Help on a specific command.  
  9.   -v --verbose  Verbose mode: errors, warnings and informational messages are printed.  
  10.   
  11. Action "create avd":  
  12.   Creates a new Android Virtual Device.  
  13. Options:  
  14.   -c --sdcard   Path to a shared SD card image, or size of a new sdcard for the new AVD  
  15.   -s --skin     Skin for the new AVD  
  16.   -a --snapshot Place a snapshots file in the AVD, to enable persistence.  
  17.   -n --name     Name of the new AVD [required]  
  18.   -p --path     Directory where the new AVD will be created  
  19.   -t --target   Target ID of the new AVD [required]  
  20.   -f --force    Forces creation (overwrites an existing AVD)  


根据上述所提到的参数,并通过查资料得到了-t --target参数如何获得。在命令行下输入android list target,显示如下:

  1. android list target  
  2.   
  3. Available Android targets:  
  4. id: 1 or "android-8"  
  5.      Name: Android 2.2  
  6.      Type: Platform  
  7.      API level: 8  
  8.      Revision: 2  
  9.      Skins: WQVGA432, WQVGA400, HVGA, WVGA854, QVGA, WVGA800 (default)  

其中的id:这一行就是我们需要的target的参数

 

如果启动的模拟器还需要sdcard的话,还需要首先创建一个sdcard的镜像

  1. mksdcard -l sdcard 512M ~/xx/sdcard.img  


这样就很容易写出创建AVD的命令了

  1. android create avd -c ~/xxx/sdcard.img -n emulator2011 -p ~/test/ -t 1 -f  

2.启动模拟器

首先通过android list avd 查看建好的虚拟设备

  1. Available Android Virtual Devices:  
  2.     Name: android.2.2  
  3.     Path: /home/XXX/.android/avd/android.2.2.avd  
  4.   Target: Android 2.2 (API level 8)  
  5.     Skin: HVGA  
  6. ---------  
  7.     Name: emulator2011  
  8.     Path: /home/XXX/test  
  9.   Target: Android 2.2 (API level 8)  
  10.     Skin: WVGA800  
  11.   Sdcard: /home/XXX/sdcard.img  


然后通过命令

  1. emulator @emulator 2011  


就启动了第二个类型的模拟器。

 

其实用命令行启动模拟器和eclipse里启动是相同的。上面两个步骤就是对应eclipse中创建avd和启动模拟器的过程,使用eclipse创建avd,它会在家目录下建立.android的隐藏文件夹,将avd的信息全都放到这里面

原文地址:https://www.cnblogs.com/lvfeilong/p/fgdsd5435.html