cordova platform add android not working while listing Android targets

AndroidWindowsCordova

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 https://stackoverflow.com/questions/20576704/phonegap-cordova-android-development/21613524#21613524

Solution 4 - Android

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 - Android

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.

Solution 6 - Android

I had the same issue even though path variables were set exactly as per the instructions. After going through multiple files I finally got it resolved. For me (Windows 7 enterprise 64 bit) I had to modify check_reqs.js and create.js from "C:\Users<user-name>.cordova\lib\android\cordova\3.5.0\bin\lib" folder to include absolute path for the android.bat. My android SDK is under "C:\Android\android-sdk".

In check_reqs.js I modified

child_process.exec('android list targets', function(err, stdout, stderr) {

to

child_process.exec('C:\\Android\\android-sdk\\tools\\android.bat list targets', function(err, stdout, stderr) {

In create.js I modified the statement

return exec('android update project --subprojects --path "' + projectPath + '" --target ' + target_api + ' --library "' + path.relative(projectPath, targetFrameworkDir) + '"');

to

return exec('C:\\Android\\android-sdk\\tools\\android.bat update project --subprojects --path "' + projectPath + '" --target ' + target_api + ' --library "' + path.relative(projectPath, targetFrameworkDir) + '"');

Also do not forget those double "\\" in the absolute path

Solution 7 - Android

after a long struggle, after doing all of the above suggestions more then once with growing desperation, i simply openned my cmd as administrator. it worked. i guess i just got too used for "sudo" before i got this pc.

Solution 8 - Android

For those who have encountered this problem I add something extra that can be helpful, in my case had all the variables properly configured and this was still unable to add android platform and I did was disable my avast antivirus and puff! everything flowed smoothly, what was happening was that the antivirus was blocking the creation of the platform.

Also important for me was to restart the pc. ;)

Regards.

Solution 9 - Android

I noticed the problem with Cygwin/Windows 7. The problem stems from the slightly different ways Cygwin and MS-DOS shells treat .bat files.

During "add platform", cordova invokes "android":

C:\Users\xxx\.cordova\lib\android\cordova\3.4.0\bin\lib\check_reqs.js

Where it calls 'android list targets' (on line 73)

"android" should resolve to /xx/android-sdk/tools/android.bat or xx:\android-sdk\tools\android.bat. (And, in fact it does, I can run "android" on the MS-DOS command shell - but not in Cygwin shell. There I need to add ".bat".)

"android" without ".bat" fails in Cygwin shell because android.exe doesn't exist (only .bat does). Changing line 73 to invoke 'android.bat list targets' will solve your problem (or give a reasonable error message).

Another work-around is to run cordova in a MS-DOS shell instead of Cygwin shell.

Solution 10 - Android

We have a list of solutions here, so I add my one. That was not clear for me until I just try. You should add %ANT_HOME%\bin;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools to USER PATH variable, not to a System one. System PATH variable will automatically concatenate the USER PATH variable and translate variables to its value. Hope that helps somebody. P.S. My OS is Win 7x64

Solution 11 - Android

This problem is usually cause because of PATH variable has not been set for Android SDK.

export HOME="/home/yourname"
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 12 - Android

Accepted answer is good and to the point.

I have come across another page where its very well explained with screen shots.

You can refer http://bealers.com/2014/06/phonegap-android-development-environment-for-windows/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbalasta75View Question on Stackoverflow
Solution 1 - Androidl.cotoneaView Answer on Stackoverflow
Solution 2 - AndroidshybovychaView Answer on Stackoverflow
Solution 3 - AndroidLlewellyn CollinsView Answer on Stackoverflow
Solution 4 - AndroidJason FarnsworthView Answer on Stackoverflow
Solution 5 - AndroidTristan IsfeldView Answer on Stackoverflow
Solution 6 - Android508NinjaView Answer on Stackoverflow
Solution 7 - AndroidYuval PerelmanView Answer on Stackoverflow
Solution 8 - AndroidDarq RoyaView Answer on Stackoverflow
Solution 9 - AndroidCharlie DalsassView Answer on Stackoverflow
Solution 10 - AndroidBillyZView Answer on Stackoverflow
Solution 11 - AndroidBastin RobinView Answer on Stackoverflow
Solution 12 - AndroidTejasvi HegdeView Answer on Stackoverflow