复用本地已下载Android库:无需重复下载导入至新项目
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
- Windows:
- How to Use:
Just add the identical Retrofit dependency line to your new project’s module-levelbuild.gradle:
Gradle will check the local cache first and skip downloading entirely if the library (and its dependencies) already exist.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'
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-aarfolder - The Gradle cache path listed above
- Your old project’s
- Add to New Project:
- Create a
libsfolder in your new project’sappdirectory. - Copy all relevant
.jar/.aarfiles into this folder. - Update your module-level
build.gradleto 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')
- Create a
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-levelbuild.gradleand 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
publishToMavenLocalGradle 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:
AddmavenLocal()to your project-levelbuild.gradlerepositories: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

