Skip to content

Commit ed45fe7

Browse files
committed
refactor:spilt the project to some modules
I am learning component-based develop(组件化开发) in Android. This means some modules is both library and module. They can run on your device.
1 parent 4776177 commit ed45fe7

File tree

11 files changed

+108
-40
lines changed

11 files changed

+108
-40
lines changed

app/build.gradle

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ apply plugin: 'kotlin-android'
44
apply plugin: 'kotlin-android-extensions'
55

66
android {
7-
compileSdkVersion 25
8-
buildToolsVersion "26.0.0"
7+
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
8+
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
99
defaultConfig {
1010
applicationId rootProject.ext.android["applicationId"]
1111
minSdkVersion rootProject.ext.android["minSdkVersion"]
@@ -56,4 +56,7 @@ dependencies {
5656
compile rootProject.ext.dependencies["rxandroid"]
5757
compile rootProject.ext.dependencies["anko"]
5858
compile rootProject.ext.dependencies["log_interceptor"]
59+
if(isLibrary){
60+
compile project(':weather')
61+
}
5962
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/MainActivity.kt

+24
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.xiasuhuei321.gank_kotlin
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import android.support.v7.app.AppCompatActivity
56
import android.view.Window
67
import android.view.WindowManager
8+
import com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
9+
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
710
import com.xiasuhuei321.gank_kotlin.extension.shortToast
11+
import com.xiasuhuei321.weather.TestActivity
812
import kotlinx.android.synthetic.main.activity_main.*
913

1014
class MainActivity : AppCompatActivity() {
15+
lateinit var weatherWv: WeatherView
1116
override fun onCreate(savedInstanceState: Bundle?) {
1217
super.onCreate(savedInstanceState)
1318
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
@@ -18,12 +23,31 @@ class MainActivity : AppCompatActivity() {
1823
weatherIv.setOnClickListener {
1924
shortToast("weather icon be clicked")
2025
}
26+
startActivity(Intent(this, TestActivity::class.java))
27+
weatherWv = WeatherView(this)
2128
}
2229

2330
override fun onResume() {
31+
// weatherWv.setZOrderOnTop(true)
32+
// weatherWv.setZOrderMediaOverlay(false)
33+
// weatherWv.canRun = true
34+
// try {
35+
// weatherWv.lock.notify()
36+
// }catch (e: Exception){}
37+
containerFl.addView(weatherWv)
38+
LogUtil.i("onResume")
2439
super.onResume()
2540
}
2641

42+
override fun onPause() {
43+
super.onPause()
44+
containerFl.removeAllViews()
45+
// weatherWv.setZOrderOnTop(false)
46+
// weatherWv.setZOrderMediaOverlay(true)
47+
// weatherWv.canRun = false
48+
// LogUtil.i("onPause")
49+
}
50+
2751
override fun onDestroy() {
2852
super.onDestroy()
2953
weatherWv.onDestroy()

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherView.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
6565

6666
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
6767
// surface发生了变化
68-
// canRun = true
68+
canRun = true
6969

7070
}
7171

@@ -91,7 +91,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
9191
init {
9292
LogUtil.i(TAG, "init开始")
9393
holder.addCallback(this)
94-
holder.setFormat(PixelFormat.RGBA_8888)
94+
holder.setFormat(PixelFormat.TRANSLUCENT)
9595
// initData()
9696
setZOrderOnTop(true)
9797
// setZOrderMediaOverlay(true)

app/src/main/res/layout/activity_main.xml

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<merge xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
43
android:layout_width="match_parent"
54
android:layout_height="match_parent"
6-
tools:context="com.xiasuhuei321.gank_kotlin.MainActivity">
5+
android:visibility="visible">
76

8-
9-
<com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
10-
android:id="@+id/weatherWv"
7+
<FrameLayout
8+
android:id="@+id/containerFl"
119
android:layout_width="match_parent"
12-
android:layout_height="match_parent" />
10+
android:layout_height="match_parent">
11+
12+
</FrameLayout>
1313

1414

1515
<RelativeLayout
@@ -51,6 +51,6 @@
5151

5252
</LinearLayout>
5353
</RelativeLayout>
54-
55-
5654
</merge>
55+
56+
<!--</merge>-->

conf.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
ext {
2+
// 这个标志位决定了部分Library是否可运行
3+
// 处于false的时候,部分组件就是一个单独
4+
// 可运行的Module
5+
isLibrary = true
26
android = [
37
compileSdkVersion: 25,
48
buildToolsVersion: "26.0.0",

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ org.gradle.jvmargs=-Xmx1536m
1515
# This option should only be used with decoupled projects. More details, visit
1616
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1717
# org.gradle.parallel=true
18-
isLibrary=false
18+
# isLibrary=false

weather/build.gradle

+20-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ if (!isLibrary.toBoolean()) {
66

77

88
android {
9-
compileSdkVersion 26
10-
buildToolsVersion "26.0.1"
9+
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
10+
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
1111

1212
defaultConfig {
13-
applicationId "com.xiasuhuei321.weather"
14-
minSdkVersion 14
15-
targetSdkVersion 26
16-
versionCode 1
17-
versionName "1.0"
13+
if (!isLibrary.toBoolean()) {
14+
applicationId "com.xiasuhuei321.weather"
15+
}
16+
minSdkVersion rootProject.ext.android["minSdkVersion"]
17+
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
18+
versionCode rootProject.ext.android["versionCode"]
19+
versionName rootProject.ext.android["versionName"]
1820

1921
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2022

@@ -25,6 +27,17 @@ android {
2527
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2628
}
2729
}
30+
31+
sourceSets {
32+
main {
33+
if (isLibrary.toBoolean()) {
34+
manifest.srcFile 'src/main/AndroidManifest.xml'
35+
} else {
36+
manifest.srcFile 'src/main/module/AndroidManifest.xml'
37+
}
38+
}
39+
}
40+
2841
}
2942

3043
dependencies {

weather/src/main/AndroidManifest.xml

+3-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.xiasuhuei321.weather">
44

5-
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
6-
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
7-
android:supportsRtl="true" android:theme="@style/AppTheme">
8-
<activity android:name=".MainActivity">
9-
<intent-filter>
10-
<action android:name="android.intent.action.MAIN" />
11-
12-
<category android:name="android.intent.category.LAUNCHER" />
13-
</intent-filter>
14-
</activity>
5+
<!-- 集成模式下的清单文件 -->
6+
<application android:theme="@style/AppTheme">
7+
<activity android:name=".TestActivity" />
158
</application>
169

1710
</manifest>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.xiasuhuei321.weather;
22

3-
import android.support.v7.app.AppCompatActivity;
43
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
6+
57

6-
public class MainActivity extends AppCompatActivity {
8+
public class TestActivity extends AppCompatActivity {
79

810
@Override
911
protected void onCreate(Bundle savedInstanceState) {
1012
super.onCreate(savedInstanceState);
1113
setContentView(R.layout.activity_main);
14+
Log.e("Weather_Main", "我启动了");
1215
}
1316
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.xiasuhuei321.weather">
3+
4+
<application
5+
android:allowBackup="true"
6+
android:icon="@mipmap/ic_launcher"
7+
android:label="weather"
8+
android:roundIcon="@mipmap/ic_launcher_round"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity
12+
android:name="com.xiasuhuei321.weather.TestActivity"
13+
android:screenOrientation="portrait">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
+14-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
5-
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
6-
android:layout_height="match_parent" tools:context="com.xiasuhuei321.weather.MainActivity">
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:gravity="center"
8+
tools:context="com.xiasuhuei321.weather.TestActivity">
79

8-
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
9-
android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
10-
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
10+
<TextView
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="This is Weather Module!"
14+
app:layout_constraintBottom_toBottomOf="parent"
15+
app:layout_constraintLeft_toLeftOf="parent"
16+
app:layout_constraintRight_toRightOf="parent"
1117
app:layout_constraintTop_toTopOf="parent" />
1218

13-
</android.support.constraint.ConstraintLayout>
19+
</LinearLayout>

0 commit comments

Comments
 (0)