Skip to content

Commit e2dde32

Browse files
committed
[1.7.0-feature] 已有项目、AGP7.0以下集成Compose能力
1 parent 3f2202b commit e2dde32

File tree

10 files changed

+191
-4
lines changed

10 files changed

+191
-4
lines changed

app/build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ android {
4242
kotlinOptions {
4343
jvmTarget = "1.8"
4444
}
45+
buildFeatures {
46+
//开启 Compose 功能
47+
compose = true
48+
}
49+
50+
composeOptions {
51+
//配置Kotlin编译器扩展版本, 需要与 Kotlin 版本控制相关联
52+
//对应关系:https://developer.android.com/jetpack/androidx/releases/compose-kotlin?hl=zh-cn
53+
kotlinCompilerExtensionVersion compose_version
54+
kotlinCompilerVersion kotlin_version //在AGP7.0+版本上不用配置这个了
55+
}
4556

4657
buildTypes {
4758
release {
@@ -86,6 +97,11 @@ android {
8697
flavor -> flavor.manifestPlaceholders = [KEY_VALUE: "${flavor.name}"]
8798
}
8899
}
100+
packagingOptions {
101+
resources {
102+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
103+
}
104+
}
89105
//修改APK生成名字 通过android.applicationVariants索引来遍历所有的 build variant
90106
applicationVariants.each { variant ->
91107
variant.outputs.each {
@@ -124,6 +140,26 @@ dependencies {
124140
implementation Deps.jetpack_datastore //datastore
125141
implementation Deps.jetpack_datastore_pf //datastore_preferences
126142

143+
/**
144+
* Compose 相关配置
145+
*/
146+
implementation 'androidx.activity:activity-compose:1.3.1'
147+
implementation "androidx.compose.runtime:runtime:$compose_version"
148+
implementation "androidx.compose.ui:ui:$compose_version"
149+
implementation "androidx.compose.foundation:foundation:$compose_version"
150+
implementation "androidx.compose.foundation:foundation-layout:$compose_version"
151+
implementation "androidx.compose.material:material:$compose_version"
152+
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
153+
implementation "androidx.compose.ui:ui-tooling:$compose_version"
154+
implementation "com.google.android.material:compose-theme-adapter:$compose_version"
155+
156+
// Import the Compose BOM
157+
// implementation platform('androidx.compose:compose-bom:2022.10.00')
158+
// // Import other Compose libraries without version numbers
159+
// // ..
160+
// implementation 'androidx.compose.ui:ui'
161+
// implementation 'androidx.compose.foundation:foundation'
162+
127163
/**
128164
* Room相关
129165
*/

app/src/main/AndroidManifest.xml

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
android:theme="@style/AppTheme"
1818
android:usesCleartextTraffic="true"
1919
tool:ignore="LockedOrientationActivity">
20-
21-
<!--存数据 可以分别在application activity service等里面取出来,注意取的方式是不一样的-->
20+
<activity
21+
android:name=".ComposeStudyActivity"
22+
android:exported="false"
23+
android:label="@string/title_activity_compose_study"
24+
android:theme="@style/AppComposeTheme" />
25+
<!-- 存数据 可以分别在application activity service等里面取出来,注意取的方式是不一样的 -->
2226
<meta-data
2327
android:name="com.nine.tripods.key"
2428
android:value="${KEY_VALUE}" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.ninetripods.mq.study
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.material.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.graphics.Color
10+
11+
class ComposeStudyActivity : ComponentActivity() {
12+
override fun onCreate(savedInstanceState: Bundle?) {
13+
super.onCreate(savedInstanceState)
14+
setContent {
15+
Greeting("Jetpack Compose")
16+
}
17+
}
18+
}
19+
20+
@Composable
21+
fun Greeting(name: String, modifier: Modifier = Modifier) {
22+
Text(
23+
text = "Hello $name!",
24+
color = Color.Red,
25+
modifier = modifier
26+
)
27+
}
28+
29+
//@Preview(showBackground = true)
30+
//@Composable
31+
//fun GreetingPreview() {
32+
// AndroidStudyTheme {
33+
// Greeting("Android")
34+
// }
35+
//}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.ninetripods.mq.study.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
5+
val Purple80 = Color(0xFFD0BCFF)
6+
val PurpleGrey80 = Color(0xFFCCC2DC)
7+
val Pink80 = Color(0xFFEFB8C8)
8+
9+
val Purple40 = Color(0xFF6650a4)
10+
val PurpleGrey40 = Color(0xFF625b71)
11+
val Pink40 = Color(0xFF7D5260)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.ninetripods.mq.study.ui.theme
2+
3+
import android.app.Activity
4+
import androidx.compose.foundation.isSystemInDarkTheme
5+
import androidx.compose.material.MaterialTheme
6+
import androidx.compose.material.darkColors
7+
import androidx.compose.material.lightColors
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.runtime.SideEffect
10+
import androidx.compose.ui.graphics.toArgb
11+
import androidx.compose.ui.platform.LocalView
12+
import androidx.core.view.WindowCompat
13+
14+
private val DarkColorScheme = darkColors(
15+
primary = Purple80,
16+
secondary = PurpleGrey80,
17+
// tertiary = Pink80
18+
)
19+
20+
private val LightColorScheme = lightColors(
21+
primary = Purple40,
22+
secondary = PurpleGrey40,
23+
// tertiary = Pink40
24+
25+
/* Other default colors to override
26+
background = Color(0xFFFFFBFE),
27+
surface = Color(0xFFFFFBFE),
28+
onPrimary = Color.White,
29+
onSecondary = Color.White,
30+
onTertiary = Color.White,
31+
onBackground = Color(0xFF1C1B1F),
32+
onSurface = Color(0xFF1C1B1F),
33+
*/
34+
)
35+
36+
@Composable
37+
fun AndroidStudyTheme(
38+
darkTheme: Boolean = isSystemInDarkTheme(),
39+
// Dynamic color is available on Android 12+
40+
dynamicColor: Boolean = true,
41+
content: @Composable () -> Unit,
42+
) {
43+
val colorScheme = when {
44+
// dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
45+
// val context = LocalContext.current
46+
// if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
47+
// }
48+
49+
darkTheme -> DarkColorScheme
50+
else -> LightColorScheme
51+
}
52+
val view = LocalView.current
53+
if (!view.isInEditMode) {
54+
SideEffect {
55+
val window = (view.context as Activity).window
56+
window.statusBarColor = colorScheme.primary.toArgb()
57+
WindowCompat.getInsetsController(window, view)?.isAppearanceLightStatusBars = darkTheme
58+
}
59+
}
60+
61+
MaterialTheme(
62+
// colorScheme = colorScheme,
63+
typography = Typography,
64+
content = content
65+
)
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.ninetripods.mq.study.ui.theme
2+
3+
import androidx.compose.material.Typography
4+
5+
// Set of Material typography styles to start with
6+
val Typography = Typography(
7+
// bodyLarge = TextStyle(
8+
// fontFamily = FontFamily.Default,
9+
// fontWeight = FontWeight.Normal,
10+
// fontSize = 16.sp,
11+
// lineHeight = 24.sp,
12+
// letterSpacing = 0.5.sp
13+
// )
14+
/* Other default text styles to override
15+
titleLarge = TextStyle(
16+
fontFamily = FontFamily.Default,
17+
fontWeight = FontWeight.Normal,
18+
fontSize = 22.sp,
19+
lineHeight = 28.sp,
20+
letterSpacing = 0.sp
21+
),
22+
labelSmall = TextStyle(
23+
fontFamily = FontFamily.Default,
24+
fontWeight = FontWeight.Medium,
25+
fontSize = 11.sp,
26+
lineHeight = 16.sp,
27+
letterSpacing = 0.5.sp
28+
)
29+
*/
30+
)

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,6 @@
9999
"when a user tries to pick up one of cards.\n\n"
100100
</string>
101101
<string name="action_settings">Settings</string>
102+
<string name="title_activity_compose_study">ComposeStudyActivity</string>
102103

103104
</resources>

app/src/main/res/values/styles.xml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<resources>
22

3+
<style name="AppComposeTheme" parent="android:Theme.Material.Light.NoActionBar" />
4+
35
<style name="textAppearanceSpan" parent="@android:style/TextAppearance">
46
<item name="android:textColor">@color/result_points</item>
57
<item name="android:textSize">20sp</item>

buildSrc/src/main/java/Deps.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal class LibsVersion {
8585
const val activity_ktx_version = "1.3.1"
8686
const val fragment_ktx_version = "1.3.6"
8787
const val datastore_version = "1.0.0"
88-
const val kotlin_version = "1.5.0"
88+
const val kotlin_version = "1.6.10"
8989
const val coroutines_version = "1.5.2"
9090
const val arouter_api_version = "1.5.1"
9191
const val arouter_compiler_version = "1.5.1"

gradle.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ android.enableJetifier=true
1919
compileDialogWithSource=1
2020
compileMVPager2WithSource=1
2121

22-
agp_version=4.1.3
22+
agp_version=4.2.2
23+
compose_version=1.1.1
24+
kotlin_version = 1.6.10
2325
room_version=2.4.0
2426

2527
# Blog: https://guolin.blog.csdn.net/article/details/119706565

0 commit comments

Comments
 (0)