provided scope包引入方式就是,项目编译时只包含对库的classpath引用,但是一并打包库到编译的项目中,一般情况是目标运行环境或容器已经包含了该库。
Android Studio自从0.4.3((Gradle 0.8)版本就引入Provided Scope库编译选项[1],但是我一直都没有试验成功,之前都是使用AS(Android Studio)打开eclipse的项目才可以。来到Android Studio beta 0.8.2版本,在AS的Project Structure Setting的module dependencies里面的scope选项里provided竟然消失了,手工修改项目build.gradle依然不行。
在Android Issues看到很多人反馈的bug请求,其中一篇比较早的Google工程师提到可以使用Android-apt第三方gradle插件来解决provided引入问题[2]。我试验了下,测试通过。
Android-apt的项目地址:https://bitbucket.org/hvisser/android-apt/overview
在项目公共build.gradle中插入
1 2 3 4 5 6 7 8 9 10 11 |
buildscript { repositories { mavenCentral() } dependencies { // replace with the current version of the Android plugin classpath 'com.android.tools.build:gradle:0.12.1' // the latest version of the android-apt plugin classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } |
然后在项目build.gradle中插入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 20 buildToolsVersion '20.0.0' defaultConfig { applicationId "org.ligboy.test.card.module1" minSdkVersion 14 targetSdkVersion 20 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } configurations { apt } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) apt 'com.android.support:support-v4:21.+' apt 'com.google.code.gson:gson:2.2.+' apt 'com.android.support:cardview-v7:+' apt 'com.android.support:recyclerview-v7:+' } |
如上面代码所示,如果想使用provided方式引入项目,格式为: apt ‘com.google.code.gson:gson:2.2.+’
参考资料:
- http://tools.android.com/recent/androidstudio043released
- https://code.google.com/p/android/issues/detail?id=65898#c7
- https://bitbucket.org/hvisser/android-apt/overview