Browse Source

1:修改组件化架构,将APP组件改成一个没有界面的壳工程,提取了一个新组件main,负责启动APP和主界面管理。

guiying712 8 years ago
parent
commit
81dbbe5af2

+ 1 - 0
app/build.gradle

@@ -53,6 +53,7 @@ dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
 
     if (!isModule.toBoolean()) {
+        compile project(':main')
         compile project(':girls')
         compile project(':news')
     } else {

+ 2 - 7
app/src/main/AndroidManifest.xml

@@ -9,6 +9,8 @@
         android:label="@string/app_name"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
+
+        <!--声明整个应用程序的路由协议-->
         <activity
             android:name="com.github.mzule.activityrouter.router.RouterActivity"
             android:theme="@android:style/Theme.NoDisplay">
@@ -22,13 +24,6 @@
             </intent-filter>
         </activity>
 
-        <activity android:name=".MainActivity">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>
-        </activity>
     </application>
 
 </manifest>

+ 2 - 2
app/src/main/java/com/guiying/androidmodulepattern/AppModule.java → app/src/main/java/com/guiying/androidmodulepattern/App.java

@@ -3,7 +3,7 @@ package com.guiying.androidmodulepattern;
 import com.github.mzule.activityrouter.annotation.Module;
 
 /**
- * <p>类说明</p>
+ * <p>这就是个壳工程,没有任何界面,只负责管理组件</p>
  *
  * @author 张华洋 2017/2/15 20:42
  * @version V1.2.0
@@ -11,5 +11,5 @@ import com.github.mzule.activityrouter.annotation.Module;
  */
 
 @Module("app")
-public class AppModule {
+public class App {
 }

+ 2 - 33
app/src/main/java/com/guiying/androidmodulepattern/MyApplication.java

@@ -2,52 +2,21 @@ package com.guiying.androidmodulepattern;
 
 import com.github.mzule.activityrouter.annotation.Modules;
 import com.guiying.common.base.BaseApplication;
-import com.guiying.common.http.HttpClient;
-import com.guiying.common.http.OnResultListener;
-import com.orhanobut.logger.Logger;
 
 /**
- * <p>类说明</p>
+ * <p>应用程序的Application只负责管理组件</p>
  *
  * @author 张华洋 2017/2/15 20:14
  * @version V1.2.0
  * @name MyApplication
  */
-@Modules({"app", "girls", "news"})
+@Modules({"app", "main", "girls", "news"})
 public class MyApplication extends BaseApplication {
 
 
     @Override
     public void onCreate() {
         super.onCreate();
-        login();
     }
 
-    /**
-     * 在这里模拟登陆,然后拿到sessionId或者Token
-     * 这样就能够在组件请求接口了
-     */
-    private void login() {
-        HttpClient client = new HttpClient.Builder()
-                .baseUrl("http://gank.io/api/data/")
-                .url("福利/10/1")
-                .build();
-        client.get(new OnResultListener<String>() {
-
-            @Override
-            public void onSuccess(String result) {
-                Logger.e(result);
-            }
-
-            @Override
-            public void onError(int code, String message) {
-                Logger.e(message);
-            }
-
-            @Override
-            public void onFailure(String message) {
-                Logger.e(message);
-            }
-        });
-    }
 }

+ 0 - 4
app/src/main/res/values/colors.xml

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<resources>
-
-</resources>

+ 0 - 2
app/src/main/res/values/strings.xml

@@ -3,6 +3,4 @@
 
     <string name="global_scheme">module</string>
 
-    <string name="app_exit_hint">再按一次退出程序哦~</string>
-
 </resources>

+ 1 - 1
build.gradle

@@ -43,7 +43,7 @@ ext {
     javaVersion = JavaVersion.VERSION_1_8
 
     // App dependencies version
-    supportLibraryVersion = "25.2.0"
+    supportLibraryVersion = "25.3.0"
     retrofitVersion = "2.1.0"
     glideVersion = "3.7.0"
     loggerVersion = "1.15"

+ 1 - 1
common/src/main/java/com/guiying/common/base/BaseApplication.java

@@ -14,7 +14,7 @@ import java.util.Stack;
 
 /**
  * 要想使用BaseApplication,必须在组件中实现自己的Application,并且继承BaseApplication;
- * 组件中实现的Application必须在AndroidManifest.xml中注册,否则无法使用;
+ * 组件中实现的Application必须在debug包中的AndroidManifest.xml中注册,否则无法使用;
  * 组件的Application需置于java/debug文件夹中,不得放于主代码;
  * 组件中获取Context的方法必须为:Utils.getContext(),不允许其他写法;
  * BaseApplication主要用来管理全局Activity;

+ 1 - 0
main/.gitignore

@@ -0,0 +1 @@
+/build

+ 50 - 0
main/build.gradle

@@ -0,0 +1,50 @@
+if (isModule.toBoolean()) {
+    apply plugin: 'com.android.application'
+} else {
+    apply plugin: 'com.android.library'
+}
+
+apply plugin: 'com.neenbedankt.android-apt'
+
+android {
+    compileSdkVersion rootProject.ext.compileSdkVersion
+    buildToolsVersion rootProject.ext.buildToolsVersion
+
+    defaultConfig {
+        minSdkVersion rootProject.ext.minSdkVersion
+        targetSdkVersion rootProject.ext.targetSdkVersion
+        versionCode rootProject.ext.versionCode
+        versionName rootProject.ext.versionName
+    }
+
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+        }
+    }
+
+    sourceSets {
+        main {
+            if (isModule.toBoolean()) {
+                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
+            } else {
+                manifest.srcFile 'src/main/release/AndroidManifest.xml'
+                //release模式下排除debug文件夹中的所有Java文件
+                java {
+                    exclude 'debug/**'
+                }
+            }
+        }
+    }
+    //设置了resourcePrefix值后,所有的资源名必须以指定的字符串做前缀,否则会报错。
+    //但是resourcePrefix这个值只能限定xml里面的资源,并不能限定图片资源,所有图片资源仍然需要手动去修改资源名。
+    //resourcePrefix "girls_"
+}
+
+dependencies {
+    compile fileTree(dir: 'libs', include: ['*.jar'])
+    compile project(':common')
+    //router
+    apt "com.github.mzule.activityrouter:compiler:$rootProject.aptCompilerVersion"
+}

+ 25 - 0
main/proguard-rules.pro

@@ -0,0 +1,25 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in D:\SDK/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile

+ 21 - 0
main/src/main/debug/AndroidManifest.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.guiying.main">
+
+    <application
+        android:name="debug.MainApplication"
+        android:allowBackup="true"
+        android:icon="@mipmap/ic_launcher"
+        android:label="@string/app_main"
+        android:supportsRtl="true"
+        android:theme="@style/AppTheme">
+        <activity android:name=".MainActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

+ 17 - 0
main/src/main/java/com/guiying/main/Main.java

@@ -0,0 +1,17 @@
+package com.guiying.main;
+
+import com.github.mzule.activityrouter.annotation.Module;
+
+/**
+ * <p>声明主要组件</p>
+ * <p>
+ * 这个组件是应用默认启动的组件
+ *
+ * @author 张华洋 2017/4/1 12:39
+ * @version V1.2.0
+ * @name Main
+ */
+
+@Module("main")
+public class Main {
+}

+ 1 - 1
app/src/main/java/com/guiying/androidmodulepattern/MainActivity.java → main/src/main/java/com/guiying/main/MainActivity.java

@@ -1,4 +1,4 @@
-package com.guiying.androidmodulepattern;
+package com.guiying.main;
 
 import android.os.Bundle;
 import android.support.design.widget.Snackbar;

+ 50 - 0
main/src/main/java/debug/MainApplication.java

@@ -0,0 +1,50 @@
+package debug;
+
+import com.guiying.common.base.BaseApplication;
+import com.guiying.common.http.HttpClient;
+import com.guiying.common.http.OnResultListener;
+import com.orhanobut.logger.Logger;
+
+/**
+ * <p>类说明</p>
+ *
+ * @author 张华洋 2017/2/15 20:09
+ * @version V1.2.0
+ * @name GirlsApplication
+ */
+public class MainApplication extends BaseApplication {
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        login();
+    }
+
+    /**
+     * 在这里模拟登陆,然后拿到sessionId或者Token
+     * 这样就能够在组件请求接口了
+     */
+    private void login() {
+        HttpClient client = new HttpClient.Builder()
+                .baseUrl("http://gank.io/api/data/")
+                .url("福利/10/1")
+                .build();
+        client.get(new OnResultListener<String>() {
+
+            @Override
+            public void onSuccess(String result) {
+                Logger.e(result);
+            }
+
+            @Override
+            public void onError(int code, String message) {
+                Logger.e(message);
+            }
+
+            @Override
+            public void onFailure(String message) {
+                Logger.e(message);
+            }
+        });
+    }
+}

+ 14 - 0
main/src/main/release/AndroidManifest.xml

@@ -0,0 +1,14 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.guiying.main">
+
+    <application android:theme="@style/AppTheme">
+        <activity android:name=".MainActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

+ 1 - 1
app/src/main/res/layout/activity_main.xml → main/src/main/res/layout/activity_main.xml

@@ -9,7 +9,7 @@
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
-    tools:context="com.guiying.androidmodulepattern.MainActivity">
+    tools:context="com.guiying.main.MainActivity">
 
     <Button
         android:id="@+id/news_button"

+ 5 - 2
app/src/main/res/values/dimens.xml → main/src/main/res/values/dimens.xml

@@ -1,5 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <!-- Default screen margins, per the Android Design guidelines. -->
+
     <dimen name="activity_horizontal_margin">16dp</dimen>
     <dimen name="activity_vertical_margin">16dp</dimen>
-</resources>
+
+
+</resources>

+ 6 - 0
main/src/main/res/values/strings.xml

@@ -0,0 +1,6 @@
+<resources>
+    <string name="app_main">main组件</string>
+
+    <string name="app_exit_hint">再按一次退出程序哦~</string>
+
+</resources>

+ 1 - 0
settings.gradle

@@ -1,4 +1,5 @@
 include ':app',
+        ':main',
         ':girls',
         ':news',
         ':common'