Java 如何将 android 项目作为库导入而不是将其编译为 apk (Android studio 1.0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27536491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import android project as library and NOT compile it as apk (Android studio 1.0)
提问by Vic Zhou
I tried to import a project(projLib) as dependency for another project(projAPK).
我试图导入一个项目(projLib)作为另一个项目(projAPK)的依赖项。
projAPK gradle has this :
projAPK gradle 有这个:
dependencies {
compile project(':libs:NewsAPI')
compile project(':projLib')
}
but when i sync the gradle it gives this error:
但是当我同步gradle时它会出现这个错误:
Error:Dependency Android_2015:projLib:unspecified on project projAPK resolves to an APK archive which is not supported as a compilation dependency. File: /Users/myname/Documents/Development/Android_2015/libs/projAPK/build/outputs/apk/projLib-release-unsigned.apk
错误:依赖 Android_2015:projLib:unspecified on project projAPK 解析为不支持作为编译依赖项的 APK 存档。文件:/Users/myname/Documents/Development/Android_2015/libs/projAPK/build/outputs/apk/projLib-release-unsigned.apk
so I guess there are two solution to this:
所以我想有两种解决方案:
- somehow make gradle think that projLib is a library that shouldn't be compiled to apk
- somehow make gradle NOT compile the projLib explicitly
- 以某种方式让 gradle 认为 projLib 是一个不应编译为 apk 的库
- 以某种方式使 gradle 不显式编译 projLib
The problem is, I couldn't find how to do any of that. Would be awesome if you guys can help :)
问题是,我找不到如何做到这一点。如果你们能帮忙,那就太棒了:)
采纳答案by Scott Barta
In projLib's build.gradle file, you'll see a statement like this:
在projLib的 build.gradle 文件中,您将看到如下语句:
apply plugin: 'com.android.application'
which tells Gradle to build it as an application, generating an APK. If you change it to this:
它告诉 Gradle 将它构建为一个应用程序,生成一个 APK。如果你改成这样:
apply plugin: 'com.android.library'
it will build as a library, generating an AAR, and it should work.
它将构建为一个库,生成一个 AAR,它应该可以工作。
If you also need projLibto generate a separate APK, then you'll have to do some refactoring to pull the common code that you need out into a third library module, and have both APKs depend on it.
如果您还需要projLib来生成单独的 APK,那么您必须进行一些重构以将您需要的公共代码提取到第三个库模块中,并使两个 APK 都依赖于它。
Libraries aren't allowed to set an applicationId
, so if you see an error message to that effect, remove it from the library's build script.
库不允许设置applicationId
,因此如果您看到有关该效果的错误消息,请将其从库的构建脚本中删除。
回答by san88
In module gradle file-
在模块 gradle 文件中-
Replaceapply plugin: 'com.android.application'
with apply plugin: 'com.android.library'
替换apply plugin: 'com.android.application'
为apply plugin: 'com.android.library'
Then removeapplicationId "xxx.xxx.xxxx"
然后删除applicationId "xxx.xxx.xxxx"
Clean and Build
清洁和构建
回答by Hamidreza Sadegh
just add these lines to library gradle
file and remove other sections
只需将这些行添加到库gradle
文件并删除其他部分
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.picasso:picasso:2.4.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:gridlayout-v7:23.1.1'
,...
}