Here are the new steps to build a hybrid Android App with cordova.
Build cordova library
Download cordova Android source cordova-android-x.x.x.tgz from cordova-source.
Unzip it. Go to directory framework, execute gradle build, then you got framework-release.aar in framework/build/outputs/aar.
Build a native Android project
Create a native Android project
Make sure its minSdkVersion not lower than 19.
Copy framework-release.aar to project/app/libs.
Add implementation fileTree(dir: 'libs', include: ['*.jar','*.aar']) to dependencies of app/build.gradle.
Write a class ExtendCordovaConfigXmlParser with which we can parse arbitrary config.xml:
Write a class ExtendCordovaActivity with which we can build hybrid page easily:
Build a cordova project by npm command
We can copy config files, plugins simply from pure cordova project.
Create a pure cordova project by cordova create <path>.
Create Android platform by cordova platform add android.
Create first hybrid page in native App
Copy platforms/android/app/src/main/java/org/apache/cordova/whitelist/WhitelistPlugin.java from cordova project to native project. Do not change its package name.
Copy platforms/android/app/src/main/res/xml/config.xml from cordova project to native project.
Copy platforms/android/app/src/assets/www from from cordova project to native project.
Create WebPagePath which can switch web page directory.
Create an Activity IndexActivity.
Create a config file index.xml in app/src/main/res/xml/ of native project. Copy content from config.xml, change widget id to package name of IndexActivity. <content src="index.html" /> points to {web page directory}/index.html.
Make a button in MainActivity which jump to IndexActivity.
Run project and click button in MainActivity, you can see:
Use plugin
Use alert dialog plugin as an example.
Execute cordova plugin add cordova-plugin-dialogs in cordova project.
Copy platforms/android/app/src/main/java/org/apache/cordova/dialogs from cordova project to native project. Do not change its package name.
Copy platforms/android/src/assets/www/plugins/cordova-plugin-dialogs from from cordova project to native project.
Copy cordova-plugin-dialogs.notification related code from cordova project/platforms/android/app/src/assets/www/cordova_plugins.js to native project/app/src/assets/www/cordova_plugins.js.
Add a line <script type="text/javascript" src="notification.js"></script> under <script type="text/javascript" src="js/index.js"></script> in native project/app/src/assets/www/index.html.
Create notification.js in native project/platforms/android/app/src/assets/www, with content:
Run project and click button in MainActivity, you can see:
Write plugin
We’ll write a plugin to let web part get App name.
Native part
Create class org.apache.cordova.appinfo.AppInfo.
Add a new feature in res/xml/index.xml.
Web part
Add a new plugin assets/www/plugins/cordova-plugin-appinfo/www/appinfo.js
Modify assets/www/cordova_plugins.js to register the plugin:
Modify assets/www/notification.js to show AppInfo:
Run App, you’ll got:
Proguard
If you use proguard in release version, you would got java.lang.RuntimeException: Failed to create webview. when start a Cordova WebView.
Add -keep public class org.apache.cordova.** { *; } in proguard-rules.prod would solve this problem.