Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 415
Matt Whetton Avatar asked Jan 09 '14 21:01

Matt Whetton


People also ask

How do I sync a project with gradle?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How do I add a project in build gradle?

Open Android Studio and choose "Import project" and choose to use Gradle as the external model. Pick your settings. gradle file as the Gradle project. [Optional] Set your "Gradle home" folder (so the text turns black instead of gray).

What is gradle build running in Android Studio?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.


2 Answers

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.

like image 58
springogeek Avatar answered Oct 31 '22 12:10

springogeek


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.

like image 24
ReedAccess Avatar answered Oct 31 '22 13:10

ReedAccess