How to fix 'net::ERR_CLEARTEXT_NOT_PERMITTED' in flutter - flutter

I have implemented webView in flutter but it is not opening my php website which is on server what I'm doing wrong.
I am new to flutter and tried webview to integrate my website webpage in my application but no luck.
Widget build(BuildContext context) {
// TODO: implement build
return WebviewScaffold(
appBar: AppBar(iconTheme:IconThemeData(color: Colors.white),title: Text("Intake Form",style:new TextStyle(color: Colors.white,fontWeight: FontWeight.bold)),backgroundColor:Colors.indigoAccent,automaticallyImplyLeading: false),
url: url,
//url: "http://xxxxxxxx/",
withJavascript: true,
supportMultipleWindows: true,
withLocalStorage: true,
allowFileURLs: true,
enableAppScheme: true,
appCacheEnabled: true,
hidden: false,
scrollBar: true,
geolocationEnabled: false,
clearCookies: true,
// usesCleartextTraffic="true"
);
}
I expect the output as running webview but error is thrown.

In main directory of your Flutter project you have three main folders:
- lib = your Dart code
- ios = generated structure for iOS platform
- android = generated structure for Android platform
We are interested in android directory.
When you open it, you will see "typical Android app structure".
So you have to do 2 things:
1) Add new file in res
Go to directory:
my_flutter_project/android/app/src/main/res/
Create xml directory (in res!)
And inside xml add new file with name: network_security_config.xml and content:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
network_security_config.xml should be located in path:
my_flutter_project/android/app/src/main/res/xml/network_security_config.xml
Here you can find more information about this file:
https://developer.android.com/training/articles/security-config
2) Modify AndroidManifest.xml
Go to:
flutter_project/android/app/src/main/AndroidManifest.xml
AndroidManifest.xml is XML file, with structure:
<manifest>
<application>
<activity>
...
</activity>
<meta-data >
</application>
</manifest>
So for <application> PROPERTIES you have to add 1 line:
android:networkSecurityConfig="#xml/network_security_config"
Remember that you have to add it as property (inside application opening tag):
<application
SOMEWHERE HERE IS OK
>
Not as a tag:
<application> <--- opening tag
HERE IS WRONG!!!!
<application/> <--- closing tag

set usesCleartextTraffic property to true in your AndroidManifest file, like below.
<application
....
android:usesCleartextTraffic="true"
....>

Android:
In flutter_project/android/app/src/main/AndroidManifest.xml
Add
<application
....
android:usesCleartextTraffic="true"
....>
IOS:
In flutter_project/ios/Runner/info.plist
Add
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run flutter clean.Then, run the app.

For ios modify info.plist ./ios/Runner/info.plist
Add the following:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
and run flutter clean before spinning up the app again

make network_security_config.xml file.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
add this two line in Manifest file in Application tag.

Related

Flutter build failed error..androidmanifest doesn't have application tag

C:\Users\devis\AndroidStudioProjects\vitality\android\app\src\debug\AndroidManifest.xml:7:9-33 Error:
Attribute application#label value=(vitality) from AndroidManifest.xml:7:9-33
is also present at [io.kommunicate.sdk:kommunicateui:2.1.6] AndroidManifest.xml:14:9-41 value=(#string/app_name).
Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:6:5-43:19 to override.
this was the error^^^^
this is my androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="devisuresh.vitality">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
im confused where to add what since i dont have an application tag.
You need to find the AndroidManifest file at the following hierarchy
android/app/src/main
then you need to add tools:replace="android:label to between application tags
To elaborate :
<application
android:name="io.flutter.app.FlutterApplication"
android:requestLegacyExternalStorage="true"
android:label="xxxxx"
android:usesCleartextTraffic="true"
tools:replace="android:label" // here it goes
android:icon="#mipmap/ic_launcher">

Http Requests are notworking after build apk [ionic 5]

I have developed a app using ionic 5.
http requests are working fine in browser. But not working after build apk.
it shows this error message
[ERROR:ssl_client_socket_impl.cc(960)] handshake failed; returned -1, SSL error code 1, net_error -202
i have code like this:
getCategories(){
return new Promise((resolve,reject)=>{
this.http.get(`${environment.apiUrl}/categories/list`).subscribe((res:any)=>{
resolve(res);
},(err)=>{
reject(err);
})
})
}
Please help me to fix this issue.
Google has updated their policy after android 8. After android 8 it will not allow you to use the HTTP as a api call you will have to use HTTPS in case you want to use.
Please see the below link this may help.
https://medium.com/#mountainappstudio/api-not-running-on-android-oreo-8-0-or-higher-version-problem-solved-ded5cc614d5e
network_security_config.xml add this file in resources/android/xml/ folder
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain>your domain</domain>
<domain includeSubdomains="true">localhost</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Add below code in config.xml
<edit-config file="app/src/main/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" />

ionic 5 Ionic/Angular cannot make http request in android emulator(net: ERR_CONNECTION_REFUSED)

I have already enabled cors in my rest API. I can succesfully make a request in my browser, but when i run my application in android studio using this command "ionic cap run android --livereload --external", i cannot get any response from my api. when checking the console i got below error "net: ERR_CONNECTION_REFUSED"
console error
If you want to test your app on Android Studio and your API is running on localhost, you need to send requests to 10.0.2.2 instead of localhost. In your case, instead of using the base API URL http://localhost:3000 in your Ionic app, use http://10.0.2.2:3000.
Next, add android:networkSecurityConfig="#xml/network_security_config" to the application element in the AndroidManifest.xml file:
<application
android:networkSecurityConfig="#xml/network_security_config"
You will need your IPv4 Address. Open Command Prompt, enter ipconfig and take note of the IPv4 Address.
Create a file named network_security_config.xml in the path android\app\src\main\res\xml and replace ipv4address with your IPv4 Address:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">ipv4address</domain>
</domain-config>
</network-security-config>
Finally, run your app with the following command:
ionic cap run android -l --host=0.0.0.0
Source: https://developer.android.com/studio/run/emulator-networking.html
You have to set android:usecleartexttraffic attributes in the config.xml.
<platform name="android">
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="#xml/network_security_config" />
<application android:usesCleartextTraffic="true" />
</edit-config>
...
</platform>
Also, you have to add subDomains of RestAPI in network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">127.0.0.1</domain>
</domain-config>
</network-security-config>

Flutter http request works on emulator, but not on real android

The method http.post works on the emulator, but when I run this on real android phone it doesn't work.
Android permission already added.
Main AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.niceapp">
<uses-permission android:name="android.permission.INTERNET"/>..
...
...
What could be the problem?
it may be because you are using localhost. check your url and the network you are in.
Try adding this lines:
<application
...
android:networkSecurityConfig="#xml/network_security_config"
android:usesCleartextTraffic="true"
...
This part is optional:
create new folder in res as Android Resource Directory and resource type as xml from dropdown.
add new xml resource file named as "network_security_config"
put this line inside "network_security_config.xml"
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">your url</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Happy coding :D

Unity firebase notifications custom image

I have decided to implement Firebase notifications into my unity project. The issue is that for notifications I want to use custom image, but that image is also used as app image (app_icon). I tried to modify manifest where I put into MessagingUnityPlayerActivity activity custom notification image (notificon) for displaying notification icon.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="${applicationId}"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name"
android:icon="#drawable/app_icon">
<!-- The MessagingUnityPlayerActivity is a class that extends
UnityPlayerActivity to work around a known issue when receiving
notification data payloads in the background. -->
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity"
android:label="#string/app_name"
android:icon="#drawable/notificon"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service android:name="com.google.firebase.messaging.MessageForwardingService"
android:exported="false"/>
</application>
</manifest>
I tried to modify it and delete some tags, but then the build wond compile. I should also mention that I have multiple manifests in my project and tried them to merge together but unsuccessfully. But this shouldnt be the problem since this is the only manifest with android:icon propery.
Thank you
Make an image (a white on transparent version of game icon, png) under folder "Assets\Plugins\Android\res\drawable.
In my case, the name of my image is "small_icon.png" then in "Assets\Plugins\Android\AndroidManifest.xml", add it after opening tag:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/small_icon" />
Your AndroidManifest.xml should be like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="${applicationId}"
android:versionCode="1" android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/app_icon">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/small_icon" />
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
....
Putting a res folder inside Assets/Plugins/Android using AAB is obsolete and will prevent you from building.
Since I was too lazy to install Android Studio to build a custom AAR I solved it by placing a ZIP-Archive into the Assets/Plugins/Android folder, with the res folder inside (res folder created with this as mentioned in an other answer) and the following snippet inside a AndroidManifest.xml next to the res folder:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.google.firebase.messaging"
android:versionCode="1"
android:versionName="1.0">
<application>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/YOURICONIMAGENAMEWITHOUTEXTENSION" />
</application>
</manifest>
Then change the ZIP file extension to AAR in the explorer.
You can then specify the icons name from the senders side.
More details of the AAR anatomy here.
No need to change the manifest. This worked for me:
So you should be able to add your
icon(s) to
Assets/Plugins/Android/res/drawable/
and reference it name
Then, in the notification json, specify your icon. For example:
"notification": {
"title": "Title",
"body" : "First Notification",
"text" : "Text",
"icon" : "MyIcon"
}
Note: I used one of these icons.
Then change the ZIP file extension to AAR in the explorer. You can
then specify the icons name from the senders side. More details of the
AAR anatomy here.
If you just change the extension to aar, it won't compile.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unityLibrary:compileReleaseJavaWithJavac'.
> Could not resolve all files for configuration ':unityLibrary:releaseCompileClasspath'.
> Failed to transform firebase_notification_icon-.aar (:firebase_notification_icon:) to match attributes {artifactType=android-classes-jar, org.gradle.status=integration, org.gradle.usage=java-api}.
> Execution failed for AarToClassTransform: C:\...\.gradle\caches\transforms-2\files-2.1\ea6ac2b3d0993f0958d52cbbdfbd76ea\jetified-firebase_notification_icon.aar.
> entry
AAR-file must be created in Android Studio. Since it must contain a number of required files such as AndroidManifest.xml, classes.jar, etc.