简体   繁体   中英

Android Gradle implementation project(': project name') not working

I have a project in Android Studio 3.5 and I am trying to add a module in Gradle using implementation project(':data') and it is not working.

I imported Retrofit and Okhttp3 under my "data" module, but It keeps saying "Cannot Resolve Symbol Retrofit". Which means it cannot find the import.

This is an old project that used to work, but as soon as I open it in Android Studio 3.5, It start getting this issues.

This is my app Gradle code below:

// app Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.example.assign"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

ext {
    supportlib = '26.0.0'
    constraintlay = '1.0.2'
    annotations = '27.0.2'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Support lib
    implementation "com.android.support:appcompat-v7:$supportlib"
    implementation "com.android.support:design:$supportlib"
    implementation "com.android.support:support-v4:$supportlib"
    implementation "com.android.support:support-annotations:$annotations"
    implementation "com.android.support.constraint:constraint-layout:$constraintlay"

    implementation project(':data')
}

This is my data Gradle code below:

// data Gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    aaptOptions {
        cruncherEnabled = false
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

ext {
    retrofit = '2.4.0'
    okhttp3 = '3.11.0'
    gson = '2.8.1'
    annotations = '27.0.2'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:support-annotations:$annotations"

    // Network
    implementation "com.google.code.gson:gson:$gson"
    implementation "com.squareup.retrofit2:retrofit:$retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit"
    implementation "com.squareup.okhttp3:okhttp:$okhttp3"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3"
}

Try changing api project(':data') or api for your third-party library.ie api "com.squareup.okhttp3:okhttp:$okhttp3"... Explanation: api is same as old compile . It support transitive dependency. Whereas implementation is direct dependency.

For example if you add dependency inside your app using implementation as you do implementation project(':data')

In this case you can access only your data modules files.So it gives error.

For more details check official document

Happy Coding.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM