简体   繁体   中英

console.log not working when running an Android app in Cordova

I have a basic Cordova application with a simple console.log() function at the end.

My index.html file looks as follows:

<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
...
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        console.log("Device is ready!");
    }
</script>
</body>
</html>

As you can see it's basically the same as the automatically generated file with some console.log script at the bottom.

Unfortunately when I run the app on Android using cordova emulate android , I cannot seem to find the output from console.log(..) anywhere. However, it works fine on browsers.

Use

cordova plugin add cordova-plugin-console

This plugin will enable console logging.

It has various methods for console printing

  1. console.log
  2. console.error
  3. console.exception
  4. console.warn
  5. console.info
  6. console.debug
  7. console.assert
  8. console.dir
  9. console.dirxml
  10. console.time
  11. console.timeEnd
  12. console.table

By default plugin is not installed so you will have to add it manually.

Reference .

Regards.

console.log(..) only works on browser console, not on command line.

To get log that appears on your app use alert(...); .

Anyway if you wanted to see your logs by consol.log() you should use an IDE like Android Studio or use ddms located where your adb is installed. (run ddms.bat )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM