settings.gradle 中的环境变量不适用于 Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21173826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:05:15  来源:igfitidea点击:

Environment variable in settings.gradle not working with Android Studio

androidandroid-studio

提问by ckihm

I do have a multi-module project with a library project in a different root path. As illustration you can imagine something like this:

我确实有一个多模块项目,其中一个库项目位于不同的根路径中。作为例证,您可以想象这样的事情:

/projects_home/projects/app_root
   |--app/
   |   |--build.gradle
   |--build.gradle
   |--settings.gradle

/libraries_home/libraries
   |--libA
       |--build.gradle

In my settings.gradle file I am able to set the absolute path to the library project utilizing the projectDir attribute. This works just fine within the console as well as with Android Studio.

在我的 settings.gradle 文件中,我可以使用 projectDir 属性设置库项目的绝对路径。这在控制台和 Android Studio 中都可以正常工作。

But if I try to use an environment variable it stops working with Android Studio. The settings.gradle for the example above would look like this:

但是,如果我尝试使用环境变量,它将停止与 Android Studio 一起使用。上面示例的 settings.gradle 将如下所示:

include ':app'
include ':libA'

project(':libA').projectDir = new File("$System.env.LIB_ROOT", '/libraries/libA')

If I build with the graddle wrapper from the console, it still works. But AS stops working with the following error msg:

如果我从控制台使用 graddle 包装器构建,它仍然有效。但 AS 停止工作,出现以下错误消息:

Gradle 'app' project refresh failed:
Configuration with name 'default' not found.

If I unset the environment variable, the build on console fails with the same msg:

如果我取消设置环境变量,控制台上的构建将失败并显示相同的消息:

* What went wrong:
A problem occurred configuring project ':app'.
> Configuration with name 'default' not found.

Therefore I guess that AS is somehow not be able to access the environment variables set with my ~/.bashrc

因此,我猜 AS 以某种方式无法访问使用我的 ~/.bashrc 设置的环境变量

Does somebody of you maybe know a way how I can make AS aware of my environment?

你们中有人知道如何让 AS 了解我的环境吗?

采纳答案by Scott Barta

Android Studio doesn't read environment variables, so this approach won't work. Also, using the projectDirscheme in settings.gradlewill probably cause problems. Android Studio has a limitation that all of its modules need to be located underneath the project root. If you have libraries that are used in multiple projects and they can't be placed under a single project root, the best advice is to have them publish JARs or AARs to a local Maven repository that individual projects can pick up.

Android Studio 不读取环境变量,因此这种方法不起作用。此外,projectDirsettings.gradle 中使用该方案可能会导致问题。Android Studio 有一个限制,即它的所有模块都需要位于项目根目录下。如果您有在多个项目中使用的库并且它们不能放在单个项目根目录下,最好的建议是让它们将 JAR 或 AAR 发布到单个项目可以获取的本地 Maven 存储库。

回答by changyuheng

Android Studio doesread the environment variables. You can prove it by launching Android Studio from the shell in which those env. variables being specified instead of X-window dash board.

Android Studio确实读取了环境变量。您可以通过从这些 env 所在的 shell 启动 Android Studio 来证明这一点。指定变量而不是 X-window 仪表板。

The reason you did not have those variables is the X-window environment you were using did not read $HOME/.bashrcwhich contained those variables. This makes sense because bashrcis for Bash not X.

您没有这些变量的原因是您使用的 X-window 环境没有读取$HOME/.bashrc包含这些变量的内容。这是有道理的,因为bashrc适用于 Bash 而不是 X。

Assume you are using GNOME or Unity, to launch Android Studio with those environment variables being specified, just modify the .desktopfile of Android Studio (e.g. ~/.local/share/applications/android-studio.desktop):

假设您使用的是 GNOME 或 Unity,要在指定这些环境变量的情况下启动 Android Studio,只需修改.desktopAndroid Studio的文件(例如~/.local/share/applications/android-studio.desktop):

Find this line:

找到这一行:

Exec="/home/username/tools/android/android-studio/bin/studio.sh" %f

Change it to:

将其更改为:

Exec=env LIB_ROOT=/libraries_home "/home/username/tools/android/android-studio/bin/studio.sh" %f

Note:

笔记:

This modification just prepend env LIB_ROOT=/libraries_hometo the original command. You must replace usernamewith your own user name.

此修改只是附加env LIB_ROOT=/libraries_home到原始命令。您必须替换username为您自己的用户名。

Update

更新

If you have any questions, please leave a commentfirst instead editing the answer directly. Especially, I don't know how to contact the editor.

如果您有任何问题,请先发表评论,而不是直接编辑答案。特别是,我不知道如何联系编辑。

回答by ckihm

Despite the answer from Scott Barta is correct, I realized there is a way to solve my problem and wan't to share this in case somebody else has the same requirement.

尽管 Scott Barta 的回答是正确的,但我意识到有一种方法可以解决我的问题,如果其他人有相同的要求,我不想分享它。

I am now using the gradle.properties file do define and use gradle properties instead of system properties. The documentation of this feature can be fined in the user guide

我现在使用 gradle.properties 文件来定义和使用 gradle 属性而不是系统属性。这个功能的文档可以在用户指南中罚款

The solution to my original question now looks like this:

我原来的问题的解决方案现在看起来像这样:

$USER_HOME/.gradle/gradle.properties:

LIB_ROOT=/libraries_home

The settings.gradle file has to be modified to use the gradle property instead of the system property:

必须修改 settings.gradle 文件以使用 gradle 属性而不是系统属性:

include ':app'
include ':libA'

project(':libA').projectDir = new File(LIB_ROOT, '/libraries/libA')

This works fine for me, headless as well as with AS.

这对我来说很好,无头以及 AS。

Some more words regarding the fact that I am working with modules which are not placed underneath one project root. Till now it looks like AS is not complaining about this. But I just started working with this structure and it may be that I will run into problems later. What I like about this is the more flat representation in AS which is more like I am used to have it with Eclipse.

关于我正在使用未放置在一个项目根目录下的模块这一事实,还有一些话要说。到目前为止,AS 似乎并没有抱怨这一点。但是我刚刚开始使用这种结构,以后可能会遇到问题。我喜欢这一点的是 AS 中更扁平的表示,这更像是我习惯于在 Eclipse 中使用它。

What is also described in the user guide, is to set system properties with the gradle.properties file. I tried this also, but I did run into the same problems with AS using environment variables.

用户指南中还描述了使用 gradle.properties 文件设置系统属性。我也试过这个,但我确实遇到了与使用环境变量的 AS 相同的问题。

回答by manroe

On Macs, Android Studio does not read environment variables for use in Gradle apparently. I believe this is the cause for confusion in the answers here - maybe it does on Windows.

在 Mac 上,Android Studio 显然不会读取在 Gradle 中使用的环境变量。我相信这是这里的答案混淆的原因 - 也许它在 Windows 上。

In order to get Android Studio to read environment variables, I run the application from the command line:

为了让 Android Studio 读取环境变量,我从命令行运行应用程序:

> /Applications/Android\ Studio.app/Contents/MacOS/studio 

The other answers here offer solutions other than using environment variables. For my situation, I'm using a library I didn't write that requires the use of an environment variable, and I'd rather not edit their code so it's easier to update later.

这里的其他答案提供了除使用环境变量之外的解决方案。对于我的情况,我使用的库不是我写的,它需要使用环境变量,我宁愿不编辑它们的代码,以便以后更容易更新。

EDIT: And, I have a dock icon to launch Android Studio this way: https://superuser.com/a/260712/501568explains how.

编辑:而且,我有一个停靠图标可以通过这种方式启动 Android Studio:https: //superuser.com/a/260712/501568解释了如何。

回答by Karoly

It works for me with the following steps:

它通过以下步骤对我有用:

  1. Set your variable in Windows
  2. Reboot
  3. reach it in gradle build: System.env.MYVARIABLE
  1. 在 Windows 中设置变量
  2. 重启
  3. 在 gradle build 中到达它: System.env.MYVARIABLE

回答by mixel

You can set environment variable by appending:

您可以通过附加来设置环境变量:

-DYOUR_VARIABLE=variable_value

to ~/Library/Preferences/AndroidStudioX.X/studio.vmoptionsthat you can open by selecting Help -> Edit Custom VM Options...from Android Studio menu.

~/Library/Preferences/AndroidStudioX.X/studio.vmoptions,你可以选择打开帮助- >编辑自定义虚拟机选项...在Android Studio菜单。

And then you can use it like:

然后你可以像这样使用它:

System.env.YOUR_VARIABLE

in build.gradleor settings.gradle.

build.gradlesettings.gradle 中