如何在Android Studio中将“资产”文件夹放在哪里?

本文翻译自: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 versus release , you can create sourcesets for those roles (eg,. app/src/release/assets/ ) 如果您需要特定于构建的资产(例如debugrelease ,则可以为这些角色创建源集(例如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 the InstrumentationRegistry for getContext() , not getTargetContext() , 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为您完成。

  1. In Android Studio ( 1.0 & above ), right-click on the 在Android Studio( 1.0及更高版本 )中,右键单击 在此处输入图片说明folder and navigate to the Assets Folder . 文件夹并导航到Assets Folder

在此处输入图片说明

  1. On the next screen just click Finish . 在下一个屏幕上,单击Finish

在此处输入图片说明

And voila! 瞧! It will create the assets folder in the main target source set. 它将在main目标源集中创建assets文件夹。

在此处输入图片说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值