📅  最后修改于: 2023-12-03 15:27:19.743000             🧑  作者: Mango
在 Android 应用程序开发中,项目的构建和管理是必不可少的一部分。同时,保证项目的干净和可维护性也是非常重要的。本文介绍了如何使用 Shell-Bash 脚本来创建离子等效的 Android clean 项目。
Android clean 项目是一种经过精心设计的项目结构,用于保持项目的干净和可维护性。它还具有良好的模块化和扩展性,使得您可以更轻松地添加和删除新的功能。
Android clean 项目的结构通常如下所示:
project
│ README.md
│ build.gradle
│ settings.gradle
│ local.properties
│
└───app
│ │ build.gradle
│ │
│ └───src
│ │ main
│ │ test
│
└───data
│ │ build.gradle
│ │
│ └───src
│ │ main
│ │ test
│
└───domain
│ │ build.gradle
│ │
│ └───src
│ │ main
│ │ test
│
└───presentation
│ │ build.gradle
│ │
│ └───src
│ │ main
│ │ test
app
模块用于定义应用程序的 UI 界面以及其加载逻辑。主要包括 Activity、Fragment、View 等。data
模块用于定义数据访问逻辑。包括数据库、网络请求、文件访问等。domain
模块用于定义业务逻辑,也就是应用程序的核心功能。presentation
模块用于定义呈现逻辑,例如 ViewModel、Presenter 等。它还负责连接 domain
和 app
模块。创建一个离子等效的 Android clean 项目并不困难。您只需要遵循以下步骤即可:
在您的项目目录中创建 create_android_clean_project.sh
文件。
在 create_android_clean_project.sh
文件中,输入以下内容:
#!/bin/bash
echo "Enter your project name: "
read project_name
mkdir -p "$project_name"/app/src/main/java/
mkdir -p "$project_name"/app/src/test/java/
mkdir -p "$project_name"/data/src/main/java/
mkdir -p "$project_name"/data/src/test/java/
mkdir -p "$project_name"/domain/src/main/java/
mkdir -p "$project_name"/domain/src/test/java/
mkdir -p "$project_name"/presentation/src/main/java/
mkdir -p "$project_name"/presentation/src/test/java/
touch "$project_name"/settings.gradle
echo "include ':app', ':data', ':domain', ':presentation'" > "$project_name"/settings.gradle
touch "$project_name"/app/build.gradle
echo "apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
applicationId 'com.example.$project_name'
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName '1.0'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
testOptions {
unitTests.returnDefaultValues = true
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}" > "$project_name"/app/build.gradle
touch "$project_name"/data/build.gradle
echo "apply plugin: 'com.android.library'
android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName '1.0'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.20'
implementation 'androidx.core:core-ktx:1.3.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}" > "$project_name"/data/build.gradle
touch "$project_name"/domain/build.gradle
echo "apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs.kotlin'
android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName '1.0'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.20'
implementation 'androidx.core:core-ktx:1.3.2'
kapt 'com.android.databinding:compiler:3.1.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation deps.koin_androidx_view_model('2.2.0')
implementation deps.mockitoAndroid('3.10.0')
implementation project(':data')
implementation deps.retrofit2('2.9.0')
implementation deps.retrofit2_converter_gson('2.9.0')
testImplementation deps.mockito('4.2.0')
testImplementation deps.robolectric('4.5.1')
}" > "$project_name"/domain/build.gradle
touch "$project_name"/presentation/build.gradle
echo "apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName '1.0'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
filter {
abiFilters 'armeabi-v7a', 'x86'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation deps.appcompat('1.3.0')
implementation deps.lifecycleExtensions('2.2.0')
implementation deps.koinAndroidxViewmodel('2.2.0')
implementation deps.constraintlayout('2.0.4')
implementation deps.kotlintStdlib('1.5.20')
implementation deps.navigationFragment('2.4.0-alpha02')
implementation deps.navigationUI('2.4.0-alpha02')
implementation deps.navigationSafeArgsGradlePlugin('2.1.2')
implementation project(':data')
implementation deps.retrofit2('2.9.0')
implementation deps.retrofit2_converter_gson('2.9.0')
implementation deps.picasso('2.71828')
implementation deps.rxAndroid('2.1.1')
implementation deps.rxJava('2.2.12')
implementation deps.timber('4.6.1')
implementation deps.material('1.3.0')
implementation deps.lottie('3.0.7')
implementation deps.viewpager2('1.0.0')
implementation deps.dexter('6.0.1')
implementation deps.fragmnetKtx('1.3.1')
implementation deps.coreKtx('1.3.2')
implementation deps.autodispose('1.4.0')
implementation deps.moshi('1.11.0')
kapt deps.picassoCompiler('2.71828')
testImplementation deps.mockito('4.2.0')
testImplementation deps.robolectric('4.5.1')
}" > "$project_name"/presentation/build.gradle
touch "$project_name"/app/src/main/AndroidManifest.xml
echo "<manifest xmlns:android='http://schemas.android.com/apk/res/android'
package='com.example.$project_name'>
<application
android:name='androidx.multidex.MultiDexApplication'
android:allowBackup='false'
android:icon='@mipmap/ic_launcher'
android:label='@string/app_name'
android:roundIcon='@mipmap/ic_launcher_round'
android:supportsRtl='true'
android:theme='@style/AppTheme'>
<activity
android:name='.MainActivity'
android:label='@string/app_name'
android:theme='@style/AppTheme.NoActionBar'>
<intent-filter>
<action android:name='android.intent.action.MAIN' />
<category android:name='android.intent.category.LAUNCHER' />
</intent-filter>
</activity>
</application>
</manifest>" > "$project_name"/app/src/main/AndroidManifest.xml
3. 保存 `create_android_clean_project.sh` 文件并运行以下命令:
```bash
chmod +x create_android_clean_project.sh
./create_android_clean_project.sh
在终端中输入您的项目名称,并按 Enter 键。
等待脚本运行完毕。此时您就已经成功创建一个离子等效的 Android clean 项目了。
本文介绍了创建离子等效的 Android clean 项目的步骤。这些步骤可以帮助您更轻松地构建并管理您的 Android 应用程序项目。使用本文介绍的脚本可以提高效率,同时也可以为您的项目增加可维护性。