البرمجة

كيفية الحصول على versionName في تطبيق React Native على Android

To get the versionName of your React Native app on Android, you can use the PackageInfo class provided by the Android framework. This class allows you to access various details about the package, including the versionName. Here’s how you can do it:

  1. First, ensure you have the necessary permissions in your AndroidManifest.xml file:

    xml
    <uses-permission android:name="android.permission.GET_PACKAGE_SIZE" />
  2. Then, in your React Native code, you can create a native module to access the versionName. Here’s an example of how you can do it:

    java
    package com.yourpackagename; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.util.Log; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; public class VersionModule extends ReactContextBaseJavaModule { private static final String TAG = "VersionModule"; public VersionModule(ReactApplicationContext reactContext) { super(reactContext); } @Override public String getName() { return "VersionModule"; } @ReactMethod public void getVersionName() { try { PackageInfo packageInfo = getReactApplicationContext().getPackageManager() .getPackageInfo(getReactApplicationContext().getPackageName(), 0); String versionName = packageInfo.versionName; Log.d(TAG, "VersionName: " + versionName); } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Error getting versionName", e); } } }
  3. Next, register your native module in your MainApplication.java file:

    java
    import com.yourpackagename.VersionModule; // Change 'yourpackagename' to your actual package name @Override protected List getPackages() { return Arrays.asList( new MainReactPackage(), new VersionModule() // Add this line ); }
  4. Finally, in your React Native JavaScript code, you can call the getVersionName method to retrieve the versionName:

    javascript
    import { NativeModules } from 'react-native'; NativeModules.VersionModule.getVersionName();

This setup will allow you to retrieve and log the versionName of your React Native app on Android.

المزيد من المعلومات

للحصول على المزيد من المعلومات حول رقم الإصدار (VersionName) في تطبيق React Native على Android، يمكنك استخدام هذا القيمة في عدة سيناريوهات مفيدة. على سبيل المثال، يمكنك استخدامها في شاشة “حول التطبيق” لعرض رقم الإصدار للمستخدمين. يمكنك أيضًا استخدام رقم الإصدار لتتبع النسخ المختلفة من التطبيق، وتحديد الإصدارات التي تحتوي على تغييرات معينة، مثل تحسينات الأداء أو إصلاحات الأخطاء.

زر الذهاب إلى الأعلى