Export APK, AAB file in React Native

Nay mình chia sẻ cách export ra file APK và AAB trong React Native đến với mọi người. Để export ra file APK  hoặc AAB release. Thì các bạn phải cần tạo một keystore , cách tạo keystore bạn tìm hiểu tại đây:

Generating an upload key

Nếu ai đang dùng Mackbook thì có thể chạy câu lệnh này để tạo keystore:

sudo keytool -genkey -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

Sau khi đã có keystore bạn hãy copy keystore đó vào thư mục android/app/ của thư mục project 

Tiếp tục mở file gradle.properties trong thư mục android , cấu hình các thuộc tính đọc keystore

MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=123123123
MYAPP_UPLOAD_KEY_PASSWORD=123123123

Tiếp đến hãy mở file build.gralde trong thư mục android/app ,cấu hình các thuộc tính đọc file 

...
android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}
...

Okay vậy là xong. giờ chúng ta sẽ xuất file thôi

# Export APK file in React Native

Đầu tiên chúng ta clean project nó trước

cd android
./gradlew clean

Tiếp đến tại thư mục root project ta chạy lệnh sao

Chạy lệnh này đối với android, nếu nó báo không có thư mục assets trong đường dẫn android/app/src/main/assets , thì bạn hãy tạo thư mục assets tại đường dẫn đó

npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

Chú ý: Đối với ios thì ta cũng phải chạy lệnh này nhé

npx react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios

Giờ là lúc chúng ta xuất tiệp apk đây

cd android
./gradlew assembleRelease

Có một trường hợp nó báo lỗi  như sau: Error: Duplicate resources

Error: Duplicate resources
* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.
> [drawable-mdpi-v4/loading] /android/app/build/generated/res/react/release/drawable-mdpi/loading.gif   [drawable-mdpi-v4/loading]  /android/app/src/main/res/drawable-mdpi/loading.gif: Error: Duplicate resources

Nó báo lỗi bị trùng hình, hình ảnh đã có rồi. Chính vì thế, ta phải vào thư mục xoá hết các tấm hình bị trùng tại đường dẫn /android/app/src/main/res (drawable, drawable-hdpi,...) 

Sau khi xoá xong. ta sẽ chạy lại lệnh :

cd android
 ./gradlew assembleRelease

Trường hợp bị lỗi khác thì chịu khó tìm và fix nó thôi keke

Còn nếu bạn muốn chạy apk file ở chế độ debug thì chạy lệnh sau:

cd android
./gradlew bundleRelease

Các tệp mà ta export ra file apk hoặc aab nó sẽ nằm trong đường dẫn sau: android/app/build/outputs

# Export AAB file in React Native

Bây giờ cần upload lên google play thì ta cần phải có file aap, chúng ta cũng sử dụng các bước trên nhé. rồi chạy lệnh dưới đây

npx react-native build-android --mode=release

Bởi vì một project nó sẽ phát sinh ra rất là nhiều lỗi, mình thì mới gặp một số trường hợp nên chia sẻ để ở đây, đôi khi ta cần dùng tới

Các bạn có thể tìm hiểu thêm tại đây: Publishing to Google Play Store

Chúc các bạn thành công

Bài Viết Liên Quan

Messsage

Ủng hộ tôi bằng cách click vào quảng cáo. Để tôi có kinh phí tiếp tục phát triển Website!(Support me by clicking on the ad. Let me have the money to continue developing the Website!)