Import Cordova project in Android Studio - android

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?

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

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).

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

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.

Related

Gradle version to use with Android/Cordova CLI

I currently use Cordova CLI on Windows to build hybrid Android apps. I am in the process of moving my development mover to a headless Ubuntu server. Getting java, NVM, Node, Android SDK and Cordova CLI installed on Ubuntu has not been a problem. Having done so and created the customary Cordova hello world project,
cordova create hello com.example.hello HelloWorld
I proceeded to add the Android platform
cordova plaform add android
and then attempted to build
cordova build android
at which point I was told
Error: Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle
in your path, or install Android Studio
Installing Gradle by following the instructions given here - I am NOT installing Android Studio and am working on a headless Ubuntu machine so automatic Gradle installation is not an option - is not a problem. However, it is not clear to me which version of Gradle I should install.
I do not want to break my current Cordova project so I am trying to replicate the current Windows/Cordova CLI setup as closely as I can
Node 8.9.2
Java 8
Gradle ???
Dipping inside the gradle-wrapper.properties file in my existing project I find
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
I have noted that when I compile the project at present there are some ominous warnings about features to be removed from future versions of Gradle that are shown. I could experiment with using gradle-2.10 but it is not entirely clearr to me that it would indeed be the right version. Hopefully, someone here will be able to point me in the right direction.
First, clean up the project
cordova clean android
Then, force the cordova to use the distribution url with the version you want, in this case: gradle-6.3
export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=https\\://services.gradle.org/distributions/gradle-6.3-all.zip
Then, build your application.
cordova build android --release

Where to place jars in Cordova app developed in Visual Studio?

I am trying to create cordova app in visual studio, because I just want to explore. I have very good idea on Cordova CLI, eclipse and Android Studio environments. My goal is to add my own custom plugin to the Visual Studio Cordova project and execute my existing code.
With the help of below link, I successfully added my custom plugin.
https://taco.visualstudio.com/en-us/docs/manage-plugins/#Custom
Apart from js files, my custom plugin also needs some native(android) jars, which I am going to add manually in Eclipse/Android Studio.
My problem here is - I don't find any libs folder in Visual Studio environment (as I am newbie), can some one let me know where exactly I need to required jars.

Import a Cordova ionic project into Android studio?

I'm trying to import an Cordova Ionic project in Android Studio to modify some things and try to solve some bugs but I can't.
I never work with Ionic before and I tried to follow some tutorials about how to import Ionic into Android but no one works for me.
In all of them you have to go to Android Studio > Import Project (Eclipse ADT, Gradle, etc.) and then select a folder called platform and then the Android Gradle.
But the thing is that in my project I only have this folders: hooks, css, www.
My highlighters to the problem above are:
To open a Cordova for Android project in Android Studio:
Launch Android Studio.
Select Import Project (Eclipse ADT, Gradle, etc).
Select the Android platform directory in your project (/platforms/android).
For the Gradle Sync question you can simply answer Yes.
Once it finishes importing, you should be able to build and run the app directly from Android Studio. See Android Studio Overview and Building and Running from Android Studio for more details.
Refs:
https://cordova.apache.org/docs/en/latest/guide/platforms/android/
https://www.codeproject.com/articles/1068176/step-by-step-guide-to-build-ionic-hybrid-app-using
For the theory behind the bove logic and possible bunders you may make, please see my answer to the following question:
Building Ionic framework in android studio
You dont have to import the Android Gradle. You have to previously do a
ionic platform add android
ionic build android
and then import your platforms/android folder
- In Android Studio
Choose File->Settings
Navigate to Plugins Tab
Choose Install JetBrains Plugins
Search for Phonegap/Cordova Plugin
Click On Install the Plugin
And Restart Android Studio
- Open IONIC Project in any editor
Run this Command
ionic cordova prepare android
It will Prepares the Ionic Project for Android
Then run this command -
ionic cordova build android
It will Builds the Ionic Project for Android.
3.Open Android Studio
Choose File > New Project > Import Project > Choose the android folder of cordova
directory > ok
Run the project with Android studio.

Building Ionic framework in android studio

I'm trying to create a hybrid application through the IONIC framework which displays multiple moving graphics implemented through HTML5 Canvas and JS. But, the animation renders very slowly in mobiles (even those with 1GB RAM).
We have identified and downloaded FastCanvas (Cordova/PhoneGap plugin). We followed the instructions given in the URL - https://github.com/phonegap/phonegap-plugin-fast-canvas. But under "Adding FastCanvas to Your Application" we hit a roadblock when we come to instructions 3 and 4 as there are no Java folders in my project.
The example (HTML5 Game) given in the url is done through PhoneGap which has the mentioned folder structure and it works fine when we build through Android studio.
We have tried to build our project (done through ionic framework) through Android Studio but, gradle build system was unable to identify the framework. Request someone to please guide me further to solve this issue since, I'm struggling to come up with a solution for long.
Note: We have already tried Canvas, JS, CSS3 and wizCanvas plugin for the animation but, the output is same - slow.
Whoo-hoo.
After a long struggle, this is what finally worked for me:
Opening a Project in Android Studio
Cordova for Android projects can be opened in the Android IDE, Android Studio. This can be useful if you wish to use Android Studio's built in Android debugging/profiling tools or if you are developing Android plugins. Please note that when opening your project in Android studio, it is recommended that you do NOT edit your code in the IDE. This will edit the code in the platforms folder of your project (not www), and changes are liable to be overwritten. Instead, edit the www folder and copy over your changes by running cordova build.
Plugin developers wishing to edit their native code in the IDE should use the --link flag when adding their plugin to the project via cordova plugin add. This will link the files so that changes to the plugin files in the platforms folder are reflected in your plugin's source folder (and vice versa).
To open a Cordova for Android project in Android Studio:
Launch Android Studio.
Select Import Project (Eclipse ADT, Gradle, etc).
Select the Android platform directory in your project (/platforms/android).
For the Gradle Sync question you can simply answer Yes.
(If prompted to update gradle from v2.3 to 3.3. click Update)
Once it finishes importing, you should be able to build and run the app directly from Android Studio. See Android Studio Overview and Building and Running from Android Studio for more details.
Refs:
https://cordova.apache.org/docs/en/latest/guide/platforms/android/
https://www.codeproject.com/articles/1068176/step-by-step-guide-to-build-ionic-hybrid-app-using

Phonegap 3.3 installation issues

I'm having some issues with the new phonegap installation, using nodejs. I've managed to get everything working, set the path to ant, android sdk, java jdk etc., everything works perfectly.
With the usage of the following commands I've managed to create a new project:
cordova create hello com.example.hello HelloWorld
However, when I've examined the project, it was empty:
So I went into the "hello" folder and added an Android platform in the following manner:
cordova platform add android
and then I've build it:
cordova build android
So, after that, there were some android specific folders, as shown below:
Is this how the structure is suppose to look like? I'm having REAL problems importing this into Android Studio. However, I managed to import the project into eclipse, but now I have two projects, first is named "HelloWorld" and the other "HelloWorldCordovaLib". This is a bit confusing. While it works, why are there two projects? Am I doing something wrong?
Moreover, when I import this project into android studio, I get a bunch of errors (whereas the same project works in eclipse): Errors with the structure
This is seems very, very confusing. I have yet to understand why Phonegap is suddenly so hard to install. I hope I'm missing something. Any kind of feedback is welcome. Thanks.
Android Studio, amazingly, can't import Android projects that don't have the Gradle project structure. Until Cordova starts generating Gradle files, or Android Studio completes the importer's types of projects it'll support, you have to export the project from Eclipse before you can import it in to Android Studio.
It's perfectly valid to have two Projects in one Cordova application. Usually, it's because your app references the Cordova project as a sort of "sub-project" (not the right term, but I'm not sure what Android calls it).
The structure is correct.
when giving the command cordova platform add android you are actually adding a folder named android inside platform folder. For android development you have to work on the files inside platforms/android.
Read more here Phonegap - Command line interface

Categories

Resources