Andriod内嵌HTML5项目构建以及整包更新配置 什么?让JAVA猿搭Android内嵌H5的架子? Java是无所不能的,Java猿是无所不会的!!!
其实以前自己做过Android的demo,积攒了那么一两天的安卓开发经验,安卓集成个H5这还是叫事?
搞它
1. 安卓开发环境安装 1.1 下载并安装 Android Studio 安卓官网 下载 并安装 Android Studio
1.2 配置VPN 建议在如下位置配置VPN,来加速SDK等组件的安装速度
1.3 选择需要的SDK版本进行安装
1.4 配置虚拟设备 1.4.1 点击如图图标配置设备
1.4.2 配置虚拟设备信息
1.4.3 下载对应版本x86 image 和 英特尔硬件加速执行管理器(Intel HAXM) 此方法为电脑端模拟安卓系统,用以测试app,此方法受电脑配置影响,会轻微卡顿,建议使用下面方法连接手机测试
1.5 编辑器连接手机
安卓手机打开 USB调试
连接手机
2. 创建空的Android项目
3. 编写内嵌H5代码 3.1 新建Activity文件 Activity 类是 Android 应用的关键组件,而 Activity 的启动和组合方式则是该平台应用模型的基本组成部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class WebActivity extends AppCompatActivity { private String apkUrl = "http://118.24.148.250:8080/yk/update_signed.apk" ; private String updateTitle = "发现新版本V2.0.0" ; private String updateContent = "1、Kotlin重构版\n2、支持自定义UI\n3、增加md5校验\n4、更多功能等你探索" ; @Override public void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.wv_webview); String url = "https://www.ytooo.top" ; webView.setWebViewClient(new WebViewClient()); webView.getSettings().setJavaScriptEnabled(true ); webView.loadUrl(url); } }
3.2 新建layout文件 lyout是安卓应用程序的布局文件
布局可定义应用中的界面结构(例如 Activity 的界面结构)。布局中的所有元素均使用 View 和 ViewGroup 对象的层次结构进行构建。View 通常绘制用户可查看并进行交互的内容。然而,ViewGroup 是不可见容器,用于定义 View 和其他 ViewGroup 对象的布局结构,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".WebActivity"> <WebView android:id ="@+id/wv_webview" android:layout_width ="fill_parent" android:layout_height ="fill_parent" /> </androidx.coordinatorlayout.widget.CoordinatorLayout >
3.2 配置 AndroidManifest.xml 每个应用项目必须在项目源设置的根目录中加入 AndroidManifest.xml 文件(且必须使用此名称)。 清单文件会向 Android 构建工具、Android 操作系统和 Google Play 描述应用的基本信息。
清单文件需声明以下内容:
应用的软件包名称,其通常与代码的命名空间相匹配。 构建项目时,Android 构建工具会使用此信息来确定代码实体的位置。 打包应用时,构建工具会使用 Gradle 构建文件中的应用 ID 来替换此值,而此 ID 则用作系统和 Google Play 上的唯一应用标识符。了解关于软件包名称和应用 ID 的更多内容。
应用的组件,包括所有 Activity、服务、广播接收器和内容提供程序。 每个组件都必须定义基本属性,例如其 Kotlin 或 Java 类的名称。 清单文件还能声明一些功能,例如其所能处理的设备配置,以及描述组件如何启动的 Intent 过滤器。了解关于应用组件的更多内容。
应用为访问系统或其他应用的受保护部分所需的权限。 如果其他应用想要访问此应用的内容,则清单文件还会声明其必须拥有的权限。 了解关于权限的更多内容。
应用需要的硬件和软件功能,这些功能会影响哪些设备能够从 Google Play 安装应用。了解关于设备兼容性的更多内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <manifest xmlns:android ="http://schemas.android.com/apk/res/android" package ="com.adc.da" > <application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:usesCleartextTraffic="true" android:theme="@style/AppTheme" > <activity android:name=".WebActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter > <action android:name ="android.intent.action.MAIN" /> <category android:name ="android.intent.category.LAUNCHER" /> </intent-filter > </activity > </application > <uses-permission android:name ="android.permission.INTERNET" /> </manifest >
3.3 启动项目
4. 编写自动更新模块 感谢UpdateAppUtils 提供的支持
4.1 添加依赖 build.gradle 文件中添加如下依赖:
1 implementation 'com.teprinciple:updateapputils:2.0.0'
4.1 更新实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 private String apkUrl = "http://118.24.148.250:8080/yk/update_signed.apk" ; private String updateTitle = "发现新版本V2.0.0" ; private String updateContent = "1、Kotlin重构版\n2、支持自定义UI\n3、增加md5校验\n4、更多功能等你探索" ; private void update (String apkUrl, String updateTitle, String updateContent) { UiConfig uiConfig = new UiConfig(); uiConfig.setUiType(UiType.PLENTIFUL); uiConfig.setUpdateLogoImgRes(R.drawable.ic_update); uiConfig.setUpdateLogoImgRes(R.drawable.ic_update); uiConfig.setUpdateBtnBgRes(R.drawable.bg_btn); uiConfig.setTitleTextColor(Color.BLACK); uiConfig.setTitleTextSize(18f ); uiConfig.setContentTextColor(Color.parseColor("#88e16531" )); UpdateConfig updateConfig = new UpdateConfig(); updateConfig.setCheckWifi(true ); updateConfig.setDebug(Boolean.TRUE); updateConfig.setNotifyImgRes(R.drawable.ic_logo); updateConfig.setApkSavePath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/download" ); updateConfig.setApkSaveName("scan-ui" + System.currentTimeMillis()); updateConfig.setForce(Boolean.TRUE); updateConfig.setAlwaysShow(Boolean.TRUE); updateConfig.setThisTimeShow(Boolean.TRUE); updateConfig.setShowNotification(Boolean.TRUE); UpdateAppUtils .getInstance() .apkUrl(apkUrl) .updateTitle(updateTitle) .updateContent(updateContent) .uiConfig(uiConfig) .updateConfig(updateConfig) .setMd5CheckResultListener(result -> { }) .setUpdateDownloadListener(new UpdateDownloadListener() { @Override public void onStart () { } @Override public void onDownload (int progress) { } @Override public void onFinish () { } @Override public void onError (Throwable e) { Log.e("error" , "更新失败" , e); } }) .update(); }
4.2 启动项目
更多好玩好看的内容,欢迎到我的博客交流,共同进步 胡萝卜啵的博客