如何在Ionic/Capacitor中设置Android Studio的正确路径?

6

我正在处理在Linux Mint电脑上的Ionic/React入门指南。

使用React构建Ionic应用

前几步进行得很顺利,但当我到达将您的应用程序部署到iOS和Android中的第5步时,出现了错误。

说明:

Open your project in Android Studio using the following command:

    npx cap open android

首先,我遇到了路径错误:

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

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

这似乎很简单。但是,Android Studio并没有安装在我的计算机上。

我进行了一些谷歌搜索,并在node_modules中进行了一些筛选,发现了“androidStudioPath”设置。我尝试将其添加到我的项目的capacitor.config.json文件中,但没有任何变化。因此,我仔细查看了代码,然后在“linux”对象中添加了它:

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

据我所知,这是正确的设置,我也设定了正确的路径。但它被忽略了。
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)

我该如何在Ionic/Capacitor中设置正确的Android Studio路径?

2个回答

17

利用源代码,卢克!

正确的设置是 "linuxAndroidStudioPath"。

在 ./node_modules/@capacitor/cli/dist/android/open.js 中,你会找到:

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' +
'}');

所以在./capacitor.config.json中,这个不起作用:

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

这不起作用:

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

这个做:

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

但是,我该如何针对使用不同路径的多个开发人员进行配置,具体取决于其操作系统?该文件是共享的,因此每个开发人员都可以提交它。使用 CAPACITOR_ANDROID_STUDIO_PATH 并没有起作用。 - nelson6e65

4

我使用 JetBrains Toolbox 来管理在 macOS 上的 Android Studio。

为了使用 npx cap open android,我需要按照以下方式导出 Android Studio 路径:

export CAPACITOR_ANDROID_STUDIO_PATH="/Users/myuser/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/201.7199119/Android Studio.app" 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接