Android projects have a specific structure, with a consistent set of directory and file names. The structure will vary depending on whether a project is created from the command line or using Android Studio. We focus on the Studio structure, on the project window on the left side of the screen. Get to that project window either by clicking on the tab or by pressing Alt+1 on Windows or Command+1 on Mac. You can also get to these windows by moving the cursor over the icon in the lower left corner.
- [Instructor] Android projects have a specific structure with a set of directory and file names that are always the same. The structure will vary depending on whether a project is created from the command line or using Android Studio. I'm going to focus on the structure created and maintained in Studio. I'm working with a project named Project Structure, that's a part of the exercise files. You can either open this project or you can work with the project that you created in the previous chapter. I'll be working in the project window over here.
You can get to the project window in a couple of different ways. You can click on the tab on the left, you can hold down the alt key on Windows or command on Mac and press one, and that will open and close that project window, you can hover the mouse over the lower left corner and choose project from there, or you can go to the menu and choose view, tool windows, project. The project window, by default, shows a scope named Android. As I mentioned earlier, this does not reflect the actual organization of the files and folders on disk.
It doesn't show everything that's in the project directory. Instead, it starts at what's known as the module directory. In Android Studio, a single project can have multiple modules, and your application is a module that, by default, is simply named app. The app directory shows a manifests directory, a Java directory, an RES directory, which stands for resources, and then there's a separate directory named gradle scripts. If you want to see the actual underlying structure of the files and folders, pull down the list of scopes and choose project.
And now you'll see that the folder that contains your project is named project structure, and there's that same app directory, which is your module. There are also directories named dot gradle and dot idea, which are visible here. Now, you can get to any of these directories in your file manager. That's finder on Mac or explorer on Windows, by right clicking on any directory here and then choose show in explorer or show in finder. And that will take you to the appropriate directory.
And you can do that with any of the sub-directories as well. If you wanted to get to your Java files, for example, you would drill down to source, main, Java, under the module directory, and then you could right click and once again show the actual directory. The reality though is that you don't need to deal with a lot of these files when you're doing application development. And that's why the Android scope is so useful. It filters down the view so you're only seeing what's really useful to you.
I'll start in the manifests directory. This virtual directory, meaning it's not a real directory on disk, contains a single file called Androidmanifest.xml. I'll talk through everything you need to do in the manifest later on in this chapter, but it's basically where you register all of your activities, that is, your screens, and also services, permissions, and other elements of your application. Next, there's the Java directory, and this contains three main packages.
The first is the Java code for the application itself, and by default, this new project contains a single Java file named mainactivity.java, and it represents your main activity or your main screen. The other two directories are the root packages for testing classes. There's one named example instrumented test in the Android test directory, and this is for running tests that are Android-specific, and then this one is named example unit test, and this is more for generic Java-based testing.
If you want to do test-driven development, these two areas are where you'll put your Java classes for running your tests. For basic application development though, you'll spend all of your time here in the application directory. The RES, or resources directory, contains a number of sub-directories. Drawable's where you put your graphics, layout is where you put XML files that define the appearance of your screens, menu is where you put other XML files that define menus, mip map contains a number of png files known as launcher icons, and values contains a bunch of XML files that define colors, dimensions, string values, and styles.
There are other directories you can create, such as one named XML and another named raw, and I'll be talking about many of these resource directories throughout this series of courses. Finally, there's the gradle scripts directory. There are two gradle scripts that are a part of every project that you create in Android Studio. The first is the project or the top level file. You typically don't make many changes to this file because these configure the entire project, not your specific application, but if you open up a project like this in a newer version of Android Studio, you'll need to update this line of code.
You'll actually be prompted automatically to do it by Android Studio when you open the project the first time after an upgrade. This version number needs to match your version of Android Studio. It references what's known as the gradle plugin, a bit of software that manages the build process. The other build dot gradle file is specific to your application, and it contains a lot of essential configuration information for your particular app. I'll talk through a lot of these properties in a later video.
So that's an introduction to the structure of an Android Studio project. When you build or package an Android app, you're taking all of these resources, Java files, graphical resources, XML-based resources, and all of the configurations that you create in your gradle build files and the Android manifest file, and these all taken together define your Android app.
- Installing Android Studio
- Creating your first Android Studio project
- Managing profile files, including Gradle scripts and support libraries
- Defining screens with activities
- Implementing designs in XML layouts