Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the proper path to Android Studio in Ionic/Capacitor?

I'm working through an Ionic/React getting-started, on my Linux Mint box.

Building an Ionic application using React

The first steps went well enough, but when I get to Deploying your application to iOS and Android Step 5, I get errors.

The instructions:

Open your project in Android Studio using the following command:

    npx cap open android

First I got pathing errors:

events.js:174
    throw er; // Unhandled 'error' event
    ^

Error: spawn /usr/local/android-studio/bin/studio.sh 
ENOENT

That seemed simple enough. That's not where android studio is installed on my machine.

I did some googling around, and some grepping in node_modules, and discovered the "androidStudioPath" setting. I tried adding it to my project's capacitor.config.json file, and it made no difference. So I looked at the code a bit more carefully, and then added it in a "linux" object:

{
    "appId": "us.jdege.mytestapp",
    "appName": "mytestapp",
    "bundledWebRuntime": false,
    "npmClient": "npm",
    "webDir": "build",
    "linux": {
        "androidStudioPath": "/var/.../studio.sh"
    }
}

From all I can tell, this is the right setting, and I'm setting the right path. But it's being ignored.

jdege@linux-2016 ~/react-projects/mytestapp $ npx cap open android
[info] Opening Android project at /home/jdege/react-projects/mytestapp/android
events.js:174
    throw er; // Unhandled 'error' event
    ^

Error: spawn /usr/local/android-studio/bin/studio.sh ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)

How do I set the proper path to Android Studio in Ionic/Capacitor?

like image 421
Jeff Dege Avatar asked Dec 01 '22 09:12

Jeff Dege


2 Answers

Use the source, Luke!

The proper setting is "linuxAndroidStudioPath".

In ./node_modules/@capacitor/cli/dist/android/open.js, you'll find:

common_1.logError('Unable to launch Android Studio. You must configure "linuxAndroidStudioPath" ' +
'in your capacitor.config.json to point to the location of studio.sh, using JavaScript-escaped paths:\n' +
'Example:\n' +
'{\n' +
'  "linuxAndroidStudioPath": "/usr/local/android-studio/bin/studio.sh"\n' +
'}');

So while in ./capacitor.config.json this doesn't work:

{
    ...
    "androidStudioPath": "/var/.../studio.sh"
}

And this doesn't work:

{
    ...
    "linux": {
        "androidStudioPath": "/var/.../studio.sh"
    }
}

This does:

{
    ...
    "linuxAndroidStudioPath": "/var/.../studio.sh"
}
like image 75
Jeff Dege Avatar answered Dec 04 '22 12:12

Jeff Dege


I use JetBrains Toolbox to manage my Android Studio on my macOS

to use npx cap open android I had to export the Android Studio path as follow:

export CAPACITOR_ANDROID_STUDIO_PATH="/Users/myuser/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7199119/Android Studio.app" 
like image 23
RenRen Avatar answered Dec 04 '22 13:12

RenRen