本文翻译自:Where do I place the 'assets' folder in Android Studio?
I am confused about the assets
folder. 我对assets
文件夹感到困惑。 It doesn't come auto-created in Android Studio, and almost all the forums in which this is discussed talk about Eclipse. 它不是在Android Studio中自动创建的,讨论该主题的几乎所有论坛都谈论Eclipse。
How can the Assets directory be configured in Android Studio? 如何在Android Studio中配置Assets目录?
#1楼
参考:https://stackoom.com/question/1EnLH/如何在Android-Studio中将-资产-文件夹放在哪里
#2楼
Since Android Studio uses the new Gradle-based build system , you should be putting assets/
inside of the source sets (eg, src/main/assets/
). 由于Android Studio使用新的基于Gradle的构建系统 ,因此您应将assets/
放在源集中 (例如src/main/assets/
)。
In a typical Android Studio project, you will have an app/
module, with a main/
sourceset ( app/src/main/
off of the project root), and so your primary assets would go in app/src/main/assets/
. 在一个典型的Android Studio项目中,您将有一个app/
模块,具有一个main/
源码集( app/src/main/
在项目根目录之外),因此您的主要资产将进入app/src/main/assets/
。 However: 然而:
If you need assets specific to a build, such as
debug
versusrelease
, you can create sourcesets for those roles (eg,.app/src/release/assets/
) 如果您需要特定于构建的资产(例如debug
与release
,则可以为这些角色创建源集(例如app/src/release/assets/
)。Your product flavors can also have sourcesets with assets (eg,
app/src/googleplay/assets/
) 您的产品口味还可以包含带有资产的资源集(例如app/src/googleplay/assets/
)Your instrumentation tests can have an
androidTest
sourceset with custom assets (eg,app/src/androidTest/assets/
), though be sure to ask theInstrumentationRegistry
forgetContext()
, notgetTargetContext()
, to access those assets 您的检测测试可以具有带有自定义资产的androidTest
(例如app/src/androidTest/assets/
),尽管一定要向InstrumentationRegistry
请求getContext()
而不是getTargetContext()
来访问这些资产
Also, a quick reminder: assets are read-only at runtime. 另外,还有一个简短的提醒:资产在运行时是只读的。 Use internal storage , external storage , or the Storage Access Framework for read/write content. 使用内部存储 , 外部存储或Storage Access Framework读取/写入内容。
#3楼
Looking inside the .iml file of your project you will see the following line: 查看项目的.iml文件,您将看到以下行:
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
This means the "assets" folder is already declared for Gradle. 这意味着已经为Gradle声明了“ assets”文件夹。 You will need to create it under src/main/
(I'm using Android Studio 0.4.2). 您需要在src/main/
下创建它(我使用的是Android Studio 0.4.2)。
#4楼
In android studio you can specify where the source, res, assets folders are located. 在android studio中,您可以指定来源,资源,资产文件夹的位置。 for each module/app in the build.gradle file you can add something like: 对于build.gradle文件中的每个模块/应用,您可以添加以下内容:
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
sourceSets {
main {
java.srcDirs = ['src']
assets.srcDirs = ['assets']
res.srcDirs = ['res']
manifest.srcFile 'AndroidManifest.xml'
}
}
}
#5楼
When upgrading to the release version of Android Studio, you may be automatically switched to the new Android project View (see here for more info). 升级到Android Studio的发行版时,您可能会自动切换到新的Android项目视图(有关更多信息,请参见此处 )。 If you swap back to either the Project or Packages view, you should see the standard folder hierarchy of a Gradle-based project. 如果交换回“项目”或“包”视图,则应看到基于Gradle的项目的标准文件夹层次结构。 Then refer to CommonsWare's answer for the appropriate location. 然后,参考CommonsWare的答案以获取适当的位置。
#6楼
Let Android Studio do it for you. 让Android Studio为您完成。
- In Android Studio ( 1.0 & above ), right-click on the 在Android Studio( 1.0及更高版本 )中,右键单击
folder and navigate to the
Assets Folder
. 文件夹并导航到Assets Folder
。
- On the next screen just click
Finish
. 在下一个屏幕上,单击Finish
。
And voila! 瞧! It will create the assets
folder in the main
target source set. 它将在main
目标源集中创建assets
文件夹。