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

复用本地已下载Android库:无需重复下载导入至新项目

Reusing Local Library Caches for New Projects (No Re-Downloads)

Absolutely, there are practical, reliable solutions to reuse already-downloaded libraries (like Retrofit) in new projects—perfect for dealing with unstable power or spotty internet. Let’s break down the easiest and most scalable methods:

1. Leverage Gradle's Local Cache (Simplest Approach)

Gradle automatically stores every library you’ve ever downloaded in a local cache directory. You don’t need extra setup if your new project uses the exact same dependency declaration as your old one.

  • Cache Locations:
    • Windows: C:\Users\<YourUsername>\.gradle\caches\modules-2\files-2.1
    • macOS/Linux: ~/.gradle/caches/modules-2/files-2.1
  • How to Use:
    Just add the identical Retrofit dependency line to your new project’s module-level build.gradle:
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    // Don’t forget any converters or adapters you used, like Gson
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    
    Gradle will check the local cache first and skip downloading entirely if the library (and its dependencies) already exist.

2. Manually Copy JAR/AAR Files to the New Project

If you want full control over which files you reuse, extract the library files from your old project or Gradle cache and add them directly:

  • Find the Files:
    Locate Retrofit’s .jar (or .aar) files and its dependencies (like OkHttp) in either:
    • Your old project’s app/build/intermediates/exploded-aar folder
    • The Gradle cache path listed above
  • Add to New Project:
    1. Create a libs folder in your new project’s app directory.
    2. Copy all relevant .jar/.aar files into this folder.
    3. Update your module-level build.gradle to reference these local files:
      implementation files('libs/retrofit-2.9.0.jar')
      implementation files('libs/okhttp-4.9.3.jar')
      implementation files('libs/converter-gson-2.9.0.jar')
      
    Important: Retrofit relies on OkHttp, so you must include that file too—otherwise your project will throw missing dependency errors.

3. Use a Local Maven Repository (Scalable for Multiple Libraries)

For a cleaner, more reusable setup (great if you reuse multiple libraries often), set up a local Maven repository:

  • Option 1: Publish from Your Old Project:
    Add the Maven Publish plugin to your old project’s module-level build.gradle and configure it to push the library to your local Maven repo (~/.m2/repository):

    plugins {
        id 'com.android.application'
        id 'maven-publish'
    }
    
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId 'com.squareup.retrofit2'
                artifactId 'retrofit'
                version '2.9.0'
            }
        }
    }
    

    Run the publishToMavenLocal Gradle task to push the library to your local repo.

  • Option 2: Manually Populate the Local Repo:
    Copy the library folders from your Gradle cache (~/.gradle/caches/modules-2/files-2.1/com.squareup.retrofit2) into your local Maven repo (~/.m2/repository/com/squareup/retrofit2), matching Maven’s directory structure.

  • Use in the New Project:
    Add mavenLocal() to your project-level build.gradle repositories:

    repositories {
        google()
        mavenCentral()
        mavenLocal() // Add this line
    }
    

    Then use the standard dependency declaration—Gradle will pull directly from your local repo instead of the internet.

Quick Tips

  • Always use the exact same library version as your old project to avoid compatibility issues and unexpected download attempts.
  • Transitive dependencies (like OkHttp for Retrofit) will already be in your Gradle cache if you used the library before—Gradle will pick them up automatically with the first method.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:52:49