Android system source code compilation series (vi)--compile the built-in browser WebView tutorial separately

Source: Internet
Author: User

original http://blog.csdn.net/zhaoxy_thu/article/details/18883015

This article mainly on the source code from the Android system extracted WebView related source code to compile a separate process to explain.

Compilation Process Description

Since WebView contains two parts, some of which are upper-level Java code, including several Java classes, to provide interfaces externally, and a lower-level C + + code, including two so libraries (libwebcore.so and Libchromium_ net.so), used for parsing and rendering of web pages. Two sections interact with JNI.

Therefore, compiling webview also needs to be divided into two parts, one to compile Java code, and the other to compile C + + code into so libraries. In addition, because WebView's Java code uses a lot of hidden APIs from the system, we also need to compile the Android system and get a few jar packages from it.

compiling Java code

First of all, we need to download and compile any version of the Android system source code, the specific steps can be found in the "Android source code download and compilation tutorial."

After compiling, we can use Eclipse (cannot use IntelliJ idea, because after adding the library cannot be set as the system library) to create a new Android project, in the SRC directory under the creation of a Android.webkit2 's bag. All files under the Frameworks/base/core/java/android/webkit directory under the source directory are then copied to the newly created package.

Since one of these classes is generated after compilation, we also need to out/target/common/obj/java_libraries/framework_intermediates/src/core/java/from the compiled source directory. The Eventlogtags.java class is also copied to the new package in Android/webkit.

Next, we need to add the jar package containing the system hide API to the project, and then copy the following three jar packages to the project's Libs directory after renaming (name Random):

Out/target/common/obj/java_libraries/framework_intermediates/classes.jarOut/target/common/obj/java_ Libraries/core_intermediates/classes.jarout/target/common/obj/java_libraries/bouncycastle_intermediates/ Classes.jar 

Open the project's Properties->java Build path->libraries->add library->user Library in Eclipse, click Next, select User Libraries ..., select New, enter a name randomly, select the system library (important), and choose OK. Select the new user Library, select Add JARs, select the newly added three jar package in the project directory, and choose OK.

After the library is added, switch to the Order and Export tab, and adjust the newly created user library to the front, making sure that the hidden API is not overwritten by the system API (because the package name is the same), select OK. In addition, the Android Private libraries library before the check off, or after the operation will be error.

Finally, because our Java files are placed under the ANDROID.WEBKIT2 package, to avoid the same name as the package of the system, we need to replace all the Android.webkit in the Java file with Android.webkit2. Use the Search command in the menu to bulk replace. After you've finished replacing, refresh the project and you'll see that all the errors are gone. (It may also be suggested that the minsdkversion version is too low to modify the Androidmanifest.xml file directly as required)

Compiling so libraries

At this point our Eclipse project is ready to be compiled and run, but it will go off when it starts, because we haven't joined the so library yet. Let's compile the so library. Then go back to the virtual machine and go to the External/chromium directory of the Android source code and execute the following command at the terminal:

$ su' grep android/webkit-rl * '

You can replace the Android/webkit in the source code with ANDROID/WEBKIT2 to ensure that it is the same as our Java code package name.

Then open the Android.mk file in the External/chromium directory, replace the libchromium_net with Libchromium_net2, and add one line local_module_tags: = optional , modify the section as follows:

Local_module: = Libchromium_net2local_module_class: = shared_librarieslocal_module_tags: = OptionalINTERMEDIATES: = $ (   Local-intermediates-dir)

Go back to the Android source code root directory and run the following command:

$ source build/envsetup.sh$ mmm external/chromium

After the compilation is successful, the libchromium_net2.so file can be found in the Out/target/product/generic/obj/lib directory.

Next enter into the External/webkit/source/webkit/android directory of the Android source code and execute the following command at the terminal:

$ su' grep android/webkit-rl * '

It is also possible to replace the Android/webkit in the source code with ANDROID/WEBKIT2, ensuring the same name as our Java code package.

Then open the Android.mk file in the External/webkit directory, replace the Libwebcore in the Libwebcore2 (three places), and add a line of local_module_tags: = optional, In addition, the local_shared_libraries inside of the libchromium_net to change into Libchromium_net2. The modification section is as follows:

# Define Our module and find the Intermediates directoryLocal_module: = Libwebcore2Local_module_class:=Static_librariesLocal_module_tags: = Optionalbase_intermediates:=$ (call Local-intermediates-dir) ...Local_shared_libraries: = Libegl libGLESv2 libandroid libandroidfw libandroid_runtime libchromium_net2 libcrypto ...local_prelink_module : = falselocal_module : = Libwebcore2local_module_tags : = optional local_ldlibs : = $ (webkit_ldlibs)local_shared_libraries : = $ (webkit_shared_ LIBRARIES)local_static_libraries : = Libwebcore2 $ (webkit_static_libraries)local_ldflags : =-fvisibility=hidden                
Also go back to the Android source code root directory and run the following command:
$ source build/envsetup.sh$ mmm external/webkit

After the compilation is successful, the libwebcore2.so file can be found in the Out/target/product/generic/obj/lib directory.

Finish compiling and running the test code

Below we put the two so files generated by the above compilation (Libchromium_net2.so and libwebcore2.so) into the Libs/armeabi directory of our project. Modify the Jniutil.java and Webviewcore.java files under the project Android.webkit2 package to

static {    system.loadlibrary ("WebCore");    System.loadlibrary ("chromium_net");} 

Instead (note that the order is reversed)

static {    system.loadlibrary ("Chromium_net2");    System.loadlibrary ("Webcore2");} 

So far, the operations related to WebView have been completed, and we can start adding test code. In the OnCreate method of the Activity.java test, add:

WebView WebView = (WebView) Findviewbyid (R.id.webview); Webview.getsettings (). setjavascriptenabled (true); Webview.loadurl ("http://www.baidu.com"); 
Add the corresponding XML layout:
<android.webkit2.WebView     android:id="@+id/webview"     android:layout_width=  android:layout_height="fill_parent"/>     

Finally, add in the Androidmanifest.xml:

<android:name="Android.permission.INTERNET"/> 

Now we can run the project on the simulator, the effect is as follows:

Attention:

    1. If a black screen appears, you can try removing the last-added Internet permissions and then trying again.
    2. Can only be run at compile time for the corresponding version of the emulator, if the prompt function cannot be found, switch the version of the emulator and try again.
Real Machine Run

After running successfully on the emulator, you can run it on the real machine, with the following effect:

But once you swipe the webview with your finger, the app will flash back because of the lack of resources, and let's add them:

First we need to put the Android source code directory in the Frameworks/base/core/res/res/values/styles.xml

<style name="Zoomcontrols" >    <name="android:gravity" >bottom</< name="Android:paddingleft" >15dip</<name="Android:paddingright" >15dip </item></style>            

Paste into the Res/values/styles.xml in our engineering catalogue.

Then copy the Frameworks/base/core/res/res/layout/zoom_magnify.xml file to the Res/layout in our project directory.

The Frameworks/base/core/res/res/drawable/btn_zoom_page.xml file is then copied to the res/drawable in our project directory.

Finally, the drawable-ldpi, drawable-mdpi, drawable-hdpi and drawab under the Frameworks/base/core/res/res The Btn_zoom_page_normal.png and btn_zoom_page_press.png files in the le-xhdpi directory are copied to the appropriate folder under our Engineering directory res.

Run again, you can slide the webview.

Resources

Android 4.1-Compiling the system browser into a standalone application

"Building a custom Android browser environment"

If you feel that you can help, but also hope to help the top, thank you:)

Android system source code compilation series (vi)--compile the built-in browser WebView tutorial separately

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.