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

43,696

Solution 1

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

apply plugin: 'com.android.application'

which tells Gradle to build it as an application, generating an APK. If you change it to this:

apply plugin: 'com.android.library'

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

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.

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.

Solution 2

In module gradle file-

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

Then remove applicationId "xxx.xxx.xxxx"

Clean and Build

Solution 3

just add these lines to library gradle file and remove other sections

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'
  ,...
}
Share:
43,696
Vic Zhou
Author by

Vic Zhou

Updated on February 13, 2022

Comments

  • Vic Zhou
    Vic Zhou over 2 years

    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 :)

  • Vic Zhou
    Vic Zhou over 9 years
    Thank you sir, this worked. Is there any reading/documentation you can recommend me about these plugin things?
  • Azurespot
    Azurespot over 9 years
    I tried that and got a new error: Error:Library projects cannot set applicationId. applicationId is set to 'com.bulletnoid.android.widget.StaggeredGridViewDemo' in default config. The StaggeredGridViewDemo is a package in the library I tried to import as a module.
  • ericharlow
    ericharlow over 9 years
    I just removed the application Id line from defaultConfig{} and it worked.
  • Vlad
    Vlad almost 8 years
    all is doing in module gradle file
  • Ric17101
    Ric17101 over 6 years
    also you will need to add 'tools:replace="android:icon" on the <application ... >... to override the main icon