如何将android项目导入为库,而不是编译为apk (android studio 1.0)

时间:2021-04-16 20:53:17

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:

但是当我同步渐变时它会产生以下错误:

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

错误:Dependency Android_2015:projLib:在project projAPK中未指定的项目将解析为APK存档,不作为编译依赖项支持。文件:/用户/名字/文件/开发/ Android_2015 / libs / projAPK /构建/输出/ 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. 不知何故,格雷尔认为projLib是一个不应该编译为apk的库
  3. somehow make gradle NOT compile the projLib explicitly
  4. 以某种方式使gradle不能显式地编译projLib

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

问题是,我找不到任何办法。如果你们能帮忙就太棒了

3 个解决方案

#1


329  

In projLib's build.gradle file, you'll see a statement like this:

projLib的构建。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 projLib to 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,因此如果您看到一个错误消息,请从库的构建脚本中删除它。

#2


23  

In module gradle file-

在模块gradle文件-

Replace apply plugin: 'com.android.application' with apply plugin: 'com.android.library'

替换应用插件:“com.android。应用程序'与应用插件:'com.android.library'

Then remove applicationId "xxx.xxx.xxxx"

然后删除applicationId“xxx.xxx.xxxx”

Clean and Build

清洁和构建

#3


1  

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'
  ,...
}

#1


329  

In projLib's build.gradle file, you'll see a statement like this:

projLib的构建。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 projLib to 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,因此如果您看到一个错误消息,请从库的构建脚本中删除它。

#2


23  

In module gradle file-

在模块gradle文件-

Replace apply plugin: 'com.android.application' with apply plugin: 'com.android.library'

替换应用插件:“com.android。应用程序'与应用插件:'com.android.library'

Then remove applicationId "xxx.xxx.xxxx"

然后删除applicationId“xxx.xxx.xxxx”

Clean and Build

清洁和构建

#3


1  

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'
  ,...
}