Recently I’ve been using PhoneGap as a framework for building mobile apps. What I like about PhoneGap is that if done in the right way I can build an app fairly quickly for almost any mobile device. In this walkthrough I’m going to cover how to get started using PhoneGap with Android Studio:
For more details about PhoneGap see my article covering PhoneGap Fundementals
Step 1 – Install Prerequisites and Tools
The very first step is to install all prerequisites and tools, these are:
- Install the Java Development Kit: Download the Java Development Kit and install it.
- If on windows, Set up the JAVA_HOME system variable:
- Select Start menu > Computer > System Properties > Advanced System Properties.
- Open Advanced tab > Environment Variables.
- Add a new system variable JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.7.0_21.
- Add the JAVA_HOME bin path system variable to the PATH variable:
- Select Start menu > Computer > System Properties > Advanced System Properties.
- Open Advanced tab > Environment Variables.
- Select the PATH system variable
- Click edit
- Add the following to the end of the variable: “;%JAVA_HOME%\bin”
- Install Android Studio: follow the instructions on the android site to install android studio.
- Ensure you have the android sdk tools and platform-tools folder in your PATH variable
- Select Start menu > Computer > System Properties > Advanced System Properties.
- Open Advanced tab > Environment Variables.
- Select the PATH system variable
- Click edit
- Add the following to the end of the variable: “;%Android SDK%\platform-tools;%Android SDK%\tools” where %Android SDK% is the path to your android sdk location, normally “C:\Program Files (x86)\Android\android-studio\sdk”
- Download and install NodeJS from the NodeJS Website
- Install PhoneGap: Install PhoneGap by executing the following command from the command line: “npm install -g phonegap”
Step 2 – Create a PhoneGap Android Project
Next we need to create our PhoneGap project with the android wrapper:
- Run the PhoneGap create command: Run “phonegap create ProjectPath PackageName ProjectName” where
- ProjectPath is the path to the folder you want to create (with the PhoneGap Contents)
- PackageName is the reverse-domain qualified project identifier, e.g. com.codenutz.myapp
- ProjectName is the name of your project
- Switch your command line directory to your project directory: Run “cd ProjectPath”
- Build for the Android platform: Run “phonegap build android” (This is important as it adds the android target to the PhoneGap project)
- Import your project into Android Studio
- Open Android Studio and Choose “Import Project…”
- Navigate to the following directory “platforms/android” within your project path.
- Click OK
- Select “Create project from existing sources”
- Click Next
- Enter your project name and click next
- Next you need to deselect the sources in the CordovaLib directory
- Click Next
- Next deselect the dexedLibs directory
- Click Next through all the remaining screens and select finish.
You should now be able to compile, run and deploy to android devices inside Android Studio.
Step 3 – Add the PhoneGap www directory into the project
Now if we want to edit the phonegap html, css and javascript files from within Android Studio we need to add the PhoneGap www directory into the project. Do not however that there is no IDE support for these types of file, so Android Studio will effectivley be a glorified text editor:
- Go to File > Project Structure
- Select “Modules” in the far left column, and ensure the “Sources” tab is selected in the main pane on the right:
- Click the “Add Content Root” button in the far right column
- Select the “www” folder located directly under your PhoneGap project folder
- Click OK
- In the pane just to the left select the folder and mark it as “Sources”
- Click OK
Now you should be able to see the www folder within your project structure, allowing you to edit the source files from with Android Studio.
Wrapping Up
One big caveat with this is that when you’re working with your project in this your cannot use the Gradle build system. At this stage it does not appear that the Gradle build system will support the project structure of the PhoneGap project, so converting doesn’t seem toi be an option. I will keep my eye on this and as soon as I can get the project working with Gradle I’ll update this post.
Now that you have a way to work with your PhoneGap in an Android friendly environment, you can start to develop your app!