无法在 Android 上安装 Cordova 发布构建 APK

Cordova release built APK cannot be installed on Android

命令 "Cordova build android" 生成一个名为 app-debug.apk 的 apk 文件,我可以将其安装在我的设备上(带有 Android v8.0 的三星 A3)

命令 "Cordova build android --release" 生成一个名为 app-release-unsigned.apk 的 apk 文件,该文件不会安装在同一设备上 ("App not installed")。

我还必须补充一点,我并不是要在 Playstore 上发布该 apk。我也尝试签署 APK,但它并没有改变结果。

这两个文件的本质区别应该是什么?发布版本失败的原因可能是什么?

这是位于 Cordova 项目文件夹中的 config.xml 的内容:

<?xml version='1.0' encoding='utf-8'?>
<widget id="eu.jrc.treechecker" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>TreeChecker</name>
    <description>
        A mobile application 
    </description>
    <author email="*******" href="********">
        Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" /> 
    <access origin="*" />
    <icon src="www/img/ic_canhemon.png" />
    <platform name="android">
        <allow-intent href="market:*" />
        <preference name="android-minSdkVersion" value="24" />
        <preference name="android-targetSdkVersion" value="28" />
    </platform>
    <plugin name="cordova-plugin-camera" spec="^4.0.3" />
    <plugin name="cordova-plugin-spinner-dialog" spec="^1.3.1" />
    <plugin name="cordova-plugin-dbcopy" spec="^2.1.2" />
    <plugin name="cordova-sqlite-storage" spec="^3.1.0" />
</widget>

这里是platforms/android/app/src/main/AndroidManifest的内容。xml:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="eu.jrc.treechecker" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
        </provider>
    </application>
    <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="28" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

编辑:这是我将用于 build.json:

{
    "android": {
        "debug": {
            "keystore": "appname-mobileapps.keystore",
            "storePassword": "***",
            "alias": "appname-mobileapps",
            "password" : "***",
            "keystoreType": ""
        },
        "release": {
            "keystore": "appname-mobileapps.keystore",
            "storePassword": "***",
            "alias": "appname-mobileapps",
            "password" : "***",
            "keystoreType": ""
        }
    }
}

调试版本和发布版本是两个不同的东西。第一个适用于开发人员,而第二个适用于您的用户。为了在 Cordova 中生成发布版本,您可以使用 Cordova CLI 或使用 Android Studio.

Cordova CLI

为了使用 CLI 进行发布构建,您需要 运行 cordova build --release 以及一组参数。 运行 没有配置的命令不会提供成功的构建。这些参数对于为 Play 商店签署您的应用程序是必需的。此外,这些配置可以在 cli 中提供,也可以通过 build.json 配置文件提供。 Signing an App 上的 Cordova 文档让您对此有更深入的了解。

Android工作室

这种方式是制作发布版本的相对简单的方式。您在这里所需要做的就是将您的应用程序加载到 android 工作室,然后按照屏幕上关于如何 Generate Signed apk 获取发布版本的说明进行操作。它会要求您创建一个密钥文件,这是未来版本所必需的。

我接受了上面的答案,但是我post这里是我一步一步做的,也是因为cordova网站上提出的方法对我不起作用。

# build apk
cordova build android --release

# sign apk (I used keytool to create mykeystore)
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <mykeystore> <PATH-TO-app-release-unsigned.apk> <appname-alias>

# align apk
zipalign -v 4 <PATH-TO-app-release-unsigned.apk> <final-apk-name>