Android WebView use packaged into a web app

Native app development costs and relatively high compared to the page, so more and more app using a web interface as, or even completely encapsulated into a website app, you can increase the speed of development, but also the basic cross-platform.

Android below to an example, a simple implementation WebView packaging site into the app ubuntu-14.04.4-desktop-amd64 environment.

 

Preparing the Environment

Development environment requires Java SDK ( official website to download ), Android SDK ( official website to download ).

Java SDK installation

wget http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jdk-8u91-linux-x64.tar.gz
tar -x -f
jdk-8u91-linux-x64.tar.gz

Then configure the PATH and JAVA_HOME

we ~ / .bashrc

Added in the last

JAVA_HOME = Export the JDK extracted directory
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin

 Save and exit vi, refresh configuration

source ~/.bashrc

Android SDK installation

ps: installation process may require a ladder, please bring your own.

wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar -x -f android-sdk_r24.4.1-linux.tgz
cd android-sdk-linux/tools

Look at what sdk version can be installed

./android list sdk

Then install the required version sdk

./android update sdk --no-ui --filter comma-separated sequence number to install

 

Project to establish and coding

Create a new directory anywhere, save the project, and then create a src directory for storing source files. Because the concept of Java packages, so into the src directory, according to the level of the package name, followed by the establishment of the appropriate directory, and then create Java source files, such as:

Copy the code
 1 package test.android;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 // import android.app.AlertDialog;
 6 import android.view.Window;
 7 // import android.view.WindowManager;
 8 import android.view.KeyEvent;
 9 import android.webkit.WebView;
10 import android.webkit.WebViewClient;
11 //import android.webkit.WebChromeClient;
12 // import android.webkit.JsResult;
13 // import android.content.DialogInterface;
14 // import android.content.DialogInterface.OnClickListener;
15 
16 public class Main extends Activity {
17   public static final String LOAD_URL = "http://www.baidu.com/";
18 
19   private WebView webView = null;
20 
21   public void onCreate(Bundle savedInstanceState) {
22     super.onCreate(savedInstanceState);
23 
24     //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
25     //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
26 
27     webView = new WebView(this);
28     this.setContentView(webView);
29     webView.setWebViewClient(new WebViewClient() {
30       public void onPageFinished(WebView view, String url) {
31         //webView.evaluateJavascript("test();", null);
32       }
33     });
34     /*
35     webView.setWebChromeClient(new WebChromeClient() {
36       public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
37 
38         new AlertDialog.Builder(_this)
39         .setMessage(message)
40         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
41           public void onClick(DialogInterface dialog, int which) {}
42         }).show();
43 
44         return true;
45       }
46     });
47     */
48     webView.getSettings().setJavaScriptEnabled(true);
49     webView.addJavascriptInterface(this, "native");
50     webView.loadUrl(LOAD_URL);
51   }
52 
53   // overwrite back button
54   public boolean onKeyDown(int keyCode, KeyEvent event) {
55     if (KeyEvent.KEYCODE_BACK == keyCode && webView.canGoBack()) {
56       webView.goBack();
57       return true;
58     }
59 
60     return super.onKeyDown(keyCode, event);
61   }
62 
63 }
Copy the code

 

Save the file as Main.java

 

Back to the project root directory, create another file, save it as AndroidManifest.xml, which reads as follows:

Copy the code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest  xmlns:android="http://schemas.android.com/apk/res/android" package="test.android">
 3   <application android:icon="@drawable/icon"
 4                android:label="@string/app_name">
 5     <activity android:name=".Main">
 6       <intent-filter>
 7         <action android:name="android.intent.action.MAIN" />
 8         <category android:name="android.intent.category.LAUNCHER" />
 9       </intent-filter>
10     </activity>
11   </application>
12 
13   <uses-permission android:name="android.permission.INTERNET" />
14 
15 </manifest>
Copy the code

 

In the new project root directory res directory, create a directory in the drawable and values ​​res.

New xml file strings.xml in values, as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">网页打包</string>
  <string name="web_url">http://www.baidu.com</string>
</resources>

The program will then need to copy into the drawable icon directory, a file named icon.png

 

Demo from here to download

 

Compile

First change to the project directory.

First to compile resources, create gen directory in the project root directory, save R.java resources generated number, enter the following command in the console:

/opt/android-sdk-linux/build-tools/24.0.0/aapt package -f -m -J gen -S res -I /opt/android-sdk-linux/platforms/android-24/android.jar -M AndroidManifest.xml

When compiling java source files, add R.java source file:

javac -encoding utf-8 -source 1.6 -target 1.6 -bootclasspath /opt/android-sdk-linux/platforms/android-24/android.jar -d bin/classes src/test/android/Main.java gen/test/android/R.java

 The package compiled files into a format dex

/opt/android-sdk-linux/build-tools/24.0.0/dx --dex --output=bin/classes.dex bin/classes

The resource file is packaged

/opt/android-sdk-linux/build-tools/24.0.0/aapt package -f -M AndroidManifest.xml -S res -I /opt/android-sdk-linux/platforms/android-24/android.jar -F bin/main.ap_

Package all files into apk

java -classpath /opt/android-sdk-linux/tools/lib/sdklib.jar com.android.sdklib.build.ApkBuilderMain main_unsigned.apk -v -u -z bin/main.ap_ -f bin/classes.dex -rf src

Generating a signature file

1 keytool -genkey -alias my.keystore -keyalg RSA -validity 20000 -keypass 123456 -storepass 123456 -keystore my.keystore

When generating a signature file, are prompted for the name of the unit and the like can directly enter ignored, and finally enter y to confirm

Apk file for signature

jarsigner -verbose -keystore my.keystore -keypass 123456 -storepass 123456 -signedjar main.apk main_unsigned.apk my.keystore

Then you can install tested

 
 
 

Native app development costs and relatively high compared to the page, so more and more app using a web interface as, or even completely encapsulated into a website app, you can increase the speed of development, but also the basic cross-platform.

Android below to an example, a simple implementation WebView packaging site into the app ubuntu-14.04.4-desktop-amd64 environment.

 

Preparing the Environment

Development environment requires Java SDK ( official website to download ), Android SDK ( official website to download ).

Java SDK installation

wget http://download.oracle.com/otn-pub/java/jdk/8u91-b14/jdk-8u91-linux-x64.tar.gz
tar -x -f
jdk-8u91-linux-x64.tar.gz

Then configure the PATH and JAVA_HOME

we ~ / .bashrc

Added in the last

JAVA_HOME = Export the JDK extracted directory
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin

 Save and exit vi, refresh configuration

source ~/.bashrc

Android SDK installation

ps: installation process may require a ladder, please bring your own.

wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
tar -x -f android-sdk_r24.4.1-linux.tgz
cd android-sdk-linux/tools

Look at what sdk version can be installed

./android list sdk

Then install the required version sdk

./android update sdk --no-ui --filter comma-separated sequence number to install

 

Project to establish and coding

Create a new directory anywhere, save the project, and then create a src directory for storing source files. Because the concept of Java packages, so into the src directory, according to the level of the package name, followed by the establishment of the appropriate directory, and then create Java source files, such as:

Copy the code
 1 package test.android;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 // import android.app.AlertDialog;
 6 import android.view.Window;
 7 // import android.view.WindowManager;
 8 import android.view.KeyEvent;
 9 import android.webkit.WebView;
10 import android.webkit.WebViewClient;
11 //import android.webkit.WebChromeClient;
12 // import android.webkit.JsResult;
13 // import android.content.DialogInterface;
14 // import android.content.DialogInterface.OnClickListener;
15 
16 public class Main extends Activity {
17   public static final String LOAD_URL = "http://www.baidu.com/";
18 
19   private WebView webView = null;
20 
21   public void onCreate(Bundle savedInstanceState) {
22     super.onCreate(savedInstanceState);
23 
24     //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
25     //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
26 
27     webView = new WebView(this);
28     this.setContentView(webView);
29     webView.setWebViewClient(new WebViewClient() {
30       public void onPageFinished(WebView view, String url) {
31         //webView.evaluateJavascript("test();", null);
32       }
33     });
34     /*
35     webView.setWebChromeClient(new WebChromeClient() {
36       public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
37 
38         new AlertDialog.Builder(_this)
39         .setMessage(message)
40         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
41           public void onClick(DialogInterface dialog, int which) {}
42         }).show();
43 
44         return true;
45       }
46     });
47     */
48     webView.getSettings().setJavaScriptEnabled(true);
49     webView.addJavascriptInterface(this, "native");
50     webView.loadUrl(LOAD_URL);
51   }
52 
53   // overwrite back button
54   public boolean onKeyDown(int keyCode, KeyEvent event) {
55     if (KeyEvent.KEYCODE_BACK == keyCode && webView.canGoBack()) {
56       webView.goBack();
57       return true;
58     }
59 
60     return super.onKeyDown(keyCode, event);
61   }
62 
63 }
Copy the code

 

Save the file as Main.java

 

Back to the project root directory, create another file, save it as AndroidManifest.xml, which reads as follows:

Copy the code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest  xmlns:android="http://schemas.android.com/apk/res/android" package="test.android">
 3   <application android:icon="@drawable/icon"
 4                android:label="@string/app_name">
 5     <activity android:name=".Main">
 6       <intent-filter>
 7         <action android:name="android.intent.action.MAIN" />
 8         <category android:name="android.intent.category.LAUNCHER" />
 9       </intent-filter>
10     </activity>
11   </application>
12 
13   <uses-permission android:name="android.permission.INTERNET" />
14 
15 </manifest>
Copy the code

 

In the new project root directory res directory, create a directory in the drawable and values ​​res.

New xml file strings.xml in values, as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">网页打包</string>
  <string name="web_url">http://www.baidu.com</string>
</resources>

The program will then need to copy into the drawable icon directory, a file named icon.png

 

Demo from here to download

 

Compile

First change to the project directory.

First to compile resources, create gen directory in the project root directory, save R.java resources generated number, enter the following command in the console:

/opt/android-sdk-linux/build-tools/24.0.0/aapt package -f -m -J gen -S res -I /opt/android-sdk-linux/platforms/android-24/android.jar -M AndroidManifest.xml

When compiling java source files, add R.java source file:

javac -encoding utf-8 -source 1.6 -target 1.6 -bootclasspath /opt/android-sdk-linux/platforms/android-24/android.jar -d bin/classes src/test/android/Main.java gen/test/android/R.java

 The package compiled files into a format dex

/opt/android-sdk-linux/build-tools/24.0.0/dx --dex --output=bin/classes.dex bin/classes

The resource file is packaged

/opt/android-sdk-linux/build-tools/24.0.0/aapt package -f -M AndroidManifest.xml -S res -I /opt/android-sdk-linux/platforms/android-24/android.jar -F bin/main.ap_

Package all files into apk

java -classpath /opt/android-sdk-linux/tools/lib/sdklib.jar com.android.sdklib.build.ApkBuilderMain main_unsigned.apk -v -u -z bin/main.ap_ -f bin/classes.dex -rf src

Generating a signature file

1 keytool -genkey -alias my.keystore -keyalg RSA -validity 20000 -keypass 123456 -storepass 123456 -keystore my.keystore

When generating a signature file, are prompted for the name of the unit and the like can directly enter ignored, and finally enter y to confirm

Apk file for signature

jarsigner -verbose -keystore my.keystore -keypass 123456 -storepass 123456 -signedjar main.apk main_unsigned.apk my.keystore

Then you can install tested

Guess you like

Origin www.cnblogs.com/linybo/p/11057811.html