How to Get a PhoneGap Project to Run in Android Studio with Gradle Build System

29,927

Solution 1

I managed to do this.

You need Android Studio and the Eclipse ADT version, as well as Cordova/PhoneGap all set up.

  1. Import the Cordova project into Eclipse.
  2. Go to File -> Export... -> Generate Gradle Build Files.
  3. Click next to get past the "Import Instead?" screen.
  4. Select both your Android project and the CordovaLib project to export and click Next.

  5. Once this completes, open Android Studio.

  6. Go to File -> Import Project...
  7. Select the build.gradle file for the main Android project, which was generated by Eclipse, and click OK.
  8. After the import, you may get some warnings about a newer gradle version in use, just check your settings and it seems to work itself out.

  9. At this point, you should have a project structure that is your main project, but with CordovaLib as a module.

Now you can open the build.gradle file in the main project directory and change it to this:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.11.+'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':CordovaLib')
    compile 'com.android.support:appcompat-v7:19.+'
}

You should now be able to convince Android Studio to compile.

An extra tip would be to create a script to run "cordova prepare" and add that to the module's run configuration as an external tool. Make sure to synchronise the whole project before deploying the APK to a device or emulator.

Solution 2

I am new to Android Studio and still getting used to the AS project structure and Gradle.

Using Cordova 4.1.2 Android Studio 1.0.1

1) I created the app using the Cordova CLI:

cordova create CordovaAndroidApp

cd CordovaAndroidApp

cordova platform add android

this version of Cordova created the build.gradle and settings.gradle files.

2) From Android Studio splash screen I selected "Start a new Android Studio project" On the second screen I checked the Phone and Tablet box; on the third screen, I chose "Add No Activity"

3) In this new Android Studio application, from the Project view in the left panel with the top level of the project selected, I selected File -> Import Project. In the popup "select Eclipse or Gradle Project to Import", I chose to the Cordova project directory, clicked down to the platforms / android directory, and selected the build.gradle file, then OK.

I was able to build and run the basic Cordova project (just the splash screen) with no problem.

Share:
29,927
Matt Whetton
Author by

Matt Whetton

UK based .NET, Web and Android Programmer, sole writer of http://www.codenutz.com , trying to make it in the startup world and gain financial independence. Also run http://www.itsmonkie.co.uk doing cross platform app development in the UK

Updated on July 18, 2020

Comments

  • Matt Whetton
    Matt Whetton almost 4 years

    I'm trying to get a new PhoneGap application setup and running inside Android Studio with the Gradle build system.

    At the moment I have successfully created the PhoneGap project and imported into Android Studio. It all appears to be working fine, but I cant work out how to move it to the Gradle build system, or even if its possible.

    Can anybody help?