Переглянути джерело

feat(android): 优化资源加载和活动跳转逻辑

- 移除 keep.xml 文件,清理不必要的资源
- 重构 ReactHostHelper 类,简化 loadBundle 方法
- 更新 MultipleReactActivityDelegate 类,使用本地文件路径加载 bundle
- 修改 NavigationManager 类,优化活动跳转逻辑
- 新增 FileHelper 工具类,用于文件操作
- 更新 BuzActivity 类,使用 NavigationHelper.routerName 作为组件名称- 修改 MainApplication 类,使用本地文件路径加载 bundle
- 在 MainActivity 布局中添加按钮- 更新 package.json,添加 androidRelease 脚本
xuqm 1 тиждень тому
батько
коміт
bd3847e0ab

Різницю між файлами не показано, бо вона завелика
+ 0 - 2
android/app/src/main/assets/app.android.bundle


BIN
android/app/src/main/assets/bundle.zip


Різницю між файлами не показано, бо вона завелика
+ 0 - 1
android/app/src/main/assets/common.android.bundle


BIN
android/app/src/main/assets/drawable-mdpi/src_app_resource_images_icon_sign_scan.png


BIN
android/app/src/main/assets/drawable-xhdpi/src_app_resource_images_icon_sign_scan.png


BIN
android/app/src/main/assets/drawable-xxhdpi/src_app_resource_images_icon_sign_scan.png


+ 0 - 4
android/app/src/main/assets/hospital.android.bundle

@@ -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);

+ 0 - 1
android/app/src/main/assets/raw/keep.xml

@@ -1 +0,0 @@
-<resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@drawable/src_app_resource_images_icon_sign_scan" />

+ 0 - 4
android/app/src/main/assets/ywq.android.bundle

@@ -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);

+ 0 - 6
android/app/src/main/java/com/facebook/react/runtime/ReactHostHelper.kt

@@ -1,9 +1,6 @@
 package com.facebook.react.runtime
 
 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
 
 class ReactHostHelper(
@@ -12,10 +9,7 @@ class ReactHostHelper(
     fun loadBundle(bundleLoader: JSBundleLoader): Boolean? {
         Intrinsics.checkNotNullParameter(bundleLoader, "bundlerLoader")
         val task = delegate.loadBundle(bundleLoader)
-
-
         task.waitForCompletion()
-
         return task.getResult()
     }
     fun getOrCreateReactInstance() {

+ 3 - 2
android/app/src/main/java/com/trust/ywx/BuzActivity.kt

@@ -26,12 +26,13 @@ class BuzActivity : ReactActivity() {
             this,
             mainComponentName,
             fabricEnabled
-        ) else MultipleReactActivityDelegate(this, mainComponentName, fabricEnabled)
+        ) else
+            MultipleReactActivityDelegate(this, NavigationHelper.routerName, fabricEnabled)
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         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() {

+ 32 - 5
android/app/src/main/java/com/trust/ywx/MainApplication.kt

@@ -10,6 +10,8 @@ import com.facebook.react.ReactPackage
 import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
 import com.facebook.react.defaults.DefaultReactNativeHost
 import com.trust.ywx.specs.navigation.NavigationPackage
+import com.trust.ywx.utils.FileHelper
+import java.io.File
 
 class MainApplication : Application(), ReactApplication {
 
@@ -21,11 +23,20 @@ class MainApplication : Application(), ReactApplication {
                     add(NavigationPackage())
                 }
 
-            override fun getJSMainModuleName(): String =
-                if (getUseDeveloperSupport()) "index" else "commom"
-
-            override fun getBundleAssetName(): String =
-                if (getUseDeveloperSupport()) "index.android.bundle" else "common.android.bundle"
+            //
+//            override fun getJSMainModuleName(): String =
+//                if (getUseDeveloperSupport()) "index" else "commom"
+//
+//            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
 
@@ -36,7 +47,23 @@ class MainApplication : Application(), ReactApplication {
     override val reactHost: ReactHost
         get() = getDefaultReactHost(applicationContext, reactNativeHost)
 
+    companion object{
+        var isNew = true
+    }
+
     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()
         loadReactNative(this)
     }

+ 8 - 8
android/app/src/main/java/com/trust/ywx/multiple/MultipleReactActivityDelegate.kt

@@ -19,6 +19,7 @@ import com.facebook.react.defaults.DefaultReactActivityDelegate
 import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags.enableBridgelessArchitecture
 import com.facebook.react.runtime.ReactHostHelper
 import com.facebook.react.runtime.ReactHostImpl
+import com.trust.ywx.utils.FileHelper
 
 class MultipleReactActivityDelegate(
     activity: ReactActivity,
@@ -29,8 +30,6 @@ class MultipleReactActivityDelegate(
 
     override fun onCreate(savedInstanceState: Bundle?) {
         val helper = ReactHostHelper(reactHost as ReactHostImpl)
-
-
         val mainComponentName = this.mainComponentName
         val launchOptions = this.composeLaunchOptions()
         val activity = reactActivity
@@ -50,15 +49,16 @@ class MultipleReactActivityDelegate(
             reactHost?.start()?.waitForCompletion()
             val result =
                 helper.loadBundle(
-                    JSBundleLoader.createAssetLoader(
-                        this.reactActivity,
-                        "assets://index.android.bundle",
-                        false
+                    JSBundleLoader.createFileLoader(
+                        FileHelper.getFilePath(
+                            "$mainComponentName.android.bundle",
+                            this.reactActivity.applicationContext,
+                            "bundles"
+                        )
                     ),
                 )
 
-            Log.i("TestApp", "load biz bundle ==> $result")
-            this.loadApp(mainComponentName)
+            this.loadApp("app")
         } else {
             this.mReactDelegate =
                 object : ReactDelegate(

+ 4 - 1
android/app/src/main/java/com/trust/ywx/specs/navigation/NavigationManager.kt

@@ -11,7 +11,10 @@ class NavigationManager(reactContext: ReactApplicationContext) :
     override fun navigate(name: String) {
         NavigationHelper.routerName = name
         AppManager.lastActivity()
-            ?.startActivity(Intent(AppManager.lastActivity(), BuzActivity::class.java))
+            ?.apply {
+                startActivity(Intent(this, BuzActivity::class.java))
+                overridePendingTransition(0, 0)
+            }
 //        AppManager.lastActivity()
 //            ?.overridePendingTransition(0, 0)
     }

+ 80 - 0
android/app/src/main/java/com/trust/ywx/utils/FileHelper.kt

@@ -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()
+                }
+            }
+        }
+    }
+
+}

+ 7 - 1
android/app/src/main/res/layout/activity_main.xml

@@ -1,6 +1,12 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
+    android:gravity="center"
     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>

+ 1 - 0
package.json

@@ -4,6 +4,7 @@
   "private": true,
   "scripts": {
     "android": "react-native run-android --active-arch-only",
+    "androidRelease": "react-native run-android --active-arch-only --mode=release",
     "ios": "react-native run-ios",
     "lint": "eslint .",
     "start": "react-native start",

Деякі файли не було показано, через те що забагато файлів було змінено