Gradle 2.3.0旧项目集成AdMob及依赖解析失败问题求助
Hey there, let's work through these two AdMob integration headaches together—they're super common when dealing with older project setups, so we'll get you sorted out in no time!
问题1:无法解析com.google.android.gms:play-services-ads:15.0.0依赖
If clicking "Install Repository and sync project" didn't fix it, here are the most likely fixes to try:
Verify Google Maven Repository is configured
Open your project's rootbuild.gradlefile and make sure theallprojectsblock includes the Google Maven repo. Older projects often miss this:allprojects { repositories { jcenter() // Or mavenCentral(), depending on your setup maven { url 'https://maven.google.com' } // This is critical for Google dependencies } }Update SDK Components
Open Android Studio's SDK Manager (Tools > SDK Manager), go to the "SDK Tools" tab, check "Show Package Details", then make sure:- Google Play Services is at least version 15.0.0 (or the latest compatible version)
- Google Repository is updated to the version that supports play-services-ads:15.0.0
Clear Gradle Cache
Sometimes cached files cause issues. Go to File > Invalidate Caches / Restart, select "Invalidate and Restart", then let Android Studio rebuild the project.Check for Proxy/Network Issues
If you're behind a proxy, make sure Android Studio is configured to use it correctly so it can reach the Google Maven repo. Also, double-check that offline mode is turned off (File > Settings > Build, Execution, Deployment > Gradle > uncheck "Offline work").
问题2:Gradle 2.3.0旧项目集成AdMob失败
Android Gradle Plugin (AGP) 2.3.0 is pretty outdated, and it has compatibility limits with newer AdMob dependency versions. Here's how to make it work:
Add Google Maven Repository
Same as above, add the Google repo to your rootbuild.gradle'sallprojectsblock—AGP 2.3.0 doesn't include this by default.Use a Compatible AdMob Version
play-services-ads:15.0.0 requires a newer AGP version than 2.3.0. Instead, use a version that's compatible with AGP 2.3.0, like12.0.1:dependencies { // For AGP 2.3.0, use 'compile' instead of 'implementation' compile 'com.google.android.gms:play-services-ads:12.0.1' }Update AndroidManifest.xml
Add the required permissions and AdMob app ID meta-data:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application ...> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/> <!-- Replace with your actual App ID --> </application>Sync and Rebuild
After making these changes, click "Sync Project with Gradle Files" and let the project rebuild. If you still get errors, try cleaning the project first (Build > Clean Project).
内容的提问来源于stack exchange,提问作者Harshit Agrawal

