为什么 assembleDebug 成功但 assembleRelease 在 react native 中失败?

Why assembleDebug success but assembleRelease failed in react native?

我已经尝试 assemble 我的项目作为一个版本。但它总是失败。下面是一些我用来构建我的 apk 作为发布的脚本:-

构建成功

.\gradlew assembleDebug --no-daemon
.\gradlew app:assembleDebug

构建失败

.\gradlew app:assembleRelease
.\gradlew assembleRelease -x bundleReleaseJsAndAssets

每次失败构建时提及失败的结果:-

> Execution failed for task ':app:mergeReleaseResources'
> Task :app:mergeReleaseResources
> FAILED cvc-type.3.1.3: The value '' of element 'id' is not valid.

我还尝试使用 keytool 创建密钥库并将其应用于

中的 signingConfigs

./android/app/build.gradle

但没有发布成功。所以,我正在恢复并使用默认 debug.keystore

参考论坛的各种答案、SO 问题、我尝试应用的教程。我完全卡住了。

有没有人遇到这个问题并能够克服障碍?

已编辑:构建时的错误日志

Using .\gradlew assembleRelease

D:\Project\Clients\android>.\gradlew assembleRelease

> Configure project :react-native-activity-result
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
WARNING: The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.4.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '23.0.1'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.

> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.

> Task :react-native-activity-result:verifyReleaseResources FAILED

> Task :app:bundleReleaseJsAndAssets
Loading dependency graph, done.
info Writing bundle output to:, D:\Project\Clients\android\app\build\generated\assets\react\release\index.android.bundle
info Writing sourcemap output to:, D:\Project\Clients\android\app\build\intermediates\sourcemaps\release\index.android.bundle.packager.map
info Done writing bundle output
info Done writing sourcemap output
info Copying 47 asset files
info Done copying assets

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-activity-result:verifyReleaseResources'.
> 1 exception was raised by workers:
  com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
  error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
  error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:7: error: resource android:attr/colorError not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:11: error: resource android:attr/colorError not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:15: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2734: error: resource android:attr/fontStyle not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2735: error: resource android:attr/font not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2736: error: resource android:attr/fontWeight not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2737: error: resource android:attr/fontVariationSettings not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2738: error: resource android:attr/ttcIndex not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2902: error: resource android:attr/startX not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2905: error: resource android:attr/startY not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2908: error: resource android:attr/endX not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2911: error: resource android:attr/endY not found.
  D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2919: error: resource android:attr/offset not found.
  error: failed linking references.



* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2m 54s
210 actionable tasks: 22 executed, 188 up-to-date

D:\Project\Clients\android> 

曾经面对过这个,这是一个 APPT2 问题(至少在我的情况下)。从共享的屏幕截图中,它告诉您 重复资源 。这可能发生在您执行 js 捆绑后,并且创建了几个包含资源的文件夹。

以下是我如何处理 APPT2 问题:

  1. 尝试导航到 (android/app/build/intermediates/res/merged/release/
  2. 看看有没有drawable-*文件夹
  3. 执行rm -rf android/app/build/intermediates/res/merged/release/drawable-*)
  4. 再试一次./gradlew assembleRelease(你这里可能还是编译失败)
  5. 回到同一个文件夹,删除 .DS_Store
  6. 再次构建
  7. 希望成功

在某些 React Native 版本中,这是一个错误。您可以执行以下操作:

node_modules/react-native文件夹中,找到react.gradle文件,

然后在doFirst之后添加如下代码:

 doLast {
    def moveFunc = { resSuffix ->
        File originalDir = 
    file("$buildDir/generated/res/react/release/drawable-${resSuffix}");
        if (originalDir.exists()) {
            File destDir = 
    file("$buildDir/../src/main/res/drawable-${resSuffix}");
            ant.move(file: originalDir, tofile: destDir);
        }
    }
    moveFunc.curry("ldpi").call()
    moveFunc.curry("mdpi").call()
    moveFunc.curry("hdpi").call()
    moveFunc.curry("xhdpi").call()
    moveFunc.curry("xxhdpi").call()
    moveFunc.curry("xxxhdpi").call()
}

然后你可以使用./gradlew assembleRelease生成apk。
同时,当你想知道构建过程中的errr是什么的时候,可以使用./gradlew assembleRelease --info commod

我终于可以创建发行版 APK

  1. 通过 运行 命令 keytool -genkey -v -keystore myappkeystore.keystore -alias myappalias-keyalg RSA -keysize 2048 -validity 10000 创建密钥库并将其放置在 .\android\app 上。详细说明可参考here
  2. 打开名为 build.gradle 的文件并查找部分名称 signingConfigs
  3. 在调试键名后插入以下脚本并替换为您注册的键:-

    release {
        storeFile file('mycreatedkeystore.keystore')
        storePassword 'mypassword'
        keyAlias 'mykeyalias'
        keyPassword 'mypassword'
    }
    
  4. 打开空白记事本并粘贴以下脚本:-

    @echo off
    :: BatchGotAdmin
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
    set driveName="%CD:~0,2%"
    set currentFolder="%~dp0"
    setlocal enableextensions enabledelayedexpansion
    set /A count=1
    set list=
    CD %driveName%
    
    echo ===========================================
    for /d %%D in (*) do (
        set "listName=%%~f"
        set list=%listName%%1
        rem echo !listName! !
        echo !count!^. %%~D
        set /a count+=1
    )
    echo ===========================================
    :startAgain
    set choice=
    set /p choice=Type in target folder from listing to build release APK (e.g. targetfolder): 
    if not '%choice%'=='' (
        if exist %choice% (
            set choice=%choice%
            CD %choice%
            goto performbuild
        )
    )
    echo Invalid directory name!
    goto startAgain
    
    :performbuild
    echo Script will start build target APK to release on %choice%...
    timeout /t 2 /nobreak>nul
    call 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/
    rem start "" %currentFolder%%choice%^\android\app\src\main\res
    timeout /t 2 /nobreak>nul
    cd %currentFolder%%choice%^\android\app\src\main\res
    echo Validating duplicating resources
    if exist "drawable-hdpi" rmdir "drawable-hdpi" /q /s
    if exist "drawable-mdpi" rmdir "drawable-mdpi" /q /s
    if exist "drawable-xhdpi" rmdir "drawable-xhdpi" /q /s
    if exist "drawable-xxhdpi" rmdir "drawable-xxhdpi" /q /s
    if exist "drawable-xxxhdpi" rmdir "drawable-xxxhdpi" /q /s
    if exist "raw" rmdir "raw" /q /s
    cd %currentFolder%%choice%^\android
    call gradlew assembleRelease -x bundleReleaseJsAndAssets
    start "" %currentFolder%%choice%^\android\app\build\outputs\apk\release"
    cd %currentFolder%%choice%
    :choiceOpt
    set /P answ=Build complete. Do you want to rebuild again[Y/N]?
    if /I "%answ%" EQU "Y" goto :performbuild
    if /I "%answ%" EQU "N" goto :justexit
    goto :choiceOpt
    
    :justexit
    echo Exiting
    timeout /t 2 /nobreak>nul
    exit
    
  5. 以任意名称保存文件并将扩展名设置为.BAT

  6. 将 BAT 文件放在根项目上方的一个文件夹中,例如

    • 您的项目文件夹:- C:\Project\Mobile\MyCurrentProject

    • 你的批处理文件夹:- C:\Project\Mobile\releaseTool.bat

  7. 运行 releaseTool.bat 和构建发布版本的批处理文件

我搜索并找到了几个脚本来完成某些 oart 并将其组合起来为 RELEASE APK 版本制作这个批处理脚本

免责声明:我不是专家,无法详细说明流程或争论我正在做的方式(如果不切实际),因为我未能构建我想要的东西和作品逐个解组合成我提供的。