On the file structure of the project in the development of Android and the standardized deployment suggestion _java

Source: Internet
Author: User

A few words.

    • Using Gradle and its recommended project framework
    • Put sensitive data such as passwords into Gradle.properties
    • Do not write HTTP clients yourself, use volley or okhttp libraries
    • Use the Jackson Library to parse JSON data
    • Avoid guava and Dalvik 65K methods limit do not use too many libraries
    • Using fragment to draw UI interfaces
    • Activity is mainly used to manage fragment
    • Layout file XML is also code, organize them well
    • In the layout file, use styles to avoid duplicate properties
    • Use multiple style files instead of a huge style file
    • Keep your color.xml short and dry, define the color plate
    • Also keep Dimens.xml DRY, define Universal constants
    • Do not create a layout that is too deep
    • Avoid WebView client processing, and be aware of memory leaks
    • Use Robolectric for unit testing, Robotium for connection (UI) testing
    • Simulator Genymotion
    • Be sure to use Proguard or Dexguard

Second, detailed
Android SDK
Place your Android SDK in your home directory or other places that have nothing to do with the application. Some Ides will associate the SDK on installation and place the SDK in the same directory as the IDE. When you need to upgrade (reload) the IDE or change the IDE, you will find the bad. Also, if your IDE is running under a user account instead of root, don't put the SDK in a system-level directory, or you'll need sudo when you use it.

Build System
The default choice is Gradle. The ant limit is more and too large. Using Gradle, you can easily do this:
-Compile different flavours or application variants
-Create a simple class-Script task
-Manage and download dependencies
-Custom Keystores
-Wait.
Android's Gradle plug-in has also been designated by Google as the new standard-compiling system, and Google is constantly upgrading it.

Project structure
There are two popular choices: the Old Ant & Eclipse ADT Project structure, the new Gradle & Android Studio project structure. You should choose the latter. If your project uses the old structure, then replace it.
Old structure

Old-structure 
├─assets
├─libs
├─res
├─src
│└─com/futurice/project Androidmanifest.xml
├─build.gradle
├─project.properties
└─proguard-rules.pro

New structure

New-structure
├─library-foobar
├─app
│├─libs
│├─src
││├─androidtest 20/>│││└─com/futurice/project
││└─main
││├─java
│││└─com/futurice/project
││├─res
   ││└─androidmanifest.xml
│├─build.gradle
│└─proguard-rules.pro
├─build.gradle
└─ Settings.gradle

The main difference of the new structure is that the ' source code set ' (Main,androidtest) is split, which comes from the Gradle concept.
Using the highest level of "app" is a good place to differentiate your app from other library items (such as: Library-foobar) that your application references. Settings.gradle then keeps the index of the libraries applied to them, and App/build.gradle can point to the libraries.

Gradle Configuration
General architecture Please follow Google's Guide on gradle for Android;
Small tasks (scripts), you can use Gradle to make small tasks rather than shell, Python or Perl, specifically refer to Gradle ' s documentation;
Password. In the build.gradle you apply, you need to define Signingconfigs for release compilation. Specifically as follows:
Don't write like this, it will appear in your version control system:

Signingconfigs {release 
  {  
   storefile file ("Myapp.keystore")  
   Storepassword "password123"
   Keyalias " Thekey "
   keypassword" password789 "
  }
}

Instead, you should create a gradle.properties file that will not be added to the version control system.

keystore_password=password123
key_password=password789

This file will be automatically imported by Gradle, so you can use it in Build.gradle:

Signingconfigs {release 
  {
  try{
    storefile file ("Myapp.keystore")  
    Storepasswordkeystore_password  
    Keyalias "Thekey"
    Keypasswordkey_password
 } catch (ex) {
  thrownewinvaliduserdataexception ("You Should define Keystore_password and Key_password in Gradle.properties. ")}}}
 

(If you use Maven to refer to the original document)

Library
Jackson is a Java library that enables you to convert objects and JSON data to and from each other. Gson is also a similar good choice. But we think Jsckson better because it supports a variety of ways to handle JSON: streaming, Memory tree model and drive Json-pojo data binding. But, remember, Jackson is bigger than Gson, so you have to consider, if you want to avoid 65k methods limit it is better to use Gson. Other choices: Json-smart and Boon Json
Network, caching, and pictures. Use volley or retrofit. Volley can also be used to load and cache pictures. If you choose Retrofit, you can use Picasso to load and cache pictures. Then use Okhttp to execute a valid HTTP request. These three kinds: retrofit, Picasso and Okhttp all come from the same company, so they complement each other. Okhttp can be used to connect with volley. The
Rxjava is a responsive programming library, in other words, handling asynchronous events. (Refer to the original document)
Retrolambda is a Java library that helps you use lambda expressions on Android or other platforms earlier than the JDK8 platform. (Specific reference to the original document)
Finally, remember the Dex method limit, do not use too many libraries. (Android apps, when packaged as a Dex file, have a maximum limit: 65,535 reference methods [1][2][3]. If you exceed the limit, a serious error will occur. Therefore, instead of using just too many libraries, use the Dex-method-counts tool to decide which classes to use to keep within limits, especially to avoid using the guava library because it contains more than 13k methods)
activities and fragments The
fragments should be your default option for deploying the UI interface on Android. Fragments can be reused in your application. We recommend using fragements instead of activities to draw the interface based on the following points:

    • Resolution is a multiple view layout. The main reason for fragments's introduction is to extend the mobile app to a platform computer so that you can display a and B views on the tablet, and only a or B on the phone. If your program starts with fragment, your program can be more easily applied to a variety of devices.
    • screen to screen communication. The Android API does not provide an appropriate way to implement complex data (such as Java objects) between the activity. However, using fragment, you can use the activity to become a channel for communication between its child fragment. Even though this is a lot better than activity-to-activity, I recommend that you take the event bus architecture, such as using Otto or Greenrobot Eventbus as a simpler solution. If you don't want to take additional libraries, then Rxjava can also be used to implement a eventbus.
    • Fragment not only can be used to lay out the UI. You can add a fragment with no UI interface as the background service for the activity. You can even create a fragment to implement fragment switching logic instead of letting the activity deal with fragment switching logic.
    • Fragment can even manage Actionbar. You can select a fragment that does not have a UI interface to specialize in managing Actionbar, or you can select each of the currently visible fragment to handle the actionbar of the parent activity. Refer to here.
    • Then, we do not recommend overuse of nested fragments, which can lead to matryoshka vulnerabilities.
    • From an architectural perspective, your application should have a top-level activity that contains most of the business-related fragments. You can also create some other support activities as long as they are simple to communicate with the main activity--the likeness of Intent.setdata () or intent.setaction ().

Java Package Structure

The Java architecture in the Android application is close to the MVC structure (model-view-controller). In Android, fragment and activity are actually controller classes. And from another point of view, they are part of the user interaction, which means they belong to the view class.
It is therefore difficult to distinguish strictly between the controller or the view of the fragment (or activity). So from the Java package Point of view, we'd better put fragment in their own fragments package, and then the activity would be in the top of the package (follow the recommendations set out above). Of course, if you want to have 2 or more activity, you create a activitys package.
In this case, the entire structure looks like a typical MVC structure. A models package contains POJOs to transform the JSON data obtained by the API interface, and a views package contains Views,notifications,action bar views,widgets. Adapters is a middle tier, located between data and views. However, they usually need to getview () to Output view view, so you can put adapters in the child package location of the views package.
Some application-level, Android-only controller classes should be placed in the managers package. Various types of data processing, such as dateutils, can be placed in utils packs. Classes that interact with the backend server should be placed in the network package.
In summary, the overall architecture from interacting with the server to interacting with the user can be designed as follows:

Com.futurice.project
├─network
├─models
├─managers
├─utils
  ├─fragments ├─views ├─ Adapters
  ├─actionbar
  ├─widgets
  ├─notifications

Resource file
Named
follow the prefix formula, similar to Type_foo_bar.xml, such as: Fragment_contact_details.xml,view_primary_button.xml,activity_main.xml.

Organize layout files
If you don't know how to normalize a layout XML file, refer to the following conventions:

    • One attribute per line, indented 4 spaces;
    • Android:id always on the first one;
    • The android:layout_** attribute should be placed at the top;
    • The Style property is placed at the tail;
    • End flag bit/> to monopolize a row, to help sort or add a property;
    • Instead of writing hard code, such as Android:text, you can consider using the Designtime attributes method for Android studio.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
 "http://schemas.android.com/" Apk/res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:layout_width=" Match_parent "
 android:layout_height= "match_parent"
 android:orientation= "vertical"
>

  <textview
   Android:id= "@+id/name"
   android:layout_width= "match_parent"
   android:layout_height= "Wrap_content"
   android:layout_alignparentright= "true"
   android:text= "@string/name"
   style= "@style/fancytext"
  />

  <include layout= "@layout/reusable_part"/>

</LinearLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.