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

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


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


下载安装Java环境。


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


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


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


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


这里可以选择图标。


选择第一个

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


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



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


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


修改Java代码:

  1. package com.myexample.helloworld; 
  2.  
  3. import android.os.Bundle; 
  4. import org.apache.cordova.*; 
  5.  
  6. public class MainActivity extends 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. }*/ 
package com.myexample.helloworld;

import android.os.Bundle;
import org.apache.cordova.*;

public class MainActivity extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
    }
}

/*
 * 下面是adt生成的代码,注释掉
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}*/

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

AndroidManifest.xml


  1. <?xml version="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. <manifest xmlns: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-permission android:name="android.permission.CAMERA" /> 
  37.     <uses-permission android:name="android.permission.VIBRATE" /> 
  38.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
  39.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  40.     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
  41.     <uses-permission android:name="android.permission.INTERNET" /> 
  42.     <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
  43.     <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
  44.     <uses-permission android:name="android.permission.RECORD_VIDEO" /> 
  45.     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
  46.     <uses-permission android:name="android.permission.READ_CONTACTS" /> 
  47.     <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
  48.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
  49.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
  50.     <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
  51.     <uses-permission android: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.                 <action android:name="android.intent.action.MAIN" /> 
  65.  
  66.                 <category android: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> 
<?xml version="1.0" encoding="utf-8"?>
<!--
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myexample.helloworld"
    android:hardwareAccelerated="true"
    android:versionCode="1"
    android:versionName="1.0"
    android:windowSoftInputMode="adjustPan" >

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />

    <application
        android:debuggable="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.myexample.helloworld.MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

</manifest>

config.xml

  1. <?xml version="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.     <access origin="*" /> 
  40.  
  41.     <!-- <content src="http://mysite.com/myapp.html" /> for external pages --> 
  42.     <content src="index.html" /> 
  43.  
  44.     <preference 
  45.         name="loglevel" 
  46.         value="DEBUG" /> 
  47.     <!-- 
  48.       <preference name="splashscreen" value="resourceName" /> 
  49.       <preference name="backgroundColor" value="0xFFF" /> 
  50.       <preference name="loadUrlTimeoutValue" value="20000" /> 
  51.       <preference name="InAppBrowserStorageEnabled" value="true" /> 
  52.       <preference name="disallowOverscroll" value="true" /> 
  53.     --> 
  54.  
  55.     <feature name="App" > 
  56.         <param 
  57.             name="android-package" 
  58.             value="org.apache.cordova.App" /> 
  59.     </feature> 
  60.     <feature name="Geolocation" > 
  61.         <param 
  62.             name="android-package" 
  63.             value="org.apache.cordova.GeoBroker" /> 
  64.     </feature> 
  65.     <feature name="Device" > 
  66.         <param 
  67.             name="android-package" 
  68.             value="org.apache.cordova.Device" /> 
  69.     </feature> 
  70.     <feature name="Accelerometer" > 
  71.         <param 
  72.             name="android-package" 
  73.             value="org.apache.cordova.AccelListener" /> 
  74.     </feature> 
  75.     <feature name="Compass" > 
  76.         <param 
  77.             name="android-package" 
  78.             value="org.apache.cordova.CompassListener" /> 
  79.     </feature> 
  80.     <feature name="Media" > 
  81.         <param 
  82.             name="android-package" 
  83.             value="org.apache.cordova.AudioHandler" /> 
  84.     </feature> 
  85.     <feature name="Camera" > 
  86.         <param 
  87.             name="android-package" 
  88.             value="org.apache.cordova.CameraLauncher" /> 
  89.     </feature> 
  90.     <feature name="Contacts" > 
  91.         <param 
  92.             name="android-package" 
  93.             value="org.apache.cordova.ContactManager" /> 
  94.     </feature> 
  95.     <feature name="File" > 
  96.         <param 
  97.             name="android-package" 
  98.             value="org.apache.cordova.FileUtils" /> 
  99.     </feature> 
  100.     <feature name="NetworkStatus" > 
  101.         <param 
  102.             name="android-package" 
  103.             value="org.apache.cordova.NetworkManager" /> 
  104.     </feature> 
  105.     <feature name="Notification" > 
  106.         <param 
  107.             name="android-package" 
  108.             value="org.apache.cordova.Notification" /> 
  109.     </feature> 
  110.     <feature name="Storage" > 
  111.         <param 
  112.             name="android-package" 
  113.             value="org.apache.cordova.Storage" /> 
  114.     </feature> 
  115.     <feature name="FileTransfer" > 
  116.         <param 
  117.             name="android-package" 
  118.             value="org.apache.cordova.FileTransfer" /> 
  119.     </feature> 
  120.     <feature name="Capture" > 
  121.         <param 
  122.             name="android-package" 
  123.             value="org.apache.cordova.Capture" /> 
  124.     </feature> 
  125.     <feature name="Battery" > 
  126.         <param 
  127.             name="android-package" 
  128.             value="org.apache.cordova.BatteryListener" /> 
  129.     </feature> 
  130.     <feature name="SplashScreen" > 
  131.         <param 
  132.             name="android-package" 
  133.             value="org.apache.cordova.SplashScreen" /> 
  134.     </feature> 
  135.     <feature name="Echo" > 
  136.         <param 
  137.             name="android-package" 
  138.             value="org.apache.cordova.Echo" /> 
  139.     </feature> 
  140.     <feature name="Globalization" > 
  141.         <param 
  142.             name="android-package" 
  143.             value="org.apache.cordova.Globalization" /> 
  144.     </feature> 
  145.     <feature name="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> 
<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->
<widget
    id="com.myexample.helloworld"
    version="2.0.0"
    xmlns="http://www.w3.org/ns/widgets" >

    <name>
helloworld
    </name>

    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>

    <author
        email="dev@cordova.apache.org"
        href="http://cordova.io" >
        Apache Cordova Team
    </author>

    <access origin="*" />

    <!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
    <content src="index.html" />

    <preference
        name="loglevel"
        value="DEBUG" />
    <!--
      <preference name="splashscreen" value="resourceName" />
      <preference name="backgroundColor" value="0xFFF" />
      <preference name="loadUrlTimeoutValue" value="20000" />
      <preference name="InAppBrowserStorageEnabled" value="true" />
      <preference name="disallowOverscroll" value="true" />
    -->

    <feature name="App" >
        <param
            name="android-package"
            value="org.apache.cordova.App" />
    </feature>
    <feature name="Geolocation" >
        <param
            name="android-package"
            value="org.apache.cordova.GeoBroker" />
    </feature>
    <feature name="Device" >
        <param
            name="android-package"
            value="org.apache.cordova.Device" />
    </feature>
    <feature name="Accelerometer" >
        <param
            name="android-package"
            value="org.apache.cordova.AccelListener" />
    </feature>
    <feature name="Compass" >
        <param
            name="android-package"
            value="org.apache.cordova.CompassListener" />
    </feature>
    <feature name="Media" >
        <param
            name="android-package"
            value="org.apache.cordova.AudioHandler" />
    </feature>
    <feature name="Camera" >
        <param
            name="android-package"
            value="org.apache.cordova.CameraLauncher" />
    </feature>
    <feature name="Contacts" >
        <param
            name="android-package"
            value="org.apache.cordova.ContactManager" />
    </feature>
    <feature name="File" >
        <param
            name="android-package"
            value="org.apache.cordova.FileUtils" />
    </feature>
    <feature name="NetworkStatus" >
        <param
            name="android-package"
            value="org.apache.cordova.NetworkManager" />
    </feature>
    <feature name="Notification" >
        <param
            name="android-package"
            value="org.apache.cordova.Notification" />
    </feature>
    <feature name="Storage" >
        <param
            name="android-package"
            value="org.apache.cordova.Storage" />
    </feature>
    <feature name="FileTransfer" >
        <param
            name="android-package"
            value="org.apache.cordova.FileTransfer" />
    </feature>
    <feature name="Capture" >
        <param
            name="android-package"
            value="org.apache.cordova.Capture" />
    </feature>
    <feature name="Battery" >
        <param
            name="android-package"
            value="org.apache.cordova.BatteryListener" />
    </feature>
    <feature name="SplashScreen" >
        <param
            name="android-package"
            value="org.apache.cordova.SplashScreen" />
    </feature>
    <feature name="Echo" >
        <param
            name="android-package"
            value="org.apache.cordova.Echo" />
    </feature>
    <feature name="Globalization" >
        <param
            name="android-package"
            value="org.apache.cordova.Globalization" />
    </feature>
    <feature name="InAppBrowser" >
        <param
            name="android-package"
            value="org.apache.cordova.InAppBrowser" />
    </feature>
    <!-- Deprecated plugins element. Remove in 3.0 -->
    <plugins>
    </plugins>

</widget>

然后,就可以运行了


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值