29

I am trying to create a Cordova project. After creating the project cordova create myProject I would like to open it in Android Studio. The problem is ... it doesn't work.

The CordovaLib will not build with various errors package android.* does not exist.

Does anyone know how to import a cordova project in Android Studio?

andrei
  • 2,934
  • 2
  • 23
  • 36
  • did you issue the commands `add platform...` and `build..` command before opening the project in android? – frank Jul 24 '14 at 14:47
  • @frank I did add platform, not the build. I found out what the problem was, it did not import the manifest for CordovaLib. I saw that folders like hooks, www, plugins, platforms/android/cordova, platforms/android/platform_www were not imported as well. Do you know how I can import them? Where to place them in the android studio project? – andrei Jul 24 '14 at 14:54
  • I am not familiar with android studio? But in Eclipse(kepler version) I just had to point to the android platform folder created by `add platform` command and it worked without any issues. – frank Jul 24 '14 at 15:08

4 Answers4

34

Unfortunately the accepted answer is a bit out of date. Using Cordova v5.3.3 (it probably works on all versions > 5) it is much the same process a building and then entering XCode for an iOS application now - the build system has moved to gradle.

Before opening in Android Studio

cordova build android

Then just open up the project using File > Open and pointing to the (yourProjectDir)/Platforms/Android directory.

If you are using an older version of the cordova android platform you might need to run

cordova platform android update

To get moved to the gradle build system which is compatible with the current version of Android Studio

Ryan Knell
  • 6,204
  • 2
  • 40
  • 32
  • Great answer but not very intuitive. Cordova for Android is quite a nightmare. – Marc Mar 14 '16 at 12:37
  • I am using the newest version of Android Studio (version 2.2 Preview 7) and when I try to import my project, literally nothing happens. I press 'ok' and it just brings me back to the "Welcome to Android Studio" screen. Any idea what could be wrong? – tommybond Aug 04 '16 at 16:36
  • 1
    @tommybond I have this same issue. Did you figure it out? – Trevor Panhorst Sep 15 '16 at 16:34
  • Unfortunately @TrevorPanhorst I did not :( – tommybond Sep 21 '16 at 19:52
  • @tommybond I was about to resolve this by updating my packages through Android – Trevor Panhorst Sep 21 '16 at 19:54
  • @Ryan, i am getting error "org.jetbrains.plugins.settings.GradleSystemSettings cannot be cast to org.jetbrains.plugins.gradle.settings.GradleSystemSettings" while opening (yourProjectDir)/Platforms/Android directory File > Open of Android Studio. Any idea, what i am missing? – Santosh Prasad Sah Apr 03 '17 at 12:03
  • @tommybond I am facing the same issue as the project doesn't open in studio, do you have any solution for that ? – Yesha Shah Mar 29 '18 at 05:43
  • This is kind of a side topic but was directly related for me. Once I opened the app in Android Studio per the above instructions I was prompted to update Gradle. I said "OK" at first and then my app stopped building successfully (OMG). I then "cordova platform remove android", "cordova platform add android" and then said "NO" to the Gradle update and my project is still building successfully. So I guess don't let Android Studio mess w/your Gradle if you can avoid it. – Christopher Mar 04 '19 at 17:37
16

Make sure you import the "platform/android" directory underneath your cordova project (and you want to Import Project, not Open Project).

You will at least need to run

cordova prepare android

before doing the import

cordova build android 

will also work, but it will create some ant directories which will not be used by Android Studio and you will have to actively ignore the files when importing otherwise you will get extra libraries in your project). Although I haven't imported a Cordova app into android studio recently, it definitely works in the Community edition of Intellij which Android Studio is based on (I did it yesterday with a project built from scratch with Cordova 3.5). I can't think of any of the additional features in Android Studio that would be useful that aren't in Intellij, as most of the additions which haven't been backported to Intellij are in the preview space which isn't going to work with Cordova anyway (all it is going to preview is a blank webkit view).

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • 3
    This isn't enough. Something breaks. Android studio restructures the project and builds don't work (using Cordova v3.5). I'm not familiar with android or android studio so repairing the project configuration or setting up a proper gradle build is beyond me at this point. I'll just go back to eclipse. It looks like work is under active development to support gradle builds in cordova https://issues.apache.org/jira/browse/CB-3445 – kindasimple Sep 02 '14 at 18:09
  • Do you know where I can download Eclipse? I need to use that instead of Android Studio but there are so many versions... – jagershark Jan 20 '15 at 17:20
  • Thank you, but unfortunately I can't seem to find the download link. Android Studio seems to be promoted massively and they have stopped providing eclipse downloads... – jagershark Jan 22 '15 at 10:22
  • @loxyboi http://developer.android.com/sdk/installing/installing-adt.html for the ADT plugin and eclipse at https://eclipse.org/ – andrei Jan 23 '15 at 10:21
1

All the answers above seem to refer to the cli. However, to actually have your project in android studio so that you can harness the power of the android studio, this is what I would suggest you do:

Please see my most relevant answer here...

Building Ionic framework in android studio

Community
  • 1
  • 1
0

We have some troubles on importing Ionic project to android studio because we have add android platform with SUDO command, and because of it, android studio dont have access privileges to read files. In my case just do sudo chmod -Rf 777 ionicFolder android studio can import project successful. I hope this help some one with this problem.

Marcelo Tadeu
  • 369
  • 3
  • 5