Error: Cannot read property 'replace' of undefined [Android]

Hi

I am first time user of Ionic. The version is now 3.3
I did default app create and resulting in a app that is functioning in the web browser when doing a ionic serve

However,
doing a
ionic cordova emulate android

results in the below error

BUILD SUCCESSFUL

Total time: 1.428 secs
Built the following apk(s):
/Volumes/LUN-NAS3-MISC/CordovaApps/myApp/platforms/android/build/outputs/apk/android-debug.apk

ANDROID_HOME=/Applications/AndroidDev/sdk
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
Error: Cannot read property ‘replace’ of undefined

What did I do wrong?

2 Likes

16 posts were split to a new topic: “Error: Cannot read property ‘replace’ of undefined” for ios-sim

Hi guys, I am emulating for Android; not iOS and getting the error.
Anyone else having the same issue with Android?

1 Like

I split the ios-sim occurences of this problem into their own topic: "Error: Cannot read property ‘replace’ of undefined" for ios-sim

@LiveRock

  1. Run the ionic cordova command with --verbose at the end and post the command and its result.
  2. Run the command without the ionic in the beginning, so it is a cordova command and post the command and its result.
  3. Post your ionic info output with the two other outputs for context.

Hi,

I just came across the same problem. I tracked it down to file /platforms/android/cordova/lib/emulator.js line 202:

var num = target.split(’(API level ‘)[1].replace(’)’, ‘’);

I replaced it with a regex search and extraction:

var num = target.match(/\d+/)[0];

It fixed the issue in my case. Nevertheless after that I still had a problem (I do not now if you are going to encounter it as well): the emulator launched but my application did not. This happens when I run the emulator for the first time. When it is already opened and I rerun the command “ionic cordova emulate android” the application starts.

17 Likes

@ahwell Could you please post your ionic info output?

$ ionic info

global packages:

    @ionic/cli-utils : 1.3.0
    Cordova CLI      : 7.0.1 
    Ionic CLI        : 3.3.0

local packages:

    @ionic/cli-plugin-cordova : 1.3.0
    @ionic/cli-plugin-ionic1  : 1.3.0
    Cordova Platforms         : android 6.2.3
    Ionic Framework           : ionic1 1.3.3

System:

    Node       : v6.10.2
    OS         : Linux 4.4
    Xcode      : not installed
    ios-deploy : not installed
    ios-sim    : not installed

Hello, I am having the same issue.

Please post stacktraces. This error message is far too common to make search food.

I got the same error message too, and as @rapropos say, this is a common error message so googling has not been of much help.

Unfortunately, I have no idea how to get the stacktrace (I tried ionic cordova run android --stacktrace but to no avail).

This is what I get when I ran ionic info:

global packages:

    @ionic/cli-plugin-proxy : 1.3.0
    @ionic/cli-utils        : 1.3.0
    Cordova CLI             : 7.0.1 
    Ionic CLI               : 3.3.0

local packages:

    @ionic/app-scripts              : 1.3.7
    @ionic/cli-plugin-cordova       : 1.3.0
    @ionic/cli-plugin-ionic-angular : 1.3.0
    Cordova Platforms               : android 6.2.3
    Ionic Framework                 : ionic-angular 3.3.0

System:

    Node       : v7.2.1
    OS         : macOS Sierra
    Xcode      : Xcode 8.3 Build version 8E162 
    ios-deploy : not installed
    ios-sim    : not installed

Basically, I am trying to test my ionic app on my android device by connecting it to my macbook pro…Can someone let me know how to pinpoint the source of error? Thanks!

Try ionic cordova run android --verbose and cordova run android --stracktrace.

this gives:

BUILD SUCCESSFUL

Total time: 2.862 secs

Built the following apk(s): 
	/Users/Joanne/Desktop/learning/mobile/platforms/android/build/outputs/apk/android-debug.apk

ANDROID_HOME=/Users/Joanne/Library/Android/sdk
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
No target specified and no devices found, deploying to emulator

Error: Cannot read property 'replace' of undefined


[ERROR] Cordova encountered an error.
        You may get more insight by running the Cordova command above directly.
        
[DEBUG] !!! ERROR ENCOUNTERED !!!
[ERROR] An error occurred while running cordova run android (exit code 1).
        

and [quote=“Sujan12, post:19, topic:93297”]
cordova run android --stracktrace
[/quote]

gives:

BUILD SUCCESSFUL

Total time: 1.601 secs
Built the following apk(s): 
	/Users/Joanne/Desktop/learning/mobile/platforms/android/build/outputs/apk/android-debug.apk
ANDROID_HOME=/Users/Joanne/Library/Android/sdk
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
No target specified and no devices found, deploying to emulator
Error: Cannot read property 'replace' of undefined

:frowning:

This is definitely an issue in cordova-android, the library Cordova uses for Android. Something goes wrong when it tries to get the list of emulators or something like that.

You (and the others having this problem) should search for an issue or report an issue at https://issues.apache.org/jira/projects/CB/issues (Include the complete output of cordova buid android --stracktrace)

I had the same issue, but starting the emulator before running the ionic cordova run android command fixed it for me.

5 Likes

Thanks @ahwell this worked for me.

2 Likes

Did anybody create an issue? What do I upvote?

Seems noone affected did - I couldn’t find an issue at issues.cordova.io right now. Please do so! (and post the link here then, of course)

Here:

https://issues.apache.org/jira/browse/CB-12981?jql=text%20~%20"cannot%20read%20property%20’%22

sorry I didn’t have much time for details.

1 Like

@ahwell Kudos, same thing here.

The target is Android API 26 and it is trying to parse it with this:
var num = target.split('(API level ')[1].replace(')', '');

Which of course fails cause it should.

Thank you very much for the help! I was struggling with that for nearly 24h… :smile: