Unable to resolve class targetSdkVersion

Viewed 4355

We use Microsoft Azure Pipelines for building Ionic apps for both ios and android. Unfortunately we encounter the following error while building the android app.

FAILURE: Build failed with an exception.

* Where:
Build file '/home/vsts/work/1/s/android/capacitor-cordova-android-plugins/build.gradle' line: 21

* What went wrong:
Could not compile build file '/home/vsts/work/1/s/android/capacitor-cordova-android-plugins/build.gradle'.
> startup failed:
  build file '/home/vsts/work/1/s/android/capacitor-cordova-android-plugins/build.gradle': 21: unable to resolve class targetSdkVersion 
   @ line 21, column 26.
             targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29

The strange part is that several days ago it worked without any problems. Commands used to generate the APK

sudo npx jetify

sudo ionic capacitor sync android

cd android && gradle wrapper && sudo ./gradlew assembleRelease `//this is where it fails`
2 Answers

Strange it is indeed. I have the same issue without any good explanation, but I also have a workable fix. Replacing the line

targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29

with

targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29

works and it makes some more sense to me than the assignment.

Edit: apparently this is the correct fix, a recent pull request in the capacitor repository does the same: https://github.com/ionic-team/capacitor/pull/4788 or the fixing https://github.com/ionic-team/capacitor/pull/4788/commits/250fdcf03be6510dd629a91a98b5220e14d31664

Ionic App generate some files in folder capacitor-android (if you use capacitor). In this folder, open the manifests/AndroidManisfest.xml and add android:exported="true" where you have without the android:exported. The issue is because Capacitor did not yet add this new requirement in the implementation for Android build script.

Related