You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic React安卓应用构建报错:invalid source release:17 如何解决?

Ionic React安卓应用构建失败:invalid source release:17 问题排查与解决

问题概述

应用在浏览器环境运行正常,但执行安卓构建时Gradle报错Cause: error: invalid source release: 17。已尝试修改app/build.gradle和capacitor.build.gradle中的Java版本为16,问题未解决。

已执行的构建步骤:

  • ionic capacitor build
  • npx cap add android
  • ionic capacitor sync android
  • ionic capacitor open android

当前app模块build.gradle代码

apply plugin: 'com.android.application'

android {
    namespace "ionic.io.starter"
    compileSdkVersion rootProject.ext.compileSdkVersion
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_16
        targetCompatibility JavaVersion.VERSION_16
    }
    defaultConfig {
        applicationId "ionic.io.starter"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        aaptOptions {
             // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
             // Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61
            ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    flatDir{
        dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion"
    implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion"
    implementation project(':capacitor-android')
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
    implementation project(':capacitor-cordova-android-plugins')
}

apply from: 'capacitor.build.gradle'

try {
    def servicesJSON = file('google-services.json')
    if (servicesJSON.text) {
        apply plugin: 'com.google.gms.google-services'
    }
} catch(Exception e) {
    logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}

解决步骤

1. 修改项目根目录build.gradle的Java版本配置

打开项目根目录下的build.gradle,找到buildscript块,确保javaVersion设置为16,同时添加全局Java编译版本约束:

buildscript {
    ext {
        compileSdkVersion = 33
        minSdkVersion = 22
        targetSdkVersion = 33
        androidxAppCompatVersion = "1.6.1"
        androidxCoordinatorLayoutVersion = "1.2.0"
        coreSplashScreenVersion = "1.0.1"
        junitVersion = "4.13.2"
        androidxJunitVersion = "1.1.5"
        androidxEspressoCoreVersion = "3.5.1"
        javaVersion = '16' // 确认此处为16
    }
    // ...其他依赖配置
}

// 添加全局Java编译版本约束
tasks.withType(JavaCompile) {
    sourceCompatibility = JavaVersion.VERSION_16
    targetCompatibility = JavaVersion.VERSION_16
}

2. 配置Android Studio的JDK路径

  • 打开Android Studio,进入File > Project Structure > SDK Location
  • 在JDK location处选择Java 16的安装目录,如果未安装Java 16,需先下载对应版本并配置

3. 修改gradle.properties指定JDK版本

在项目根目录的gradle.properties文件中添加或修改以下内容:

org.gradle.java.home=/你的Java16安装路径

例如Windows路径可能是C:\Program Files\Java\jdk-16.0.2,macOS路径可能是/Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home

4. 清理缓存并重新构建

  • 在Android Studio终端执行./gradlew clean(Windows使用gradlew clean)
  • 重新执行ionic capacitor sync android,然后再次尝试构建项目

内容的提问来源于stack exchange,提问作者Arielle F

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:05:25