Cordova deploy on Android
网上找了几篇Phonegap在安卓上的部署,版本都比较老了,不过还是部署成功了,
写篇博客以做纪录。
1.先下载IDE:戳
2.下载Phonegap:戳
3.启动ADT,新建普通Android Application Project
4.在Package Explorer的项目项下
assets目录下新建www文件夹,
res目录下新建xml文件夹
5.将下载的Phonegap解压,
cordova.js copy to www folder,
cordova-2.9.0.jar copy to libs folder,
config.xml copy to res/xml folder.
6.项目属性-> Java Build Path,
Libraries -> Add Jars, Add /libs/cordova-2.9.0.jar,
Order and Export -> 勾选cordova-2.9.0.jar.
7.Open /src/com.package.projectname/MainActivity.java
里面代码全部del,贴入
1 package com.package.projectname; 2 3 import android.os.Bundle; 4 import org.apache.cordova.*; 5 6 public class MainActivity extends DroidGap { 7 8 @Override 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 super.loadUrl(Config.getStartUrl()); 12 } 13 }
这里主要是引用cordova的package,把默认的android的activity去掉。改继承自activity为DroidgGap.
loadUrl里路径可以自己写,为了以后修改方便调用了Config.getStartUrl(),这样对应配置文件的key starturl了.
8.修改AndroidManifest.xml
activity标签处增加android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
增加uses-permission和supports-screens,如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.shgbit.gbconf" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 <supports-screens 7 android:largeScreens="true" 8 android:normalScreens="true" 9 android:smallScreens="true" 10 android:xlargeScreens="true" 11 android:resizeable="true" 12 android:anyDensity="true" 13 /> 14 15 <uses-permission android:name="android.permission.CAMERA" /> 16 <uses-permission android:name="android.permission.VIBRATE" /> 17 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 18 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 19 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 20 <uses-permission android:name="android.permission.INTERNET" /> 21 <uses-permission android:name="android.permission.RECEIVE_SMS" /> 22 <uses-permission android:name="android.permission.RECORD_AUDIO" /> 23 <uses-permission android:name="android.permission.RECORD_VIDEO"/> 24 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 25 <uses-permission android:name="android.permission.READ_CONTACTS" /> 26 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 27 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 28 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 29 <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 30 <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 31 32 <application 33 android:allowBackup="true" 34 android:icon="@drawable/ic_launcher" 35 android:label="@string/app_name" 36 android:theme="@style/AppTheme" > 37 <activity 38 android:name="com.shgbit.gbconf.MainActivity" 39 android:label="@string/app_name" 40 android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"> 41 <intent-filter> 42 <action android:name="android.intent.action.MAIN" /> 43 44 <category android:name="android.intent.category.LAUNCHER" /> 45 </intent-filter> 46 </activity> 47 </application> 48 49 <uses-sdk 50 android:minSdkVersion="8" 51 android:targetSdkVersion="17" /> 52 </manifest>
9.open res/xml/config.xml,确认starturl,如下
<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="index.html" />
10.open /assets/www folder,
add html,css,js and so on...
这里注意看到了/assets/www目录,你的web内容,js框架等等全部扔这里吧...
11.create virtual devices
12.Run,Android虚拟机根据机器性能不同启动时间长短不一,请耐心等待,如果成功,如图:
点击GBConf,如下:
看看Cordova如何调用的,如图:
Done!
最后,本篇初学笔记,仅供初学者学习方便之用,各位大牛请绕路~勿拍砖...