简体   繁体   中英

Add external jar in gradle to Android project

I am trying to add a jar to a project on Android. I did a test with the maps.jar library. Which has a class named com.google.android.maps.MapView when i do the import of that class. I get the error package com.google.android.maps does not exist import com.google.android.maps.MapView;

I try to include this jar file in several ways into my gradle.build:

dependencies {
 runtime files('/Users/user/Documents/workspace/MyAndroidApp/libs/maps.jar')  
 //runtime fileTree(dir: '/Users/user/Documents/workspace/MyAndroidApp/libs', include: '*.jar')
 //classpath fileTree(dir: '/Users/user/Documents/workspace/MyAndroidApp/libs', include: 'maps.jar')        
}

Not luck so far.

Any idea? Thanks!

Here is how i got it going:

  1. Drag your jar file into the libs folder.
  2. Right click on the jar file and select "Add Library" now the jar file is a library.
  3. open the build.gradle file (note there are two build.gradle files at least, dont use the root one use the one in your project scope).
  4. find the dependencies section (for me i was trying to the admob -GoogleAdMobAdsSdk jar file)

    dependencies { compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.3.1.jar') }

  5. last go into settings.gradle and ensure it looks something like this:

    include ':yourproject', ':yourproject:libs:GoogleAdMobAdsSdk-6.3.1'

  6. Finally, Go to Build--->Rebuild Project

必须将编译依赖项添加到compile配置(而不是runtimeclass path )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM