How to import android project as library and NOT compile it as apk (Android studio 1.0)

I tried to import a project(projLib) as dependency for another project(projAPK).

projAPK gradle has this :

dependencies {
compile project(':libs:NewsAPI')
compile project(':projLib')
}

but when i sync the gradle it gives this error:

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

so I guess there are two solution to this:

  1. somehow make gradle think that projLib is a library that shouldn't be compiled to apk
  2. somehow make gradle NOT compile the projLib explicitly

The problem is, I couldn't find how to do any of that. Would be awesome if you guys can help :)

43812 次浏览

projLib的 build.gradle 文件中,您将看到如下语句:

apply plugin: 'com.android.application'

它告诉 Gradle 把它构建成一个应用程序,生成一个 APK。如果你把它改成这样:

apply plugin: 'com.android.library'

it will build as a library, generating an AAR, and it should work.

如果您还需要 ProjLib来生成一个单独的 APK,那么您将不得不进行一些重构来将需要的公共代码提取到第三个库模块中,并使两个 APK 都依赖于它。

库不允许设置 applicationId,因此如果您看到这样的错误消息,请将其从库的构建脚本中删除。

在模块分级文件 -

apply plugin: 'com.android.library'代替 apply plugin: 'com.android.application'

然后是 拿开 applicationId "xxx.xxx.xxxx"

清理和构建

只需将这些行添加到库 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'
,...
}

对于 Kotlin DSL(build.gradle.kts) ,使用以下表示法:

plugins {
id("com.android.library")
// ...
}