feat(android): 优化资源加载和活动跳转逻辑
- 移除 keep.xml 文件,清理不必要的资源 - 重构 ReactHostHelper 类,简化 loadBundle 方法 - 更新 MultipleReactActivityDelegate 类,使用本地文件路径加载 bundle - 修改 NavigationManager 类,优化活动跳转逻辑 - 新增 FileHelper 工具类,用于文件操作 - 更新 BuzActivity 类,使用 NavigationHelper.routerName 作为组件名称- 修改 MainApplication 类,使用本地文件路径加载 bundle - 在 MainActivity 布局中添加按钮- 更新 package.json,添加 androidRelease 脚本
这个提交包含在:
父节点
d1458981c5
当前提交
bd3847e0ab
文件差异因一行或多行过长而隐藏
二进制文件未显示。
文件差异因一行或多行过长而隐藏
二进制文件未显示。
|
之前 宽度: | 高度: | 大小: 1.5 KiB |
二进制文件未显示。
|
之前 宽度: | 高度: | 大小: 3.5 KiB |
二进制文件未显示。
|
之前 宽度: | 高度: | 大小: 6.9 KiB |
@ -1,4 +0,0 @@
|
|||||||
__d(function(g,r,i,a,m,e,d){var t=r(d[0]),n=r(d[1]),p=t(r(d[2]));n.AppRegistry.registerComponent(r(d[3]).Apps.Hospital,function(){return p.default})},10000000,[5,3,10000001,491]);
|
|
||||||
__d(function(g,r,i,a,m,e,d){var t=r(d[0]);Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var n=r(d[1]),o=t(r(d[2])),s=r(d[3]);var l=n.StyleSheet.create({container:{flex:1}});e.default=function(){var t='dark'===(0,n.useColorScheme)();return(0,s.jsxs)(n.View,{style:l.container,children:[(0,s.jsx)(n.StatusBar,{barStyle:t?'light-content':'dark-content'}),(0,s.jsx)(n.View,{style:{height:100}}),(0,s.jsx)(n.Text,{children:"\u4e92\u8054\u7f51\u533b\u9662"}),(0,s.jsx)(n.Button,{title:'\u8fd4\u56de',onPress:function(){(0,r(d[4]).pop)()}}),(0,s.jsx)(n.View,{style:{height:15}}),(0,s.jsx)(n.Button,{title:'Toast',onPress:function(){(0,r(d[5]).showMessage)('\u4e92\u8054\u7f51\u533b\u9662\u5f39\u51fatoast','info','common\u5411\u4e0b\u517c\u5bb9')}}),(0,s.jsx)(o.default,{})]})}},10000001,[5,3,503,243,491,502]);
|
|
||||||
__r(108);
|
|
||||||
__r(10000000);
|
|
||||||
@ -1 +0,0 @@
|
|||||||
<resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@drawable/src_app_resource_images_icon_sign_scan" />
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
__d(function(g,r,i,a,m,e,d){var n=r(d[0]),t=r(d[1]),p=n(r(d[2]));t.AppRegistry.registerComponent(r(d[3]).Apps.Ywq,function(){return p.default})},10000000,[5,3,10000001,491]);
|
|
||||||
__d(function(g,r,i,a,m,e,d){var t=r(d[0]);Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var n=r(d[1]),o=t(r(d[2])),s=r(d[3]);var l=n.StyleSheet.create({container:{flex:1}});e.default=function(){var t='dark'===(0,n.useColorScheme)();return(0,s.jsxs)(n.View,{style:l.container,children:[(0,s.jsx)(n.StatusBar,{barStyle:t?'light-content':'dark-content'}),(0,s.jsx)(n.View,{style:{height:100}}),(0,s.jsx)(n.Button,{title:'onConfirm',onPress:function(){}}),(0,s.jsx)(n.View,{style:{height:15}}),(0,s.jsx)(n.Button,{title:'Toast',onPress:function(){(0,r(d[4]).showMessage)('\u533b\u7f51\u7b7e\u5f39\u51fatoast-error','error')}}),(0,s.jsx)(o.default,{})]})}},10000001,[5,3,503,243,502]);
|
|
||||||
__r(108);
|
|
||||||
__r(10000000);
|
|
||||||
@ -1,9 +1,6 @@
|
|||||||
package com.facebook.react.runtime
|
package com.facebook.react.runtime
|
||||||
|
|
||||||
import com.facebook.react.bridge.JSBundleLoader
|
import com.facebook.react.bridge.JSBundleLoader
|
||||||
import com.facebook.react.interfaces.TaskInterface
|
|
||||||
import com.facebook.react.runtime.internal.bolts.Task
|
|
||||||
import kotlin.coroutines.Continuation
|
|
||||||
import kotlin.jvm.internal.Intrinsics
|
import kotlin.jvm.internal.Intrinsics
|
||||||
|
|
||||||
class ReactHostHelper(
|
class ReactHostHelper(
|
||||||
@ -12,10 +9,7 @@ class ReactHostHelper(
|
|||||||
fun loadBundle(bundleLoader: JSBundleLoader): Boolean? {
|
fun loadBundle(bundleLoader: JSBundleLoader): Boolean? {
|
||||||
Intrinsics.checkNotNullParameter(bundleLoader, "bundlerLoader")
|
Intrinsics.checkNotNullParameter(bundleLoader, "bundlerLoader")
|
||||||
val task = delegate.loadBundle(bundleLoader)
|
val task = delegate.loadBundle(bundleLoader)
|
||||||
|
|
||||||
|
|
||||||
task.waitForCompletion()
|
task.waitForCompletion()
|
||||||
|
|
||||||
return task.getResult()
|
return task.getResult()
|
||||||
}
|
}
|
||||||
fun getOrCreateReactInstance() {
|
fun getOrCreateReactInstance() {
|
||||||
|
|||||||
@ -26,12 +26,13 @@ class BuzActivity : ReactActivity() {
|
|||||||
this,
|
this,
|
||||||
mainComponentName,
|
mainComponentName,
|
||||||
fabricEnabled
|
fabricEnabled
|
||||||
) else MultipleReactActivityDelegate(this, mainComponentName, fabricEnabled)
|
) else
|
||||||
|
MultipleReactActivityDelegate(this, NavigationHelper.routerName, fabricEnabled)
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
AppManager.addActivity(this)
|
AppManager.addActivity(this)
|
||||||
Toast.makeText(this, "BuzActivity:" + BuildConfig.DEBUG, Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "BuzActivity:" + NavigationHelper.routerName, Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
|||||||
@ -10,6 +10,8 @@ import com.facebook.react.ReactPackage
|
|||||||
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
||||||
import com.facebook.react.defaults.DefaultReactNativeHost
|
import com.facebook.react.defaults.DefaultReactNativeHost
|
||||||
import com.trust.ywx.specs.navigation.NavigationPackage
|
import com.trust.ywx.specs.navigation.NavigationPackage
|
||||||
|
import com.trust.ywx.utils.FileHelper
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class MainApplication : Application(), ReactApplication {
|
class MainApplication : Application(), ReactApplication {
|
||||||
|
|
||||||
@ -21,11 +23,20 @@ class MainApplication : Application(), ReactApplication {
|
|||||||
add(NavigationPackage())
|
add(NavigationPackage())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getJSMainModuleName(): String =
|
//
|
||||||
if (getUseDeveloperSupport()) "index" else "commom"
|
// override fun getJSMainModuleName(): String =
|
||||||
|
// if (getUseDeveloperSupport()) "index" else "commom"
|
||||||
override fun getBundleAssetName(): String =
|
//
|
||||||
if (getUseDeveloperSupport()) "index.android.bundle" else "common.android.bundle"
|
// override fun getBundleAssetName(): String? =
|
||||||
|
// if (getUseDeveloperSupport()) "index.android.bundle" else null
|
||||||
|
//
|
||||||
|
// override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
|
||||||
|
// super.getJSBundleFile()
|
||||||
|
// } else {
|
||||||
|
// FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
|
||||||
|
// }
|
||||||
|
override fun getJSBundleFile(): String =
|
||||||
|
FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
|
||||||
|
|
||||||
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
|
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
|
||||||
|
|
||||||
@ -36,7 +47,23 @@ class MainApplication : Application(), ReactApplication {
|
|||||||
override val reactHost: ReactHost
|
override val reactHost: ReactHost
|
||||||
get() = getDefaultReactHost(applicationContext, reactNativeHost)
|
get() = getDefaultReactHost(applicationContext, reactNativeHost)
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
var isNew = true
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
|
val f = File(FileHelper.getDirPath(this, "bundles"))
|
||||||
|
val bf = File(FileHelper.getFilePath("bundle.zip", this, "bundles"))
|
||||||
|
if (!f.exists() || !f.isDirectory) {
|
||||||
|
f.mkdirs()
|
||||||
|
}
|
||||||
|
if (f.list().size <= 1) {
|
||||||
|
bf.delete()
|
||||||
|
FileHelper.copyAssetFileToInternalStorage(this, "bundle.zip", bf)
|
||||||
|
if (bf.exists()) {
|
||||||
|
FileHelper.unzip(bf, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
loadReactNative(this)
|
loadReactNative(this)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import com.facebook.react.defaults.DefaultReactActivityDelegate
|
|||||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags.enableBridgelessArchitecture
|
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags.enableBridgelessArchitecture
|
||||||
import com.facebook.react.runtime.ReactHostHelper
|
import com.facebook.react.runtime.ReactHostHelper
|
||||||
import com.facebook.react.runtime.ReactHostImpl
|
import com.facebook.react.runtime.ReactHostImpl
|
||||||
|
import com.trust.ywx.utils.FileHelper
|
||||||
|
|
||||||
class MultipleReactActivityDelegate(
|
class MultipleReactActivityDelegate(
|
||||||
activity: ReactActivity,
|
activity: ReactActivity,
|
||||||
@ -29,8 +30,6 @@ class MultipleReactActivityDelegate(
|
|||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
val helper = ReactHostHelper(reactHost as ReactHostImpl)
|
val helper = ReactHostHelper(reactHost as ReactHostImpl)
|
||||||
|
|
||||||
|
|
||||||
val mainComponentName = this.mainComponentName
|
val mainComponentName = this.mainComponentName
|
||||||
val launchOptions = this.composeLaunchOptions()
|
val launchOptions = this.composeLaunchOptions()
|
||||||
val activity = reactActivity
|
val activity = reactActivity
|
||||||
@ -50,15 +49,16 @@ class MultipleReactActivityDelegate(
|
|||||||
reactHost?.start()?.waitForCompletion()
|
reactHost?.start()?.waitForCompletion()
|
||||||
val result =
|
val result =
|
||||||
helper.loadBundle(
|
helper.loadBundle(
|
||||||
JSBundleLoader.createAssetLoader(
|
JSBundleLoader.createFileLoader(
|
||||||
this.reactActivity,
|
FileHelper.getFilePath(
|
||||||
"assets://index.android.bundle",
|
"$mainComponentName.android.bundle",
|
||||||
false
|
this.reactActivity.applicationContext,
|
||||||
|
"bundles"
|
||||||
|
)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
Log.i("TestApp", "load biz bundle ==> $result")
|
this.loadApp("app")
|
||||||
this.loadApp(mainComponentName)
|
|
||||||
} else {
|
} else {
|
||||||
this.mReactDelegate =
|
this.mReactDelegate =
|
||||||
object : ReactDelegate(
|
object : ReactDelegate(
|
||||||
|
|||||||
@ -11,7 +11,10 @@ class NavigationManager(reactContext: ReactApplicationContext) :
|
|||||||
override fun navigate(name: String) {
|
override fun navigate(name: String) {
|
||||||
NavigationHelper.routerName = name
|
NavigationHelper.routerName = name
|
||||||
AppManager.lastActivity()
|
AppManager.lastActivity()
|
||||||
?.startActivity(Intent(AppManager.lastActivity(), BuzActivity::class.java))
|
?.apply {
|
||||||
|
startActivity(Intent(this, BuzActivity::class.java))
|
||||||
|
overridePendingTransition(0, 0)
|
||||||
|
}
|
||||||
// AppManager.lastActivity()
|
// AppManager.lastActivity()
|
||||||
// ?.overridePendingTransition(0, 0)
|
// ?.overridePendingTransition(0, 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,80 @@
|
|||||||
|
package com.trust.ywx.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
|
import com.trust.ywx.AppManager
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.zip.ZipEntry
|
||||||
|
import java.util.zip.ZipInputStream
|
||||||
|
|
||||||
|
|
||||||
|
class FileHelper {
|
||||||
|
companion object {
|
||||||
|
fun getDirPath(context: Context?, type: String?): String {
|
||||||
|
return (context ?: AppManager.lastActivity())?.getExternalFilesDir(type)?.absolutePath!!
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFilePath(name: String, context: Context?, type: String?): String {
|
||||||
|
val path = getDirPath(context, type) + File.separator + name
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
fun copyAssetFileToInternalStorage(
|
||||||
|
context: Context,
|
||||||
|
assetFileName: String,
|
||||||
|
destFile: File
|
||||||
|
) {
|
||||||
|
val assetManager = context.assets
|
||||||
|
|
||||||
|
try {
|
||||||
|
assetManager.open(assetFileName).use { `in` ->
|
||||||
|
FileOutputStream(destFile).use { out ->
|
||||||
|
val buffer = ByteArray(1024)
|
||||||
|
var read: Int
|
||||||
|
while ((`in`.read(buffer).also { read = it }) != -1) {
|
||||||
|
out.write(buffer, 0, read)
|
||||||
|
}
|
||||||
|
Log.d(
|
||||||
|
">>>>>>>>>>>>>>CopyAssets",
|
||||||
|
"Copied: " + assetFileName + " to " + destFile.getAbsolutePath()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(">>>>>>>>>>>>>>CopyAssets", "Failed to copy asset: $assetFileName", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun unzip(zipFile: File, targetDir: File) {
|
||||||
|
if (!targetDir.exists()) targetDir.mkdirs()
|
||||||
|
|
||||||
|
ZipInputStream(FileInputStream(zipFile)).use { zipIn ->
|
||||||
|
var entry: ZipEntry?
|
||||||
|
while ((zipIn.getNextEntry().also { entry = it }) != null) {
|
||||||
|
val file = File(targetDir, entry!!.name)
|
||||||
|
if (entry.isDirectory) {
|
||||||
|
file.mkdirs()
|
||||||
|
} else {
|
||||||
|
// 确保父目录存在
|
||||||
|
val parent = file.getParentFile()
|
||||||
|
if (!parent.exists()) parent.mkdirs()
|
||||||
|
|
||||||
|
FileOutputStream(file).use { fos ->
|
||||||
|
val buffer = ByteArray(1024)
|
||||||
|
var len: Int
|
||||||
|
while ((zipIn.read(buffer).also { len = it }) > 0) {
|
||||||
|
fos.write(buffer, 0, len)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
zipIn.closeEntry()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,6 +1,12 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
android:gravity="center"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
<Button
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="button"
|
||||||
|
android:id="@+id/btn1"
|
||||||
|
android:layout_gravity="center"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@ -4,6 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"android": "react-native run-android --active-arch-only",
|
"android": "react-native run-android --active-arch-only",
|
||||||
|
"androidRelease": "react-native run-android --active-arch-only --mode=release",
|
||||||
"ios": "react-native run-ios",
|
"ios": "react-native run-ios",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"start": "react-native start",
|
"start": "react-native start",
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户