Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nativescript gradle build fails with "resource xml/network_security_config not found"

I have an existing Nativescript angular app that I have been able to run on my Android emulator without any problems. The other day I updated the tns-android version from 4.1.3 to 5.0.0 using the following command:

tns platform update android

I then tried running the emulator again using the following command

tns run android --bundle

However, I haven't been able to get the app working since the update because of the following error:

error: resource xml/network_security_config (aka org.nativescript.app:xml/network_security_config) not found.

My AndroidManifest.xml looks like this:

<application
   android:name=...
   android:debuggable="true"
   android:networkSecurityConfig="@xml/network_security_config">
   ...
</application>

I have a network_security_config.xml file located in /App_Resouces/Android/xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <debug-overrides>
      <trust-anchors>
         <certificates src="system" />
         <certificates src="user" />
      </trust-anchors>
   </debug-overrides>
</network-security-config>

I tried removing the platform and adding it again but the same error occurred. I also tried removing the platform and installing a specific version of the android platform but still no luck.

When I checked my git history I can see the only tracked file that changed in the repo was the package.json file. I tried reverting back to the last commit that I know worked but still I saw the same error.

Previously

"tns-android": {
  "version": "4.1.3"
} 

After Update

"tns-android": {
  "version": "5.0.0"
} 

The only way I can get my build to work is if I remove android:networkSecurityConfig="@xml/network_security_config" from the AndroidManifest.xml. However, I need this for development purposes as I am using a localhost endpoint and Android needs to use https://10.0.2.2. See the following article https://medium.com/@noumaan/ssl-app-dev-a2923d5113c6

I'm pretty sure at this point that during the platform update some file was modified outside of the repo that is causing this issue. I tried creating a new nativescript-angular project and then adding network_security_config to the AndroidManifest but the error still happens on a brand new project

tns create HelloWorld --template tns-template-blank-ng

The full log file from running the tns build command can be seen below:

Gradle build...
         + setting applicationId
         + applying user-defined configuration from /<path>/nativescript-app/app/App_Resources/Android/app.gradle
Using support library version 28.0.0
         + adding nativescript runtime package dependency: nativescript-optimized-with-inspector
         + adding aar plugin dependency: /<path>/nativescript-app/node_modules/tns-core-modules-widgets/platforms/android/widgets-release.aar

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Android resource linking failed
  Output:  /<path>/nativescript-app/platforms/a
ndroid/app/build/intermediates/merged_manifests/debug/processDebugManifest/merged/An
droidManifest.xml:22: error: resource xml/network_security_config (aka org.nativescr
ipt.app:xml/network_security_config) not found.
  error: failed processing manifest.

  Command: /<path>/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.0-4818971-osx.jar/ea8e83f954287505ed9d825341b12875/aapt2-3.2.0-4818971-osx/aapt2 link -I\
          /<path>/Library/Android/sdk/platforms/android-28/android.jar\
          --manifest\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml\
          -o\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/processed_res/debug/processDebugResources/out/resources-debug.ap_\
          -R\
          @/<path>/nativescript-app/platforms/android/app/build/intermediates/incremental/processDebugResources/resources-list-for-resources-debug.ap_.txt\
          --auto-add-overlay\
          --java\
          /<path>/nativescript-app/platforms/android/app/build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r\
          --custom-package\
          org.nativescript.app\
          -0\
          apk\
          --no-version-vectors\
          --output-text-symbols\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/symbols/debug/R.txt\
          --no-version-vectors
  Daemon:  AAPT2 aapt2-3.2.0-4818971-osx Daemon #0
  Output:  /<path>/nativescript-app/platforms/android/app/build/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml:22: AAPT: error: resource xml/network_security_config (aka org.nativescript.app:xml/network_security_config) not found.

  error: failed processing manifest.
  Command: /<path>/.gradle/caches/transforms-1/files-1.1/aapt2-3.2.0-4818971-osx.jar/ea8e83f954287505ed9d825341b12875/aapt2-3.2.0-4818971-osx/aapt2 link -I\
          /<path>/Library/Android/sdk/platforms/android-28/android.jar\
          --manifest\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/merged_manifests/debug/processDebugManifest/merged/AndroidManifest.xml\
          -o\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/processed_res/debug/processDebugResources/out/resources-debug.ap_\
          -R\
          @/<path>/nativescript-app/platforms/android/app/build/intermediates/incremental/processDebugResources/resources-list-for-resources-debug.ap_.txt\
          --auto-add-overlay\
          --java\
          /<path>/nativescript-app/platforms/android/app/build/generated/not_namespaced_r_class_sources/debug/processDebugResources/r\
          --custom-package\
          org.nativescript.app\
          -0\
          apk\
          --no-version-vectors\
          --output-text-symbols\
          /<path>/nativescript-app/platforms/android/app/build/intermediates/symbols/debug/R.txt\
          --no-version-vectors
  Daemon:  AAPT2 aapt2-3.2.0-4818971-osx Daemon #0

* 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

BUILD FAILED in 3s
Unable to apply changes on device: emulator-5554. Error is: Command ./gradlew failed
 with exit code 1.
Executing after-watch hook from /<path>/natives
cript-app/hooks/after-watch/nativescript-dev-typescript.js
Executing after-watch hook from /<path>/natives
cript-app/hooks/after-watch/nativescript-dev-webpack.js
Stopping webpack watch
Executing after-watch hook from /<path>/natives
cript-app/hooks/after-watch/nativescript-dev-typescript.js
Executing after-watch hook from /<path>/nativescript-app/hooks/after-watch/nativescript-dev-webpack.js
like image 649
Barry Jones Avatar asked Nov 15 '18 00:11

Barry Jones


2 Answers

To make it work for cordova-android@^8.1.0, make sure you have this configuration:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />

For android@^8.1.0 you need to make sure the target is correct: target="app/src/main/res/xml/network_security_config.xml"

like image 105
Itay Avatar answered Sep 18 '22 13:09

Itay


After reading the "Network security configuration" it says to place the xml file in res/xml/network_security_config.xml. So my full path is

./App_Resources/Android/src/main/res/xml/network_security_config.xml

After I made this change the Android build is working again. I'm still not sure what changed as this previously worked when the xml file was located at:

./App_Resources/Android/xml/network_security_config.xml
like image 23
Barry Jones Avatar answered Sep 21 '22 13:09

Barry Jones