Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Gradle sync failed: Project :app declares a dependency

I am working in Android Studio 2.3 and I want to make use of a library I found on github (https://github.com/henrychuangtw/Android-ChatHead) and there is no Jar file. In settings.gradle I have declared the directory in which the library resides in like this:

include ':app'
include ':Android-ChatHead'

project(':Android-ChatHead').projectDir=new File('/Users/lorand/AndroidStudioProjects/Doritest/android_chatHeads')

And I have also added the library to the build.gradle dependencies:

dependencies {

        compile fileTree(dir: 'libs', include: ['*.jar'])

        compile project(':Android-ChatHead')

        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
    }

After this, if I sync, I get this error:

Gradle sync failed: Project :app declares a dependency from configuration 'compile' to configuration 'default' which is not declared in the descriptor for project :Android-ChatHead. Consult IDE log for more details (Help | Show Log)

If I add /app to the end of the file path in settings.gradle, I get the following error:

Error:Dependency Doritest:Android-ChatHead:unspecified on project core resolves to an APK archive which is not supported as a compilation dependency. 
File: /Users/lorand/AndroidStudioProjects/Doritest/android_chatHeads/Android-ChatHead/app/build/outputs/apk/Android-ChatHead-release-unsigned.apk

I cannot figure out what I should do.

like image 280
P.Lorand Avatar asked Apr 13 '17 11:04

P.Lorand


2 Answers

You cannot add dependencies like that as far as i know I assume there is no jar for dependency you want to use, so you will have to add Android-ChatHead as module

In order to do so follow these steps:

  1. Click File > New > Import Module.
  2. In the Source directory box, type or select the directory of the module(s) that you want to import:

    • If you are importing one module, indicate its root directory.

    • If you are importing multiple modules from a project, indicate the project folder. For each module inside the folder, a box appears and indicates the Source location and Module name. Make sure the Import box is checked for each module that you want to import.

  3. Type your desired module name(s) in the Module name field(s).

  4. Click Finish.

  5. In your settings.gradle add

    include ':app', ':Android-ChatHead'

  6. In dependencies section of you app build.gradle add

    compile project(':Android-ChatHead')

  7. clean/Build the project

like image 132
Ivan Milisavljevic Avatar answered Oct 08 '22 12:10

Ivan Milisavljevic


Importing by copy and by reference are different

What P.Lorand is doing is referencing an module rather than copying so any edits he makes on the module affect all other applications referencing the module.

With the Ivan Milisavljevic way, a module gets copied to an application. Edits made on the imported module is effective only within the application;sometimes good idea, sometimes an annoyance.

Application and module have a different structure

Application has application root and an application module within it;normally named app.
Module's root is the module itself.

changing this:

project(':Android-ChatHead').projectDir=new File('/Users/lorand/AndroidStudioProjects/Doritest/android_chatHeads')

to

project(':Android-ChatHead').projectDir=new File('/Users/lorand/AndroidStudioProjects/Doritest/android_chatHeads/moduleName')

might help.

Also if you are converting an application to a module, do make sure to rename app, two modules with the same name app is a promise for disaster.

With Android-ChatHead

It contains an app folder, so it's an application and not a module.
build.gradle contains a line apply plugin: 'com.android.application', so the application bit needs to be replaced with library.
Comment out applicationId "henrychuang.tw.chatheadmsg".
Rename the module name app to something else; I've never done it manually, with android studio ctl+alt+r on app folfder in Project tab.

Then you should be fine. Try importing by copy first.

like image 27
TastyCatFood Avatar answered Oct 08 '22 11:10

TastyCatFood