一个简单的 Android 版本目录实现指南

图片

使用 TOML 格式 在本文中,我们将探讨版本目录以及如何实现它。

版本目录

Gradle 版本目录使您能够以可扩展的方式添加和维护依赖项和插件。因此,不必在各个构建文件中硬编码依赖项名称和版本,而是在目录中定义它们,并在所有模块中使用。

实现

要实现版本目录,我们可以在 settings.gradle 文件的 dependencyResolutionManagement 块中定义目录(查看此链接以了解更多详细信息),但我们将使用 TOML 格式来实现它...

https://docs.gradle.org/current/userguide/platforms.html

1- 创建一个版本目录文件

在根项目的 gradle 文件夹中,创建一个名为 libs.versions.toml 的文件。Gradle 默认在 libs.versions.toml 文件中查找目录,因此建议使用这个默认名称。

2- 定义您的版本、库和插件...

在 libs.versions.toml 中,我们可以定义我们的表..."

[versions]
kotlin = "1.8.10"
compose = '1.4.0'
material3 = '1.1.0-rc01'
...

[libraries]
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "material3" }
...

[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
...

基本上,[versions] 和 [libraries] 是一组键/值对(如 kotlin = "1.8.10")的集合,称为 Tables...

https://toml.io/en/v1.0.0#table

这些表将从所有构建文件的libs.table.key处访问,因此我们稍后将在构建文件中使用它们。

请注意,我们使用了版本引用(version.ref = "xxx")而不是硬编码版本。这样可以使我们的版本可重复使用 :)

注意:您可以使用分隔符,例如 -、_、.,Gradle 将它们归一化为 .,并允许您创建子部分。因此,compose-ui 将成为 compose.ui

3- 定义 bundles(可选)

优化的做法是声明依赖束,这是常用在一起的“依赖项组”。例如...

[bundles]
compose = [
    "compose-ui",
    "compose-material3"
]

4- 最后,在您的模块中使用它们 🤩

同步项目,嘿!您现在拥有了一切,可以轻松地使用类型安全的自动完成来定义依赖项… ,

plugins {
    alias libs.plugins.kotlin.android
    ...
}

dependencies {
    // compose
    implementation libs.compose.ui
    implementation libs.compose.material3

    // or you can use bundles
    implementation libs.bundles.compose
}

实际上就是这样 :) 现在您只需同步项目,解决 Gradle 抛出的数千个错误,然后就可以开始了

以下是一些有用的注意事项...

  • • 1- 您可以不使用库的完整 ID…,

compose-ui = { module = "androidx.compose.ui:ui", ...}

而是分开定义库的组和名称…,

androidx-ktx = { group = "androidx.core", name = "core-ktx", ... }
  • • 2- 通常,版本名称使用驼峰命名(例如,hiltCompose),而库、捆绑包和插件使用短横线命名(例如,androidx-ktx)。

  • • 3- 在我们的 TOML 文件中声明的版本也可以通过类型安全的访问器访问,使它们可以用于更多用例,而不仅仅是依赖版本。例如,如果您正在使用 compose… "

composeOptions {
    kotlinCompilerExtensionVersion libs.versions.compose.get()
}

版本目录(Version catalog)的优势

  • • 1- 使用类型安全的自动完成轻松添加依赖项。

  • • 2- 所有依赖项都在一个地方进行管理,并对所有模块可见。

  • • 3- 集中式版本控制。

  • • 4- 捆绑包功能令人惊叹

缺点

  • • 1- 没有依赖项更新建议。(但您可以使用此插件来帮助您保持库的最新状态)

  • • 2- 迁移项目可能会比较繁琐(使用 Copilot 或向懂行的人寻求帮助吧)。

参考链接

https://github.com/infoilyasipek/satellite/commit/f516054ae393eca2cf1c8bdd58735aa1819aabbc https://docs.gradle.org/current/userguide/platforms.html https://developer.android.com/build/migrate-to-catalogs#groovy https://proandroiddev.com/better-dependencies-management-using-buildsrc-kotlin-dsl-eda31cdb81bf

转自:一个简单的 Android 版本目录实现指南

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单指南针App的制作过程: 1. 首先,打开Android Studio并创建一个新项目。 2. 在MainActivity中创建一个CompassView类,用于绘制指南针。 3. 在CompassView类中,我们需要实现onDraw方法,用于绘制指南针。在这个方法中,我们需要使用Canvas和Path对象来绘制指南针的各个部分。 4. 在MainActivity中,我们需要获取传感器数据并更新指南针的方向。我们可以使用SensorManager和SensorEventListener来实现这一点。 5. 在onCreate方法中,我们需要注册SensorEventListener并启动传感器。 6. 在onResume方法中,我们需要重新注册SensorEventListener并恢复传感器。 7. 在onPause方法中,我们需要注销SensorEventListener并暂停传感器。 8. 最后,在AndroidManifest.xml文件中添加必要的权限。 下面是一个简单的示例代码: MainActivity.java: ``` public class MainActivity extends AppCompatActivity implements SensorEventListener { private CompassView compassView; private SensorManager sensorManager; private Sensor accelerometer; private Sensor magnetometer; private float[] gravity; private float[] geomagnetic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); compassView = findViewById(R.id.compass); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); gravity = new float[3]; geomagnetic = new float[3]; } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { System.arraycopy(event.values, 0, gravity, 0, 3); } if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { System.arraycopy(event.values, 0, geomagnetic, 0, 3); } float[] R = new float[9]; float[] I = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic); if (success) { float[] orientation = new float[3]; SensorManager.getOrientation(R, orientation); float azimuth = (float) Math.toDegrees(orientation[0]); compassView.setAzimuth(azimuth); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } ``` CompassView.java: ``` public class CompassView extends View { private static final float MAX_ROSE_ARC_DEGREE = 360f; private static final float MIN_ROSE_ARC_DEGREE = 10f; private static final float MAX_ROSE_ARC_DEGREE_HALF = MAX_ROSE_ARC_DEGREE / 2f; private static final float MIN_ROSE_ARC_DEGREE_HALF = MIN_ROSE_ARC_DEGREE / 2f; private float azimuth = 0f; public CompassView(Context context) { super(context); } public CompassView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public CompassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float w = getWidth(); float h = getHeight(); float r = Math.min(w, h) / 2f; Paint paint = new Paint(); paint.setAntiAlias(true); paint.setTextSize(30f); paint.setTextAlign(Paint.Align.CENTER); //绘制方位线 float cx = w / 2f; float cy = h / 2f; paint.setColor(Color.parseColor("#FF0000")); canvas.drawLine(cx, 0f, cx, h, paint); canvas.drawLine(0f, cy, w, cy, paint); //绘制指南针 paint.setColor(Color.parseColor("#000000")); Path rosePath = new Path(); RectF rectF = new RectF(cx - r, cy - r, cx + r, cy + r); rosePath.addArc(rectF, MAX_ROSE_ARC_DEGREE_HALF + azimuth - MIN_ROSE_ARC_DEGREE_HALF, MIN_ROSE_ARC_DEGREE); rosePath.addArc(rectF, MAX_ROSE_ARC_DEGREE_HALF + azimuth + MIN_ROSE_ARC_DEGREE_HALF, MIN_ROSE_ARC_DEGREE); rosePath.addArc(rectF, MAX_ROSE_ARC_DEGREE_HALF + azimuth + MIN_ROSE_ARC_DEGREE_HALF * 3f, MIN_ROSE_ARC_DEGREE); rosePath.addArc(rectF, MAX_ROSE_ARC_DEGREE_HALF + azimuth + MIN_ROSE_ARC_DEGREE_HALF * 5f, MIN_ROSE_ARC_DEGREE); canvas.drawPath(rosePath, paint); //绘制方位文字 paint.setColor(Color.parseColor("#000000")); canvas.drawText("N", cx, cy - r + 30f, paint); canvas.drawText("S", cx, cy + r - 10f, paint); canvas.drawText("E", cx + r - 30f, cy, paint); canvas.drawText("W", cx - r + 30f, cy, paint); } public void setAzimuth(float azimuth) { this.azimuth = azimuth; invalidate(); } } ``` 在AndroidManifest.xml文件中添加必要的权限: ``` <uses-permission android:name="android.permission.INTERNET" /> ``` 最后,在activity_main.xml中添加CompassView组件: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.compass.CompassView android:id="@+id/compass" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` 这样就完成了一个简单指南针App的制作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值