Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining from which repository dependency a given package arises in Android Studio

Question:

For a given import, e.g.

import androidx.appcompat.app.AppCompatActivity;

Is there a quick and efficient means to back tracing from which gradle dependency, and from which repo that dependency is pulled from in Android Studio? A similar function is represented by the Ctrl+q quickkey to fetch the documentation on whatever is currently under the cursor, but from what I understand this does not go beyond the scoep of the .java files. In other words, I'd like to find someway of mapping out the tree of dependencies from a given import statement.

Example Use Case:

For example, if I have the default MainActivity.java when creating a new blank activity project in Android Studio 4.0 I have the following default MainActivity.java code:

package jp.oist.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

with the following project-level gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and the following app-level gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "jp.oist.myapplication"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

Here, because of the simplicity of the project, I know that the

import androidx.appcompat.app.AppCompatActivity;

would arise from the app-level gradle file dependency:

implementation 'androidx.appcompat:appcompat:1.1.0'

which in turn is being pulled from the project-level gradle file defined repositories:

repositories {
    google()
    jcenter()
}

I hope I have the understanding of all that correct? Even in this case though, it is still ambiguous whether the androidx.appcompat:appcompat:1.1.0 package is being pulled from google() or jcenter(). Obviously the Android Studio Linter is confirming which one in the background, as it would throw an error if it was unable to pull from any repos.

I'm hoping to discover a method for finding these dependency tree details in a similar way to how I use the Ctrl+q to see the documentation and package details of a given method or variable anywhere else in the code. I also use the Ctrl+b to go to the declaration or usage of a given method or variable.

In the default code above, I can click on onCreate(Bundle ..., press Ctrl-q and I will find that it is defined in jp.oist.myapplication.MainActivity, and I also see that it overrides the onCreate method of class AppCompatActivity. I could open up the AppCompatActivity class and repeat the same step to find that the onCreate method here overrides the onCreate method of the class FragmentActivity...so on and so forth. This gives me a means to trace all the way up to the upermost parent (or dependency if thinking in terms of the gradle context).

Is there a similar functionality for tracing back the "parent" or dependenc(ies) of a given import statement? And then the repo of that dependency?

I would find this immensely useful when trying to learn from sample code (e.g. Google's Vision Quickstart sample project). Trying to understand how to adapt such samples to my code requires that I know where the base dependencies lie and rather than just copy and pasting EVERYTHING, I'd rather have a better understanding of what is going into my code.

What I've Tried:

Googling and searching on stackoverflow for things like, "android studio which repo import from", ""android studio" tracing import statements back to gradle", etc. but haven't found a good search string to use it seems. I'm sure this has been asked or answered SOMEWHERE, but I'm simple unable to find it. Any points in the right direction are appreciated!

System used to generate defaults:

Android Studio 4.0 Build #AI-193.6911.18.40.6514223, built on May 21, 2020 Runtime version: 1.8.0_242-release-1644-b3-6222593 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Linux 5.3.0-46-generic GC: ParNew, ConcurrentMarkSweep Memory: 1981M Cores: 8 Registry: ide.new.welcome.screen.force=true Non-Bundled Plugins: PlantUML integration, SequenceDiagram, org.jetbrains.kotlin, com.google.services.firebase, com.intellij.marketplace, com.thoughtworks.gauge, org.intellij.plugins.markdown, org.pmesmeur.sketchit, ru.leoklo.stackuml, simpleUML

like image 461
topher217 Avatar asked Dec 11 '25 23:12

topher217


1 Answers

In order to figure out the dependency parents you need to:

  1. Open a class from import statement and see the library ID and version (in the Project tab)
  2. Then click "Analyze dependencies..." in the Gradle tab:

Dependency Analyzer

like image 152
soshial Avatar answered Dec 13 '25 12:12

soshial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!