Where to place the 'assets' folder in Android Studio? - android-studio

I am confused about the assets folder. It doesn't come auto-created in Android Studio, and almost all the forums in which this is discussed talk about Eclipse.
How can the Assets directory be configured in Android Studio?

Since Android Studio uses the new Gradle-based build system, you should be putting assets/ inside of the source sets (e.g., 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/. However:
If you need assets specific to a build type, such as debug versus release, you can create sourcesets for those roles (e.g,. app/src/release/assets/)
Your product flavors can also have sourcesets with assets (e.g., app/src/googleplay/assets/)
Your instrumentation tests can have an androidTest sourceset with custom assets (e.g., app/src/androidTest/assets/), though be sure to ask the InstrumentationRegistry for getContext(), not getTargetContext(), to access those assets
Also, a quick reminder: assets are read-only at runtime. Use internal storage, external storage, or the Storage Access Framework for read/write content.

Let Android Studio do it for you.
In Android Studio (1.0 & above), right-click on the folder and navigate to the Assets Folder.
On the next screen just click Finish.
And voila! It will create the assets folder in the main target source set.

Looking inside the .iml file of your project you will see the following line:
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
This means the "assets" folder is already declared for Gradle. You will need to create it under src/main/ (I'm using Android Studio 0.4.2).

Select the app folder and then:
File > New > folder > assets Folder ,
the default location is inside /main folder

First of all the "Assets" folder will not be created automatically with the project. We have to create it.
The location of Assets folder is: App > src > Assets
Please have a look of the simple image below.
Note: For creating assets folder just click on Project => Right click => Select New => Folder => Assets. It will create Assets folder.

It's simple, follow these steps
File > New > Folder > Assets Folder
Note : App must be selected before creating folder.

In android studio you can specify where the source, res, assets folders are located.
for each module/app in the build.gradle file you can add something like:
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
sourceSets {
main {
java.srcDirs = ['src']
assets.srcDirs = ['assets']
res.srcDirs = ['res']
manifest.srcFile 'AndroidManifest.xml'
}
}
}

Simply, double shift then type Assets Folder
choose it to be created in the correct place

Click over main → new -> directory → and type as name "assets"
or... main -> new -> folder -> assets folder (see image)

In Android Studio 4.1.1
Right Click on your module (app for example) -> New -> Folder -> Assets Folder

File > New > folder > assets Folder

Project -> app -> src -> main -> RMB(right mouse button) -> New -> Directory:

Two ways:
Select app/main folder, Right click and select New => Folder => Asset Folder.
It will create 'assets' directory in main.
Select main folder, Right click and select New => Directory
Enter name as 'assets' = > Ok.

If you tried all your bullets in this thread in vain try cleaning your project . In my case it only worked after Projet -> clean

right click on app folder->new->folder->Assets folder->set Target Source set->click on finish button

Put the assets folder in the main/src/assets path.

Src/main/Assets
It might not show on your side bar if the app is selected. Click the drop-down at the top that says android and select packages. you will see it then.

need configure parameter for gradle
i hope is will work
// file: build.gradle
sourceSets {
main {
assets.srcDirs = ['src/main/res/icon/', 'src/main/assets/']
}
}

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). If you swap back to either the Project or Packages view, you should see the standard folder hierarchy of a Gradle-based project. Then refer to CommonsWare's answer for the appropriate location.

In Android Studio, click on the app folder, then the src folder, and then the main folder. Inside the main folder you can add the assets folder.

Step 1 : Go to Files.
Step 2 : Go to Folders.
Step 3 : Create Assets Folder.
In Assets folder just put fonts and use it if needed.

follow these steps
1)file->New->Folder
there are multiple options like
aidl folder
assets folder
jni folder
2) choose options assets folder
3) then there is option to change path of assets folder if you
want to change then check otherwise left that checkbox of cahnge folder location
4) click on finish

Either create a directory under /app/src/main or use studio File-> New -> Folder - > Assets Folder.

In Android Studio right-click Folder in app->src->main then create new DIRECTORY name that assets.

It seems that nobody mentioned this:
You can define it in Project Structure > Project Settings:

Related

How do I create an assets folder using current Android Studio (3.5.3)?

Does anyone know how to create an assets folder in an Android project? There are lots of answers to this from about 5 years ago, but they do not work with the current version of Android Studio (3.5.3).
Go through File > New > Folder > Assets folder.
Ne need to check change folder location.
The assets folder's default location is inside of app/src/main folder.
Just move to that main directory inside YourProjectName -> app -> src -> main
Right click -> New -> Directory -> Type assets -> OK
Thats it.

How to acess new created folder in android studio?

I created a folder in res folder and it named draw2 which i will use for specisific imageviews.I want to access the images inside this folder how to do it?
You cannot. Custom folders in res folder are not supported.
I created a folder in res folder and it named draw2 which i will use for specisific imageviews.
Custom folders in res folder are not supported. Better To create raw folder or asset folder
for this purpose
raw/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
How to create assets in android studio
How to create raw in android studio
for more information The Android Project View
module-name/
build/
libs/
src/
androidTest/
main/
AndroidManifest.xml
java/
jni/
gen/
res/
assets/
test/
build.gradle (module)
build.gradle (project)
Just Put the images you trying to access into your (drawable) folder, because in android the res folder has specific folder names like (values,mipmap,anim,drawable ...) , and you can't create new folder name , it will not be recognized.

How to use npm packages with ASP.NET CORE 2 in Visual Studio 2017?

I added some npm packages to my ASP.NET Core 2 project in Visual Studio 2017. Now I want to use css and js files from these packages, but VS doesn't see them because node_modules folder is outside wwwroot. What is the common practice here, how to make Visual Studio working with node_modules?
As soon as you add the desired npm packages to your project, either through:
npm install command
or
typing the package name and its version in the npm configuration file
or
searching the package in the Library Dialog, indicated in the following image for instance,
they will be downloaded and restored in the node_modules folder. But to use them in your views, you need to add them to a client-side folder, such as lib under wwwroot folder.
You can copy them to wwwroot folder manually through the file system, or use the Library dialog:
right click on the lib directory inside wwwroot
Click Add, and choose Client-Side Library
From the provider, choose fileSystem (if desired files already exist in node_modules folder)
Choose all or specific files needed
Click install
However, you can manage this file copying operation manually.
Now, from the folders under wwwroot, you can drag and drop the file into your views. Be careful to omit ~ if your view is a Layout.
Hope this helped....
More details here: https://learn.microsoft.com/en-us/aspnet/core/client-side/libman/libman-vs?view=aspnetcore-3.0
The common practice is to bundle your web assets, and put only the compiled bundle into your wwwroot folder.
Since Visual Studio 2015 we have multiple Taskrunners from the NPM World:
Gulp
Grunt
With them you could write a script, which automatically bundles your web assets. This script has full access to the NPM infrastructure.
There are some tools out there which make this super easy:
gulp-uglify
gulp-cssmin
More Information:
https://learn.microsoft.com/en-us/aspnet/core/client-side/using-gulp
That said, it is also possible to include your npm folder:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "node_modules")),
RequestPath = "/node_modules"
});
}
But this would be the wrong way.

Android studio configuration with Gradle

I am having an issue to configure Android studio after importing gradle project. My project structures is as following:
Root Project
|_.config
|_settings.gradle
|_build.gradle
|_app_module
|_lib1_module
|_lib2_module
|_ etc.
Please note, that for several reasons I am not able to move settings.gradle and build.gradle files to project root . Also, the files mentioned above are configured correctly, so there is no problem with the build.
So the problem is that once the project in imported, the all gradle related file and folders are are generated inside .config folder, which doesn't disturd me.
Also the .idea folder and all relevant sub-folders are generated in this very folder.
So this is the problem. For some reason, the studio doesn't recognize all sub-modules are under source control. I.e. it's vcs.xml is empty and contains only project root. Actually the problem is resolved by adding the modules via settings--> subversion menu, but I'd really like out-of-the-box solution. I've tried to put .idea folder under source-control - doesn't help. Upon project creation, the vcs.xml is re-written and empty again.
So, the main question is:
Is there a possibility to
Force studio to generate .idea folder and the root project folder without changing location of .gradle files OR
Make studio NOT to re-write it's settings files upon project import (e.g. keep predefined vcs.xml)
PS. I'm aware the putting all generated files (including gradle folder, gradlew, etc.) will probably solve the issue, but I wouldn't like to go so far.
Thank all in advance
It should be a comment, but it is too long.
Android Studio works with a project.
Inside this project you can configure more modules but it need this structure:
Root Project
|_.idea
|_.gradle
|_.config
|_build.gradle
|_app_module
|_build.gradle
|_lib1_module
|_build.gradle
|_lib2_module
|_build.gradle
|_ etc.
|_settings.gradle
|_build.gradle
Inside the settings.gradle you have to define all modules:
include ':config' , ':app_module' , ':lib1_module', ':lib2_module'
Under the root folder, Android Studio when builds the project with gradle, makes same folders, like the .idea.
I read that your are not able to move the settings.gradle file.
In this case Android Studio recognizes the config folder as the root of a project and build the .idea folder inside.
|_.config (root)
|_.idea
|_.gradle
|_build.gradle
|_settings.gradle
|_myModule
Force studio to generate .idea folder and the root project folder without changing location of .gradle files OR
As I know you can't avoid this.
Also ,
Make studio NOT to re-write it's settings files upon project import
The .idea, the .gradle folder, the .iml files are built locally by the IDE when the project is built. The IDE re-writes these resources, also these files contain local paths and usually they are not commited in VCS for this reason.

How to create a library project in Android Studio and an application project that uses the library project

How do I create an Android Library Project (e.g. com.myapp.lib1) and the application project (e.g. com.myapp.app) and make the build system include com.myapp.lib1 on the application project?
I went to the Project Structure -> Modules -> My App project and added a dependency to the lib project. IntelliJ now can recognize classes from the lib project when used in the app project, but when I run the app project, there are errors like:
Gradle: error: package com.myapp.lib1 does not exist
I wonder why there is no example of stand alone jar project.
In eclipse, we just check "Is Library" box in project setting dialog.
In Android studio, I followed this steps and got a jar file.
Create a project.
open file in the left project menu.(app/build.gradle): Gradle Scripts > build.gradle(Module: XXX)
change one line: apply plugin: 'com.android.application' -> 'apply plugin: com.android.library'
remove applicationId in the file: applicationId "com.mycompany.testproject"
build project: Build > Rebuild Project
then you can get aar file: app > build > outputs > aar folder
change aar file extension name into zip
unzip, and you can see classes.jar in the folder.
rename and use it!
Anyway, I don't know why google makes jar creation so troublesome in android studio.
To create a library:
File > New Module
select Android Library
To use the library add it as a dependancy:
File > Project Structure > Modules > Dependencies
Then add the module (android library) as a module dependency.
Run your project. It will work.
Google’s Gradle Plugin recommended way for configuring your gradle files to build multiple projects has some shortcomings If you have multiple projects depending upon one library project, this post briefly explain Google’s recommended configuration, its shortcomings, and recommend a different way to configure your gradle files to support multi-project setups in Android Studio:
An alternative multiproject setup for android studio
A Different Way :
It turns out there’s a better way to manage multiple projects in Android Studio. The trick is to create separate Android Studio projects for your libraries and to tell gradle that the module for the library that your app depends on is located in the library’s project directory. If you wanted to use this method with the project structure I’ve described above, you would do the following:
Create an Android Studio project for the StickyListHeaders library
Create an Android Studio project for App2
Create an Android Studio project for App1
Configure App1 and App2 to build the modules in the StickyListHeaders project.
The 4th step is the hard part, so that’s the only step that I’ll describe in detail. You can reference modules that are external to your project’s directory by adding a project statement in your settings.gradle file and by setting the projectDir property on the ProjectDescriptor object that’s returned by that project statement:
The code one has to put in settings.gradle:
include ':library1'
project(':library1').projectDir = new File('../StickyListHeader/library1')
If you’ve done this correctly, you’ll notice that the modules referenced by your project will show up in the project navigator, even if those modules are external to the project directory:
This allows you to work on library code and app code simultaneously. Version control integration also works just fine when you reference modules externally this way. You can commit and push your modifications to the library code just like you can commit and push modifications to your app code.
This way of setting up multiple projects avoids the difficulties that plague Google’s recommended configuration. Because we are referencing a module that is outside of the project directory we don’t have to make extra copies of the library module for every app that depends on it and we can version our libraries without any sort of git submodule nonsense.
Unfortunately, this other way of setting up multiple projects is very difficult to find. Obviously, its not something you’ll figure out from looking at Google’s guide, and at this point, there’s no way to configure your projects in this way by using the UI of Android Studio.
Check out this link about multi project setups.
Some things to point out, make sure you have your settings.gradle updated to reference both the app and library modules.
settings.gradle: include ':app', ':libraries:lib1', ':libraries:lib2'
Also make sure that the app's build.gradle has the followng:
dependencies {
compile project(':libraries:lib1')
}
You should have the following structure:
MyProject/
| settings.gradle
+ app/
| build.gradle
+ libraries/
+ lib1/
| build.gradle
+ lib2/
| build.gradle
The app's build.gradle should use the com.android.application plugin while any libraries' build.gradle should use the com.android.library plugin.
The Android Studio IDE should update if you're able to build from the command line with this setup.
For Intellij IDEA (and Android Studio) each library is a Module. Think of a Module in Android Studio as an equivalent to project in Eclipse. Project in Android Studio is a collection of modules. Modules can be runnable applications or library modules.
So, in order to add a new android library project to you need to create a module of type "Android library". Then add this library module to the dependency list of your main module (Application module).
The simplest way for me to create and reuse a library project:
On an opened project file > new > new module (and answer the UI questions)
check/or add if in the file settings.gradle: include ':myLibrary'
check/or add if in the file build.gradle:
dependencies {
...
compile project(':myLibrary')
}
To reuse this library module in another project, copy it's folder in the project instead of step 1 and do the steps 2 and 3.
You can also create a new studio application project
You can easily change an existing application module to a library module by changing the plugin assignment in the build.gradle file to com.android.library.
apply plugin: 'com.android.application'
android {...}
to
apply plugin: 'com.android.library'
android {...}
more here
You can add a new module to any application as Blundell says on his answer and then reference it from any other application.
If you want to move the module to any place on your computer just move the module folder (modules are completely independent), then you will have to reference the module.
To reference this module you should:
On build.gradle file of your app add:
dependencies {
...
compile project(':myandroidlib')
}
On settings.gradle file add the following:
include ':app', ':myandroidlib'
project(':myandroidlib').projectDir = new File(PATH_TO_YOUR_MODULE)
Don't forget to use apply plugin: 'com.android.library' in your build.gradle instead of apply plugin: 'com.android.application'
Documentation Way
This is the recommended way as per the advice given in the Android Studio documentation.
Create a library module
Create a new project to make your library in. Click File > New > New Module > Android Library > Next > (choose name) > Finish. Then add whatever classes and resourced you want to your library.
When you build the module an AAR file will be created. You can find it in project-name/module-name/build/outputs/aar/.
Add your library as a dependency
You can add your library as a dependency to another project like this:
Import your library AAR file with File > New Module > Import .JAR/.AAR Package > Next > (choose file location) > Finish. (Don't import the code, otherwise it will be editable in too many places.)
In the settings.gradle file, make sure your library name is there.
include ':app', ':my-library-module'
In the app's build.gradle file, add the compile line to the dependencies section:
dependencies {
compile project(":my-library-module")
}
You will be prompted to sync your project with gradle. Do it.
That's it. You should be able to use your library now.
Notes
If you want to make your library easily available to a larger audience, consider using JitPac or JCenter.
Had the same question and solved it the following way:
Start situation:
FrigoShare (root)
|-Modules: frigoshare, frigoShare-backend
Target: want to add a module named dataformats
Add a new module (e.g.: Java Library)
Make sure your settings.gradle look like this (normally automatically):
include ':frigoshare', ':frigoShare-backend', ':dataformats'
Make sure (manually) that the build.gradle files of the modules that need to use your library have the following dependency:
dependencies {
...
compile project(':dataformats')
}
Purpose: Android library at single place - Share across multiple projects
http://raevilman.blogspot.com/2016/02/android-library-project-using-android.html
As theczechsensation comment above I try to search about Gradle Build Varians and I found this link: http://code.tutsplus.com/tutorials/using-gradle-build-variants--cms-25005
This is a very simple solution. This is what I did:
- In build.gradle:
flavorDimensions "version"
productFlavors {
trial{
applicationId "org.de_studio.recentappswitcher.trial"
flavorDimension "version"
}
pro{
applicationId "org.de_studio.recentappswitcher.pro"
flavorDimension "version"
}
}
Then I have 2 more version of my app: pro and trial with 2 diffrent packageName which is 2 applicationId in above code so I can upload both to Google Play. I still just code in the "main" section and use the getpackageName to switch between to version. Just go to the link I gave for detail.
There are two simplest ways if one does not work please try the other one.
Add dependency of the library inside dependency inside build.gradle file of the library u r using, and paste ur library in External Libraries.
OR
Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there Now the trick here is that now go inside settings.gradle file now add this line "include ':app:libs'" after "include ':app'" It will definitely work...........:)
In my case, using MAC OS X 10.11 and Android 2.0, and by doing exactly what Aqib Mumtaz has explained.
But, each time, I had this message : "A problem occurred configuring project ':app'. > Cannot evaluate module xxx : Configuration with name 'default' not found."
I found that the reason of this message is that Android 2.0 doesn't allow to create a library directly. So, I have decided first to create an app projet and then to modify the build.gradle in order to transform it as a library.
This solution doesn't work, because a Library project is very different than an app project.
So, I have resolved my problem like this :
First create an standard app (if needed) ;
Then choose 'File/Create Module'
Go to the finder and move the folder of the module freshly created in your framework directory
Then continue with the solution proposed by Aqib Mumtaz.
As a result, your library source will be shared without needing to duplicate source files each time (it was an heresy for me!)
Hoping that this help you.

Resources