52

UPDATE 1:

This was everything I did in the end:

sudo aptitude install -y python-software-properties python g++ make
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo aptitude update
sudo aptitude install nodejs
sudo aptitude install npm
sudo npm install -g cordova
sudo npm install -g phonegap
phonegap create my-app
cd my-app
export PATH=${PATH}:/home/oshirowanen/Downloads/adt-bundle-linux-x86_64-20131030/sdk/platform-tools:/home/oshirowanen/Downloads/adt-bundle-linux-x86_64-20131030/sdk/tools
sudo aptitude install ant
cordova platform add android
android create avd --name my_and --target 1
phonegap run android

Was any of that not neccessary?


ORIGINAL QUESTION:

On my Ubuntu 12.04 computer, I've just installed node.js with npm and cordova:

$ node -v
v0.10.23

$ npm -v
1.3.17

$ cordova -v
3.3.0-0.1.1

When I try to add android:

$ cordova platform add android

I get the following output:

Creating android project...

/usr/lib/node_modules/cordova/node_modules/q/q.js:126
                    throw e;
                          ^
Error: An error occured during creation of android sub-project. 

/home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:126
                    throw e;
                          ^
Error: An error occurred while listing Android targets
    at /home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/lib/check_reqs.js:87:29
    at _rejected (/home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:808:24)
    at /home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:834:30
    at Promise.when (/home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:1079:31)
    at Promise.promise.promiseDispatch (/home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:752:41)
    at /home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:574:44
    at flush (/home/oshirowanen/.cordova/lib/android/cordova/3.3.0/bin/node_modules/q/q.js:108:17)
    at process._tickCallback (node.js:415:13)

    at /usr/lib/node_modules/cordova/src/platform.js:244:30
    at ChildProcess.exithandler (child_process.js:641:7)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at maybeClose (child_process.js:735:16)
    at Socket.<anonymous> (child_process.js:948:11)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Pipe.close (net.js:466:12)

Anyone know why this is happening and how to resolve it?

I get the same issue with PhoneGap.

oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • Same error/question [cordova platform add android not working while listing Android targets](http://stackoverflow.com/questions/20323787/cordova-platform-add-android-not-working-while-listing-android-targets) – givanse Feb 05 '14 at 14:31
  • possible duplicate of [Cordova platform add android error](http://stackoverflow.com/questions/20897115/cordova-platform-add-android-error) – Alexandros Spyropoulos Apr 06 '14 at 16:35

11 Answers11

89

It's possible that the Android tools binaries are not in your path.

According to the PhoneGap docs (http://docs.phonegap.com/en/2.9.0/guide_getting-started_android_index.md.html), you need to include the Android SDK's tools and platform-tools in your PATH environment variable.

export PATH=${PATH}:/Development/adt-bundle/sdk/platform-tools:/Development/adt-bundle/sdk/tools

where Development is the path you installed your SDK.

TomL
  • 2,724
  • 1
  • 17
  • 7
  • FYI, I am on windows and I added both of these folders to my path. However, Windows trims the path variable at a certain length, so the full folder paths weren't being included. Moved the adt stuff into "c:\adt" to shorten up the paths and it worked. – Jacob Adams Dec 19 '13 at 16:54
  • Thanks! This worked for me (after spending to many hours debuggin what exactly went wrong..). – Dillen Meijboom Jan 29 '14 at 15:30
  • Thanks you so much that's the solution to my problem. Thanks again. – emaleavil May 15 '14 at 09:51
  • If you're using android studio, use this: export PATH=${PATH}:/opt/android-studio/sdk/tools – Nick Aug 09 '14 at 08:31
  • I set PATH etc and still get this error. Please help here: http://stackoverflow.com/questions/26783594/phonegap-cordova-build-android-node-modules-q-q-js-throw-e – mylord Nov 06 '14 at 15:57
10

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.

Llewellyn Collins
  • 2,243
  • 2
  • 23
  • 37
  • 1
    The original question was regarding Ubuntu, but this fixed the same issue for me on Windows 8. – BiscuitBaker Apr 08 '14 at 11:37
  • 2
    This runs on Windows 8 perfectly. Thanks! – Tarik Jul 15 '14 at 16:33
  • I set PATH etc and still get this error. Please help here: http://stackoverflow.com/questions/26783594/phonegap-cordova-build-android-node-modules-q-q-js-throw-e – mylord Nov 06 '14 at 15:56
  • @mylord I know this sounds trivial, but did you close your cmd/terminal and try again? That got me confused for awhile – Llewellyn Collins Nov 06 '14 at 16:04
  • Yup, I just tried it.. same error. PATH=/Users/rover/Documents/Dev/Android/adt-bundle-mac-x86_64-20140702/sdk:/Users/rover/Documents/Dev/Android/adt-bundle-mac-x86_64-20140702/sdk/platform-tools:/Users/rover/Documents/Dev/Android/adt-bundle-mac-x86_64-20140702/sdk/build-tools – mylord Nov 06 '14 at 19:53
5

I'm running Ubuntu 12.04 and was having the same problem as you until a few minutes ago.

The two things I've noted that I had missed during installation:

  1. The Android version should be 19 and you should be able to type 'android' at the console and see some output or see a window pop-up. Make sure that you add the path to the folder where android.jar is located to your PATH variable, and set ANDROID_HOME to that same path as well. Typically, you would do this in your .bashrc file or in /etc/environment If you edit either of these files, you'll need to either 'source ~/.bashrc' or reopen your terminal to force it to reload the file. Type 'android' to make sure its working.

  2. I observed that the cordova setup script was attempting to write files into a subdirectory 'add-ons' of the Android SDK. That directory does not exist in the Android 19 distribution itself. Consequently, I created it manually and made it writeable by my user account.

ex: Android distribution path:

/opt/android/

The path that its trying to write to:

/opt/android/sdk/add-ons

Once I had done that I was able to get past the problem you've noted above.

  • 1
    I suggest that you rename the question to "Android, Cordova 3.3.0 - Can't create sub-project" or something similar. The current title is not descriptive, and many people appear to be having this problem. – evolvingfaster Jan 03 '14 at 08:58
1

It looks like the cordova andorid library file corrupted in your home directory.

Try to remove home//.cordova directory and do this again cordova platform add android in your cordova project.

Make sure you have sourced /platform-tools and /tools to PATH.

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
0

Android

The Android command-line tools are built upon shell scripts. You must have the Android SDK's tools and platform-tools folders in your PATH!

Create a project

Run the create command with the following parameters:

Path to your new Cordova Android project

Package name, following reverse-domain style convention

Main Activity name

$ /path/to/cordova-android/bin/create /path/to/my_new_cordova_project com.example.cordova_project_name CordovaProjectName
Darshan
  • 2,272
  • 3
  • 31
  • 43
0

It looks like this error could also be caused by the absence of a compatible Android SDK version (at the moment 4.2).

rcason
  • 211
  • 2
  • 6
0

I use cordova. 2days I have same problem"java,ant,android,cordova,command test is ok, but run cordova platform add android show me Error: An error occurred while listing Android targets". Now I fixit use: "chmod 777 ant and android sdk place" ,then it will work. I use ubuntu 14.04 32bit,

0

if the problem still exist please make sure that you have the right permissions try

sudo chmo-R 777 /home/your-user-name/.cordova/

Also make sure that you are NOT using sudo to add Android as a platform

This is incorrect in the default Cordova installation

sudo cordova platform add android

Just do

cordova platform add android
Khaled Awad
  • 1,644
  • 11
  • 24
0

I've searched a lot for this, and what worked for me was setting the shell variable $ANDROID_HOME to the path of the sdk/ folder in my adt-bundle directory from android. Use export to do this:

export ANDROID_HOME="/Users/YOUR_USER/Developer/adt-bundle/sdk/" but be sure to change the directory to match yours.

Good Luck!

0

Not sure if someone else had a problem with this, but when removed and added my platform again, I realized in my config.xml I had <icon src='...'> and the path inside the src was wrong.

Here was the error output I received when I added the platform again:cp: no such file or directory: C:\Users\{pathtofile}.png.

After I fixed the path, it worked fine.

superjisan
  • 1,604
  • 14
  • 15
-2

It can be that your cordova installation have been corrupted try to remove your .cordova folder "rm -R ~/.cordova" And "run cordova platform add android" again

Erik Axelsson
  • 401
  • 1
  • 4
  • 6