PhoneGap2.9.0本地将html打包成Android应用

来源:互联网 发布:大数据应用网站 编辑:程序博客网 时间:2024/05/22 15:38

PhoneGap的在线打包有大小限制,超过30M的包无法在线打包。当然,可以把包里面的图片、声音文件去掉,然后打包。下载以后,解包,重新打包并签名。蛮麻烦的。


本地打包的简单方法如下:


下载安装Java环境。


下载安装ADT。http://developer.android.com/sdk/index.html


打开ADT,新建一个安卓应用项目


输入名称啥的,然后就可以一路下一步


可以选择下项目位置,我的是默认的。


这里可以选择图标。


选择第一个

这个时候,一个安卓项目就建好了。这个时候运行,会看到默认的样子,不管他,无视。


将PhoneGap目录下的android目录下的jar文件拷贝到项目的libs目录下



将xml目录拷贝到项目的res目录下


在assetc目录下,建立一个www目录,下面放html内容。为了偷懒,我把phonegap例子里面的内容拷贝过来了。


修改Java代码:

[java] view plaincopyprint?
  1. package com.myexample.helloworld; 
  2.  
  3. import android.os.Bundle; 
  4. import org.apache.cordova.*; 
  5.  
  6. public class MainActivityextends DroidGap 
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) 
  9.     { 
  10.         super.onCreate(savedInstanceState); 
  11.         // Set by <content src="index.html" /> in config.xml 
  12.         super.loadUrl(Config.getStartUrl()); 
  13.         //super.loadUrl("file:///android_asset/www/index.html") 
  14.     } 
  15.  
  16. /*
  17. * 下面是adt生成的代码,注释掉
  18. import android.os.Bundle;
  19. import android.app.Activity;
  20. import android.view.Menu;
  21. public class MainActivity extends Activity {
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.     }
  27.     @Override
  28.     public boolean onCreateOptionsMenu(Menu menu) {
  29. // Inflate the menu; this adds items to the action bar if it is present.
  30.         getMenuInflater().inflate(R.menu.main, menu);
  31.         return true;
  32.     }
  33. }*/ 

修改一下项目根目录下的AndroidManifest.xml和res/xml目录下的config.xml文件

AndroidManifest.xml


[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <!-- 
  3.        Licensed to the Apache Software Foundation (ASF) under one 
  4.        or more contributor license agreements.  See the NOTICE file 
  5.        distributed with this work for additional information 
  6.        regarding copyright ownership.  The ASF licenses this file 
  7.        to you under the Apache License, Version 2.0 (the 
  8.        "License"); you may not use this file except in compliance 
  9.        with the License.  You may obtain a copy of the License at 
  10.  
  11.          http://www.apache.org/licenses/LICENSE-2.0 
  12.  
  13.        Unless required by applicable law or agreed to in writing, 
  14.        software distributed under the License is distributed on an 
  15.        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
  16.        KIND, either express or implied.  See the License for the 
  17.        specific language governing permissions and limitations 
  18.        under the License. 
  19.  
  20. --> 
  21. <manifestxmlns:android="http://schemas.android.com/apk/res/android" 
  22.     package="com.myexample.helloworld" 
  23.     android:hardwareAccelerated="true" 
  24.     android:versionCode="1" 
  25.     android:versionName="1.0" 
  26.     android:windowSoftInputMode="adjustPan"> 
  27.  
  28.     <supports-screens 
  29.         android:anyDensity="true" 
  30.         android:largeScreens="true" 
  31.         android:normalScreens="true" 
  32.         android:resizeable="true" 
  33.         android:smallScreens="true" 
  34.         android:xlargeScreens="true"/> 
  35.  
  36.     <uses-permissionandroid:name="android.permission.CAMERA"/> 
  37.     <uses-permissionandroid:name="android.permission.VIBRATE"/> 
  38.     <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/> 
  39.     <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/> 
  40.     <uses-permissionandroid:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> 
  41.     <uses-permissionandroid:name="android.permission.INTERNET"/> 
  42.     <uses-permissionandroid:name="android.permission.RECEIVE_SMS"/> 
  43.     <uses-permissionandroid:name="android.permission.RECORD_AUDIO"/> 
  44.     <uses-permissionandroid:name="android.permission.RECORD_VIDEO"/> 
  45.     <uses-permissionandroid:name="android.permission.MODIFY_AUDIO_SETTINGS"/> 
  46.     <uses-permissionandroid:name="android.permission.READ_CONTACTS"/> 
  47.     <uses-permissionandroid:name="android.permission.WRITE_CONTACTS"/> 
  48.     <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  49.     <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> 
  50.     <uses-permissionandroid:name="android.permission.GET_ACCOUNTS"/> 
  51.     <uses-permissionandroid:name="android.permission.BROADCAST_STICKY"/> 
  52.  
  53.     <application 
  54.         android:debuggable="true" 
  55.         android:hardwareAccelerated="true" 
  56.         android:icon="@drawable/ic_launcher" 
  57.         android:label="@string/app_name"> 
  58.         <activity 
  59.             android:name="com.myexample.helloworld.MainActivity" 
  60.             android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" 
  61.             android:label="@string/app_name" 
  62.             android:theme="@android:style/Theme.Black.NoTitleBar"> 
  63.             <intent-filter> 
  64.                 <actionandroid:name="android.intent.action.MAIN"/> 
  65.  
  66.                 <categoryandroid:name="android.intent.category.LAUNCHER"/> 
  67.             </intent-filter> 
  68.         </activity> 
  69.     </application> 
  70.  
  71.     <uses-sdk 
  72.         android:minSdkVersion="7" 
  73.         android:targetSdkVersion="17"/> 
  74.  
  75. </manifest> 

config.xml

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <!-- 
  3. Licensed to the Apache Software Foundation (ASF) under one 
  4. or more contributor license agreements.  See the NOTICE file 
  5. distributed with this work for additional information 
  6. regarding copyright ownership.  The ASF licenses this file 
  7. to you under the Apache License, Version 2.0 (the 
  8. "License"); you may not use this file except in compliance 
  9. with the License.  You may obtain a copy of the License at 
  10.  
  11. http://www.apache.org/licenses/LICENSE-2.0 
  12.  
  13. Unless required by applicable law or agreed to in writing, 
  14. software distributed under the License is distributed on an 
  15. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
  16. KIND, either express or implied.  See the License for the 
  17. specific language governing permissions and limitations 
  18. under the License. 
  19. --> 
  20. <widget 
  21.     id="com.myexample.helloworld" 
  22.     version="2.0.0" 
  23.     xmlns="http://www.w3.org/ns/widgets"> 
  24.  
  25.     <name> 
  26. helloworld 
  27.     </name> 
  28.  
  29.     <description> 
  30.         A sample Apache Cordova application that responds to the deviceready event. 
  31.     </description> 
  32.  
  33.     <author 
  34.         email="dev@cordova.apache.org" 
  35.         href="http://cordova.io"> 
  36.         Apache Cordova Team 
  37.     </author> 
  38.  
  39.     <accessorigin="*"/> 
  40.  
  41.     <!-- <content src="http://mysite.com/myapp.html" /> for external pages --> 
  42.     <contentsrc="index.html"/> 
  43.  
  44.     <preference 
  45.         name="loglevel" 
  46.         value="DEBUG"/> 
  47.     <!-- 
  48.       <preferencename="splashscreen"value="resourceName"/> 
  49.       <preferencename="backgroundColor"value="0xFFF"/> 
  50.       <preferencename="loadUrlTimeoutValue"value="20000"/> 
  51.       <preferencename="InAppBrowserStorageEnabled"value="true"/> 
  52.       <preferencename="disallowOverscroll"value="true"/> 
  53.     --> 
  54.  
  55.     <featurename="App"> 
  56.         <param 
  57.             name="android-package" 
  58.             value="org.apache.cordova.App"/> 
  59.     </feature> 
  60.     <featurename="Geolocation"> 
  61.         <param 
  62.             name="android-package" 
  63.             value="org.apache.cordova.GeoBroker"/> 
  64.     </feature> 
  65.     <featurename="Device"> 
  66.         <param 
  67.             name="android-package" 
  68.             value="org.apache.cordova.Device"/> 
  69.     </feature> 
  70.     <featurename="Accelerometer"> 
  71.         <param 
  72.             name="android-package" 
  73.             value="org.apache.cordova.AccelListener"/> 
  74.     </feature> 
  75.     <featurename="Compass"> 
  76.         <param 
  77.             name="android-package" 
  78.             value="org.apache.cordova.CompassListener"/> 
  79.     </feature> 
  80.     <featurename="Media"> 
  81.         <param 
  82.             name="android-package" 
  83.             value="org.apache.cordova.AudioHandler"/> 
  84.     </feature> 
  85.     <featurename="Camera"> 
  86.         <param 
  87.             name="android-package" 
  88.             value="org.apache.cordova.CameraLauncher"/> 
  89.     </feature> 
  90.     <featurename="Contacts"> 
  91.         <param 
  92.             name="android-package" 
  93.             value="org.apache.cordova.ContactManager"/> 
  94.     </feature> 
  95.     <featurename="File"> 
  96.         <param 
  97.             name="android-package" 
  98.             value="org.apache.cordova.FileUtils"/> 
  99.     </feature> 
  100.     <featurename="NetworkStatus"> 
  101.         <param 
  102.             name="android-package" 
  103.             value="org.apache.cordova.NetworkManager"/> 
  104.     </feature> 
  105.     <featurename="Notification"> 
  106.         <param 
  107.             name="android-package" 
  108.             value="org.apache.cordova.Notification"/> 
  109.     </feature> 
  110.     <featurename="Storage"> 
  111.         <param 
  112.             name="android-package" 
  113.             value="org.apache.cordova.Storage"/> 
  114.     </feature> 
  115.     <featurename="FileTransfer"> 
  116.         <param 
  117.             name="android-package" 
  118.             value="org.apache.cordova.FileTransfer"/> 
  119.     </feature> 
  120.     <featurename="Capture"> 
  121.         <param 
  122.             name="android-package" 
  123.             value="org.apache.cordova.Capture"/> 
  124.     </feature> 
  125.     <featurename="Battery"> 
  126.         <param 
  127.             name="android-package" 
  128.             value="org.apache.cordova.BatteryListener"/> 
  129.     </feature> 
  130.     <featurename="SplashScreen"> 
  131.         <param 
  132.             name="android-package" 
  133.             value="org.apache.cordova.SplashScreen"/> 
  134.     </feature> 
  135.     <featurename="Echo"> 
  136.         <param 
  137.             name="android-package" 
  138.             value="org.apache.cordova.Echo"/> 
  139.     </feature> 
  140.     <featurename="Globalization"> 
  141.         <param 
  142.             name="android-package" 
  143.             value="org.apache.cordova.Globalization"/> 
  144.     </feature> 
  145.     <featurename="InAppBrowser"> 
  146.         <param 
  147.             name="android-package" 
  148.             value="org.apache.cordova.InAppBrowser"/> 
  149.     </feature> 
  150.     <!-- Deprecated plugins element. Remove in 3.0 --> 
  151.     <plugins> 
  152.     </plugins> 
  153.  
  154. </widget> 

然后,就可以运行了


PhoneGap的官方方法不是这样的,是用命令行生成默认包的。但是要装好几个东西。具体可以看PhoneGap包里面的readme文档。

0 0
原创粉丝点击