Android APK is not running independently,It's running only i am running it in my system's browser and in same network

Whenever I am creating bundle for android through Android studio that’s working only when my computer (Where application is running) and device are in same network.
But application not loading when I install the bundle and trying to run from mobile data.
Have you faced such issue?
I think it’s expecting the application to run in my localhost:8080 .
but when i create bundle it should be running in the device.
Any help would be appreciated.
In Cordova i never faced this issue. May be I am missing out few configuration…

Your are right
when you use any command such as

npx cap open android

OR my fav

ionic cap run android -l --external  //Open Andoid studio and enable live reload

the capacitor will add the following to its capacitor.config.json file

  "server": {
    "url": "http://192.168.1.5:8100",
    "cleartext": true
  }

once you stop the dev server this will be removed, so if see the same file before or after the dev server start or stop there will be no such config… however only during the time the dev server is running will it be present…

What i mean by that is you will eventually be needed to replace this line
"url": "http://192.168.1.5:8100",
to point at your prod server after you deploy…

you are saying to replace “url”: “http://192.168.1.5:8100”, with what?
what do you mean by prod server?

Basically, when you run/start a dev server capacitor adds the URL to its config file, that URL is the one which your apk is trying to access. However, this information is removed once you stop the dev server… meaning it won’t be shown in the capacitor.config.json file

So if you are installing the apk on your phone then the phone needs to be connected to your machine via USB or needs to be in the same LAN…

As far as the Prod server it concerned once your app is ready to be deployed the “URL” will need to point to a website where your server of the app is running something like these

  1. samplewebapp.heroku.app
  2. api.yourwebsitename.com

Point / solution to your issue

When you use Mobile data then the app needs to be hosted on a internet facing domain as it is a differenet network not same as your wifi network… even on emulator

so for the app to connect to localhost:PORT is not possible, via mobile data…

This is because the app you are trying to install is built using a dev env config… not the prod env config…

But my application is going to be purely app which will be deployed to App store and play store only…
I don’t think i am going to deploy it on any of the web server…
What shall i do in such scenarios?

Since your app is trying to access localhost:8080 it clearly indicates it is depended on some services from server… it can be anything like Authentication, fetching or saving data to db etc…
in such case even the google Guideline asks you to update the URL schemes either in the andriod mainfest file or in this case capacitor.config.json file

This link will tell you how to prepare you ionic app for google play deployment Link

This is the Google’s checklist / Best-practise guide for deploying/Launching any app to playstore Link

Lastly This is link for releasing the app to play store from Google Link and even this tell you to update your urls for servers and services Link

isn’t there any way i can run apk independently as i have done in cordova apps?

Please try by replacing your code with below code in network_security_config.xml

<network-security-config>
	<base-config cleartextTrafficPermitted="true">
		<trust-anchors>
			<certificates src="system" />
		</trust-anchors>
	</base-config>
	<domain-config cleartextTrafficPermitted="true">
		<domain includeSubdomains="true">localhost</domain>
	</domain-config>
</network-security-config>

Path for security file → resources\android\xml

In my android project there is no file called network_security_config.xml

Can you try building an APK instead of a bundle and see if the APK can be installed and run properly on the device?

I tried apk only…I am able to install it but it’s getting stuck to splash screen

I manually added network_security_config.xml but still facing same issue

Is your project in IONIC ?

Yes…it’s in Ionic capacitor…Application is up and running with below commnad…But APKs are not working independently
ionic cap run android -l --external

can you share the code…

it’s not code issue…It’s a configuration issue…Let me share the config…

Blockquote
{

“appId”: “com.tes.customer”,

“appName”: “test”,

“bundledWebRuntime”: false,

“npmClient”: “npm”,

“webDir”: “dist/app/browser”,

“plugins”: {

"SplashScreen": {

  "launchShowDuration": 10000,

  "FadeSplashScreen": false,

  "launchAutoHide": false

},

"CapacitorFirebaseAuth": {

  "providers": [

    "google.com",

    "twitter.com",

    "facebook.com"

  ],

  "languageCode": "en",

  "nativeAuth": false

}

},

“server”: {

"allowNavigation": [

  "*"

],

"url": "http://192.168.0.139:8100",

"cleartext": true,

"originalUrl": "http://localhost:8100",

"originalCleartext": true

}

}

this seems to me that android studio [gradle] just needs to be re-indexed…

here are some more troubleshooting steps
First, Close Android Studio, any enmulator devices currently running then

  1. Go to you code make any changes and re-save it --just need to ensure that the compiler thinks that there is a change.
  2. Then run ionic build
  3. Followed by ionic cap run android -l --external

when the android studio opens just see in the right side bottom you see one of the following messages…

  1. " scanning file for indexing"
  2. “Indexing”

after this open the newly Index app on the emulator or build a apk to be used on the mobile phone…

However DO NOT STOP THE SERVER / OR CLOSE THE ANDROID STUDIO
that we break the new app as well…

with this command ionic cap run android -l --external the app is running untill i continue to host it on my laptop…
That’s what I don’t want. I want to test the application independently on device which used to happen on corodova…

Hello Same issue i am facing with IOS as well.The IPA bundle is not running independently.
It’s running only when i am running the application at my mac which is on same network as my device…I am sure something needs to be done at configuration end.

In case if anyone face this problem.
The reason for my issue was

“server”: {

"allowNavigation": [

  "*"

],

when i removed it and did simple Ionic build and
Ion capacitor run android/IOS
then after I simply created the apk from Android studio and it’s working after running the apk independently on device.

1 Like