You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Android Studio中导入使用GitHub CustomGauge仓库遇阻求助

解决CustomGauge库导入及使用问题

一、远程依赖导入(推荐方案)

针对你遇到的两个依赖报错,按以下步骤修正:

  1. 配置仓库到settings.gradle
    新版Android Studio要求仓库配置统一放在项目根目录的settings.gradle中,打开该文件,在dependencyResolutionManagement块内添加jitpack仓库(该库托管在此):
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
  1. 添加依赖到build.gradle (:app)
    打开app模块的build.gradle,在dependencies块中添加:
implementation 'com.github.pkleczko:CustomGauge:1.0.4'

点击右上角「Sync Now」等待同步完成即可。

二、手动导入模块(远程依赖失效时用)

你之前的Import Module操作有误,正确步骤如下:

  1. 下载CustomGauge源码压缩包,解压后将内部包含build.gradle的CustomGauge子文件夹,复制到你的Android项目根目录下。
  2. 打开项目根目录的settings.gradle,在include列表中添加模块声明:
include ':app', ':CustomGauge'
  1. 打开build.gradle (:app),在dependencies块添加模块依赖:
implementation project(':CustomGauge')

点击「Sync Now」完成同步。

三、使用库的必要操作

  1. 添加import语句
    在要使用CustomGauge的Java文件顶部,必须导入类:
import com.pkleczko.customgauge.CustomGauge;
  1. XML布局引用控件
    在布局文件中使用全类名声明控件:
<com.pkleczko.customgauge.CustomGauge
    android:id="@+id/customGauge"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cg_maxValue="100"
    app:cg_minValue="0"
    app:cg_value="50" />
  1. 代码中调用方法
    在Activity/Fragment中获取控件并调用API:
CustomGauge gauge = findViewById(R.id.customGauge);
gauge.setValue(75); // 设置当前数值
gauge.setMinValue(0); // 设置最小值

四、你之前操作的问题点

  • 「Failed to resolve」报错:未添加正确的仓库(该库不在mavenCentral,需要jitpack)
  • 仓库配置报错:新版Android Studio禁止在模块build.gradle中添加仓库,必须统一放在settings.gradle
  • Import Module操作错误:未将模块放入项目目录并在settings.gradle中声明
  • 设置依赖后无效果:缺少必要的import语句,导致无法识别库中的类

内容的提问来源于stack exchange,提问作者user1484397

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:55:18