Android Studio 3.1添加本地Gson依赖失败,求助解决方案
Hey there, let's get that local Gson dependency working in your Android Studio 3.1 project. I’ve tackled similar setup issues before, so here’s a step-by-step fix tailored to your setup:
settings.gradle first Since you’ve extracted Gson into TestGsonExtras\subProject\gson, you need to tell Gradle to recognize this sub-project. Update your settings.gradle file to include the Gson module:
include ':app', ':subProject:gson'
This tells Gradle that there’s a valid Gradle project in that subdirectory (make sure the extracted Gson folder has its own build.gradle file—this comes with the official Gson source download).
Next, open your app module’s build.gradle (the one inside the app folder) and add the local project dependency in the dependencies block:
dependencies { // Keep your existing dependencies here (like appcompat, etc.) implementation project(':subProject:gson') }
If you still run into problems, check these quick fixes:
- Missing
build.gradlein Gson folder: For some reason, if the extracted Gson folder doesn’t have its ownbuild.gradle, create one inside thegsondirectory with this basic Java library config:apply plugin: 'java-library' repositories { jcenter() } dependencies { implementation 'com.google.code.findbugs:jsr305:3.0.2' } sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 - Sync Gradle manually: Click the "Sync Project with Gradle Files" button (the elephant icon) in the top-right corner of Android Studio and wait for the process to finish. Check the Build tab for specific error messages if sync fails.
- Alternative: Use a local JAR instead: If dealing with sub-projects feels cumbersome, you can compile the Gson source into a JAR (or use a pre-built one) and drop it into your
app/libsfolder. Then update your app’sbuild.gradledependencies:dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // Your other dependencies }
Once you’ve made these changes, sync your project again, and you should be able to use Gson and its Extra classes without issues.
内容的提问来源于stack exchange,提问作者fredimx40

