cordova platform add android not working while listing Android targets

71,691

Solution 1

To work, this cordova command needs to use some programs located into your sdk/tools directory. You need also have installed apache ant.

Then you must add these directories into your PATH system variable:

Background:

  • let's assume you have installed your Android SDK to the c:\sdk\android directory
  • you have installed you Apache ant to the c:\tools\apache-ant directory

Then you must create two system variables:

  1. ANDROID_HOME with the c:\sdk\android value
  2. ANT_HOME with the c:\tools\apache-ant value

Finally, you must modify the PATH variable and add those two to the end of the PATH' value:

;%PATH%\tools;%ANT_HOME%\bin;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools

NOTE: for those who uses Linux, the instruction differs a bit.

More documentation available here.

Solution 2

For those chosen ones, who preferred Linux development environment

Requirements

First of all, you will need a few things to get started. They are: Android SDK and Apache Ant. Of course, you will need Java SDK (JDK) installed.

To get Android SDK working for all users, you shall need to modify the /etc/environment file and then restart your PC. But if you do not want that hard way - follow me, think of yourself as the only PC user. And use /home/YOUR_USERNAME/.bashrc file to edit.

Let's remember your home path one time to prevent further long lines. Add this one to your /home/YOUR_USERNAME/.bashrc:

export HOME="/home/YOUR_USERNAME"

We'll then use the $HOME notation when we want to say "/home/YOUR_USERNAME directory".

Setting up Android SDK

Download the Android SDK archive and unzip it somewhere. Let's say, yo your home directory, $HOME/adt-bundle/.

Add these lines to your $HOME/.bashrc:

export ANDROID_HOME="$HOME/android-bundle/sdk/tools"
export ANDROID_PLATFORM_TOOLS="$HOME/android-bundle/sdk/platform-tools"
export PATH="$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS:$PATH"

Setting up Ant

Just as with the Android SDK, download an archive and unzip it to your home directory. Then add these to your .bashrc:

export ANT_HOME="$HOME/ant"
export PATH="$PATH:$ANT_HOME/bin"

I've installed one via the apt-get so this did not affect my .bashrc.

Applying changes

To make these changes work, you should either work in a new terminal window (opened after the changes), or run source ~/.bashrc to make changes available in the current terminal window.

Wrapping up

At the end, you will got:

  1. Two directories at your home directory - ant and android-bundle
  2. A few lines, added to your .bashrc:

    export ANDROID_HOME="$HOME/android-bundle/sdk/tools"
    export ANDROID_PLATFORM_TOOLS="$HOME/android-bundle/sdk/platform-tools"
    export PATH="$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS:$PATH"
    
    export ANT_HOME="$HOME/ant"
    export PATH="$PATH:$ANT_HOME/bin"
    

Solution 3

Run the "android" command from your adt\sdk\tools folder and install the latest Tools and SDK. Also make sure your PATH has the right variables.

For this you will need ANT to be installed , a JAVA JDK and an Android SDK installed

JAVA_HOME (C:\Program Files\Java\jdk)

ANT_HOME ({ant location}\apache\apache-ant)

ANDROID_HOME ({android sdk location}\android-sdk)

Add these to your PATH variable like %ANT_HOME%/bin;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%JAVA_HOME%\bin

Close and re-open your cmd and run your command again.

Similiar to PhoneGap/Cordova Android Development

Solution 4

I'm not sure if this is your problem, but I've encountered similar errors when the cordova library cache gets polluted with something corrupted. To fix it, you just need to delete the cordova cache, and it will automatically repopulate next time you use 'cordova'.

On OS X, this directory is ~/.cordova. On Windows, I assume it's .cordova in your users home directory still.

Solution 5

The answer is "All of the Above". Do as mentioned with the environment variables, however, also do this:

C:\Users{YOUR_NAME}.cordova\lib\android\cordova\3.5.0\framework edit the project.properties file and change target=android-19 to target=android-20.

Presumably this will need to be changed for the next rendition of android sdk's as well until this little issue gets resolved.

Share:
71,691
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I got problem when i want to add an android platform to my phoneGap application. I got this message in my CLI when i execute the command cordova platform add android :

    Checking Android requirements... (Error: An error occurred while listing Android targets)

    I already try to add my android sdk location in the path variable.

    Please help me ! :D

    I work on Windows 7 64 bits, i install the android API 17, 18 and 19 with the android SDK. I am on 3.2 phoneGap version.

  • Marshal
    Marshal over 10 years
    Gees, I don't understand why it's so hard for the open source project team to take some effort for good documentation. It always creates a big gap between a fairly good product and its users, leaving users fumbling around and wasting time until some light shed by the experienced.
  • l.cotonea
    l.cotonea over 10 years
    I understand you. I think that these last years, open software projects have lost in quality. It's maybe the latest fad: each day, there is 5 starred project under github and everyone seems to zap to take the last popular project. However, in our case (Cordova), I think this is a planning problem. They have to release quickly some new versions to fix some problem with Androïd Kit Kat. I suppose that the team wants to stabilize this version in a primary goal, and after that, they will probably finish the documentation part...
  • deinerson1
    deinerson1 over 10 years
    Clarification: this should prep a system to not need PATH environment mods for ant: sudo apt-get install ant ant-contrib ant-optional ant-gcj
  • shybovycha
    shybovycha over 10 years
    @deinerson1 but are you sure, Debian/Ubuntu repositories contain the correct, or event the required Ant version? ;)
  • deinerson1
    deinerson1 over 10 years
    This command displays the version in the ubuntu/debian repos to aid in validating compatibility (Version: 1.9.2-1 as of this comment): apt-cache show ant
  • shybovycha
    shybovycha over 10 years
    @deinerson1 different Ubuntu versions use different package sets. So, for example, Ubuntu 10.04 LTS will not contain the latest version of Ant within its repos. I unified this process. Ah, yes, and gcj is not oracle-java7 or whatever. Those are different Java compilers ;)
  • swdev
    swdev over 10 years
    Concise solution for Windows platform. Great!
  • Cammy
    Cammy about 10 years
    I agree with @Marshal, for the mediocre programmer this is close to rocket science. I really dont get why there's gaps in documentation (like phonegaps) that regular people have big issues with :(
  • ziiweb
    ziiweb about 10 years
    This answer gave me the hint in LINUX, since I can also run android command to open the Android SDK Manager and install Andrid SDK Build-tools 19.0.2. Before that I also installed Android SDK from their site, and add export PATH=${PATH}:/home/YOUR_USER_HERE/adt-bundle/sdk/platform-to‌​ols:/home/YOUR_USER_‌​HERE/adt-bundle/sdk/‌​tools to ~/.bash_profile. After that everything is installed ok.
  • 尤川豪
    尤川豪 about 10 years
    I love you. you save my day
  • Barış Akkurt
    Barış Akkurt about 10 years
    I am getting the same error. however all of my variables are defined you can see here. stackoverflow.com/questions/23422961/…
  • Admin
    Admin about 10 years
    and make sure to open up a new terminal window or run source ~/.bashrc after making these changes!! I forgot to do that and I thought it still wasn't working.
  • Jeffrey Nicholson Carré
    Jeffrey Nicholson Carré almost 10 years
    Same thing for Comodo... [>.<]
  • John Walthour
    John Walthour almost 10 years
    This worked for me. In addition, I had to remove an existing reference to Ant that was already in the PATH variable. On my machine, the PATH had an absolute reference to Ant already (it was C:\Program Files\apache-ant-xxx or something like that). The command prompt could find ant, but cordova couldn't. Everything started working when I removed the full reference and replaced it with %ANT_HOME%\bin.
  • 0x6C77
    0x6C77 almost 10 years
    I would have got this working first time if I had remembered to close and re-open my command prompt window. Wasted around 15 minutes before I realized.
  • mrshickadance
    mrshickadance almost 10 years
    Here is a blog about installing it from start to finish with a little more detail than phonegap/cordova docs offer. A lot easier than piecing together the different stack questions: andrewsavory.com/blog/2013/2517
  • mylord
    mylord over 9 years
    I have this and still the same error. I don't have $ANT_HOME however, as there is no bin dir on osx? $ which ant /usr/local/bin/ant $ ls /usr/local/bin/ant /usr/local/bin/ant $ ant -v Apache Ant(TM) version 1.9.4 compiled on April 29 2014 $ cordova --version 4.0.0
  • mylord
    mylord over 9 years
    I have this and still the same error. export HOME="/Users/rover" export ANDROID_HOME="$HOME/Documents/Dev/Android/adt-bundle-mac-x86‌​_64-20140702/sdk" export ANDROID_PLATFORM_TOOLS="$ANDROID_HOME/platform-tools" export PATH="$ANDROID_HOME:$ANDROID_PLATFORM_TOOLS:$ANDROID_HOME/bu‌​ild-tools:$PATH" export ANT_HOME="/usr/local/bin/ant" #export PATH="$PATH:$ANT_HOME/bin" $ which ant /usr/local/bin/ant $ ls /usr/local/bin/ant /usr/local/bin/ant $ ant -v Apache Ant(TM) version 1.9.4 $ cordova --version 4.0.0 error is: node_modules/q/q.js:126 throw e;
  • user1568901
    user1568901 almost 8 years
    If you install the full Android Studio w/SDK, it automagically sets all this up for you.