androidgradleandroid-gradle-pluginandroid-gradle-3.0

Can't find lib via transitive use of sub module


Android Studio 3.4.2

I has main project (app) that use module mytransport like this:

app/build.gradle

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"

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

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation "org.androidannotations:androidannotations-api:$AAVersion"

    implementation project(':mytransport')

}

In mytransport/build.gradle:

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

    implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'

}

In mytransport code I success use code like this:

in MyProject\mytransport\src\main\java\com\mycompany\android\mytransport\util\MyUtil.java

snippet:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class JSONUtil {
private static GsonBuilder gsonbuilder = new GsonBuilder();

Nice.

Now I want in main app to use gson lib. So in main app I try this in

MyProject\android\MyProject\app\src\main\java\com\mycompany\android\myproject\main\MainApp.java

snippet

import android.app.Application;
import android.content.Context;
public class MainApp extends Application {

private static GsonBuilder gsonbuilder = new GsonBuilder();

but I get compile error:

Cannot resolve symbol 'GsonBuilder'

Why it can't find gson lib in main app? I use it TRANSITIVE by mytransport module


Solution

  • Use api instead of implementation.

    In mytransport/build.gradle:

    dependencies {
       //...
       api 'com.google.code.gson:gson:2.8.5'
    }
    

    Just an example.

    In library/build.gradle:

    dependencies {
        api project(':libraryA')
    }
    

    In app/build.gradle:

    dependencies {
        api project(':library')
    }
    

    In your app you will be able to access both library and libraryA.

    Using the implementation configuration:

    In library/build.gradle:

    dependencies {
        implementation project(':libraryA')
    }
    

    In app/build.gradle:

    dependencies {
        implementation project(':library')
    }
    

    In this case in your app you can't access the libraryA methods.