问题描述
我对 Android Studio 和 Java 非常陌生.
I am very new to Android Studio and Java.
我正在开发应用程序.这个应用程序将基本上遵循分层架构,具有不同的层,例如,UI、数据访问层、服务层等.
I am in the process of developing an app. This app will basically follow a layered architecture, with different layers, for instance, the UI, Data Access Layer, Service layer etc.
我不清楚包和模块之间的区别.
I am not clear on the differences between packages and modules.
我的问题是,将所有这些不同的层放在模块中还是包中?
My question is, where would one put all these different layers, in modules, or packages?
指向@Angel 对 this 问题,两者之间的唯一区别是\,模块定义了更严格的规则,谁可以访问它们,必须导入模块的命名空间.
Pointing to @Angel reponse to this question, the only difference between the two is \, modules define a more strict rule by who can access them, by having to import the namespaces for the modules.
推荐答案
A module 是项目中源代码的容器.一个项目可以有多个模块,但每个模块都是一组独立的代码和资源.
A module is the container for the source code within a project. A single project can have multiple modules, but each module is a separate set of code and resources.
例如,当您使用默认设置创建一个新项目时,Android Studio 会生成一个名为 app
的模块.此模块包含您的应用程序的所有源代码、资源文件和应用级设置.
For instance, when you create a new project with the default settings, Android Studio generates a module called app
. This module holds all of the source code, resource files and app-level settings for your application.
但是,如果您使用手机/平板电脑应用程序以及 Android Wear 应用程序创建一个新项目,您将看到两个模块;mobile
和 wear
.这些模块中的每一个都包含各自应用程序的源代码、资源文件和应用程序级设置.
But, if you create a new project with a Phone/Tablet application as well as an Android Wear application, you will see two modules; mobile
and wear
. Each of these modules contain the source code, resource files and app-level settings for their respective application.
您还可以创建另一个模块在多个模块之间共享;此模块将被视为库模块.
You can also create another module to be shared between multiple modules; this module would be considered a library module.
包 是本质上是源代码所属的目录(文件夹).通常,这是一个唯一标识您的应用程序的目录结构;例如 com.example.app
.然后你可以在你的应用程序包中创建包来分隔你的代码;例如 com.example.app.ui
或 com.example.app.data
.
A package is essentially the directory (folder) to which source code belongs. Normally, this is a directory structure that uniquely identifies your application; such as com.example.app
. Then you can create packages within your application package that separates your code; such as com.example.app.ui
or com.example.app.data
.
因此,为了回答您的问题,每个应用程序的包都位于应用程序模块的 src/main/java
目录中.您可以在应用程序包中放置一个单独的包,以分隔应用程序架构的每个层".
Therefore, to answer your question, the package for each application resides within the src/main/java
directory of the application module. You could place a separate package within the application pacakge to separate each "layer" of your application architechture.
仅作为视觉示例,这是我的一个项目的基本结构:
Just for a visual example, this is the basic structure for one of my projects:
project
|-- build.gradle
|-- settings.gradle
~
|-- common // a common library module for both mobile and wear
| |-- build.gradle
| |-- proguard-rules.pro
| +-- src
| +-- main
| |-- AndroidManifest.xml
| |-- assets
| |-- java
| | +-- com
| | +-- example
| | +-- library // common module library package
| | |-- data
| | +-- util
| +-- res
|
|-- mobile // mobile application module
| |-- build.gradle
| |-- proguard-rules.pro
| +-- src
| +-- main
| |-- AndroidManifest.xml
| |-- assets
| |-- java
| | +-- com
| | +-- example
| | +-- app // mobile module application package
| | |-- data
| | |-- ui
| | +-- util
| +-- res
|
+-- wear // wear application module
|-- build.gradle
|-- proguard-rules.pro
+-- src
+-- main
|-- AndroidManifest.xml
|-- assets
|-- java
| +-- com
| +-- example
| +-- app // wear module application package
| |-- data
| |-- ui
| +-- util
+-- res
这篇关于模块与包 Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!