Can't add module dependency to CordovaLib for Cordova project using Android Studio
Asked Answered
J

2

5

I can't workout how to add the CordovaLib directory as a module dependency in Android Studio.

(NOTE: this is the first time I've ever used Android Studio, so possibly I just don't know what I'm doing)

I'm using:

  • OSX Yosemite
  • Android Studio 1.1.0
  • Cordova 4.3

Here are the steps I have taken:

  1. Create new cordova project
  2. Add android platform and some cordova plugins
  3. Import the generated project into Android Studio (NOTE: when it asks about the gradle wrapper, I say no and just point it to where I have gradle installed - /usr/local/Cellar/gradle/2.2.1/libexec)
  4. Try to build - at this point it will complain for several of the Cordova plugins that cannot find symbol class CordovaPlugin

Obviously it doesn't know about the CordovaLib subproject. This is what I'm trying to fix.

I can't work out how to tell the Android Studio that CordovaLib is a module dependency.

I've gone to the Project Structure window, but can't see any way to link to CordovaLib.

Adding module dependencies in Android Studio

Clicking the "+" buttons does not do anything. I can't work out if I'm doing something wrong, or there are bugs in Android Studio when importing the project...

Can someone please confirm if I'm doing something wrong?

Or please confirm if they are able to import Cordova generated projects into Android Studio, using Cordova 4.3 and Android Studio 1.1.0.

Thanks!

Jarvisjary answered 14/3, 2015 at 11:57 Comment(4)
cordova won't support android studio yet, they will add the support on cordova android 4.0Hertzog
@Hertzog - where are you getting that from? As far as I can see, there is Android Studio support now. Cordova CLI generates a project structure that works with Gradle as well as the .gradle build script. It imports into Android Studio fine and it should be working. My issue appears to be with Android Studio itself - it seems like there is something wrong with the "Project Structure" dialog when trying to setup the module dependencies.Jarvisjary
@Hertzog - actually you're right. When using the cordova-android from the master branch, it correctly sets up CordovaLib as a module dependency automatically. Don't know why I couldn't do it manually though...Jarvisjary
I've read this on the cordova developer mail listHertzog
J
7

First, thanks to @jcesarmobile for putting me on the right track.

Here is how to use the latest cordova-android directly (4.0.0-dev) from Github with Android Studio:

  1. Clone the cordova-android repo:

    git clone [email protected]:apache/cordova-android.git
    
  2. Add the platform to your cordova project

    cordova platform add /path/to/cloned/repo/cordova-android
    
  3. Build the project

    cordova build android
    
  4. Import into Android Studio as a non-android-studio project

Jarvisjary answered 14/3, 2015 at 23:44 Comment(6)
I couldn't get anything older than cordova-android 4.0.0-dev working in Android Studio. It's probably possible, but was taking way to much time so I gave up. I updated my projects to cordova-android 4.0.0-dev, and found it pretty stable. The only gotcha I found was the new "whitelist" functionality, which requires the cordova-plugin-whitelist plugin installed in your project.Jarvisjary
how do I update to dev version? In this way? cordova platform update /path/to/cloned/repo/cordova-android I'm scared to use a develop version... :) thanks for the feedback! ByeJuvenescence
Yes, if you add cordova-android directly from the github repo, then you will be using the dev version. (which is currently 4.0.0-dev). You can also do it this way: cordova platform add https://github.com/apache/cordova-android.gitJarvisjary
I found out, that when you import your whole cordova project, not only the platforms/android folder, the Cordova libs are included. But after that, Android Studio says, that it doesn't use gradle for building. But after all, you can build your project. What do you think? Is this the right approach?Isisiskenderun
@23tux, hmm not sure if I understand completely. If you mean that you added the root cordova directory to Android Studio - that wouldn't work well - you should only add the generated android project in platforms/android. Adding the generated project will also include the Cordova Lib sub-project, which Android Studio should automatically configure as a dependency. From that point, Android Studio should use Gradle for building - you want that, because the Cordova people have provided a build.gradle file, and we want to use their build process.Jarvisjary
Cordova-Android 4.0 is released now so you can get that from git if you're nervous about getting the latest dev build. Don't try to skip the command line build, it won't work; something in the build process completes the gradle setup for Android Studio. And beware the whitelist as noted by @Jarvisjary -- no ajax calls or external resources will work unless you install the new plugin from github.com/apache/cordova-plugin-whitelist.git. The new plugin is not in the registry so you have to install from git.Stylistic
S
7

I had the same problem, is because there are error to load the module CordovaLib. You have to make this two steps:

1.- In root file (Android) add a file named "settings.gradle" with the content:

include ':CordovaLib'
project(':CordovaLib').projectDir = new File('CordovaLib')

2.- Go to file "build.gradle" and find this part:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    // SUB-PROJECT DEPENDENCIES START
    // SUB-PROJECT DEPENDENCIES END
}

and change it to:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':CordovaLib')
    // SUB-PROJECT DEPENDENCIES START
    // SUB-PROJECT DEPENDENCIES END
}

Now you just have to Run the project.

Substrate answered 21/4, 2015 at 21:47 Comment(1)
settings.gradle is Auto-Generated file.Hiltner

© 2022 - 2024 — McMap. All rights reserved.