John
John

Reputation: 613

Android project structure based on features

I am currently using the default android folder structure. It's now becoming very hard to manage the activities and views. I was wonder if there way to structure my project layouts and activities by features.

For example:

Following is my current project structure.

Java
  -Activity
    OrderListActivity.java
    ProductListAcitvity.java
Res
  -Layout
    OrderListView.xml
    ProductView.xml

I am trying to find a way to combine the view and activities into a same folder like:

 Java
  -Features
    -OrderList
        OrderListActivity.java
        OrderListView.xml
    -Products
        ProductListActivity.java
        ProductView.xml

Is this structure possible with gradle? if so could you please provide some samples gradle settings to achieve this.

Thanks

Upvotes: 0

Views: 598

Answers (3)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You can't place a java source file with layout file in the same directory. But you can organize each layout by features with a slight hack in your app build.gradle.

First, create a root directory for the layouts in res directory. The name should be layouts:

res/layouts

Then, you can create a directory for the layout based on the features. We use OrderList and Products. Each directory needs a layout directory inside of them. Now create the directories in the layouts like this:

res/layouts/orderlist/layout
res/layouts/products/layout

Then, add the following code to your app build.gradle:

android {

     ...

      sourceSets {
        main {
          res.srcDirs = ['src/main/res',
                         'src/main/res/layouts',
                         'src/main/res/layouts/orderlist',
                         'src/main/res/layouts/products',
        }
      }

   ...

}

Change your project to project view like this:

enter image description here

Sync your project to update the build.gradle. Now, you can add your layout inside the orderlist and products

Upvotes: 1

HaroldSer
HaroldSer

Reputation: 2065

You cannot have java files with xml resources inside the same folder. They belong to different categories. What you could do is associate the xml files with their corresponding java files by using appropriate naming conventions.

Upvotes: 0

Nabin Bhandari
Nabin Bhandari

Reputation: 16409

In short, not possible.

If your app contains a large amount of files, you can separate them into different modules.

Upvotes: 0

Related Questions