获取依赖项运行时列表

时间:2016-01-04 12:23:19

标签: android android-studio build.gradle build-dependencies

最近我在Android Dependencies做了一些研究工作,一个问题困在我的脑海里。问题是我们可以使用java代码获取依赖项列表吗?

例如,我的build.gradle文件中的依赖项

dependencies {
   compile 'com.google.android.gms:play-services:+'
   compile 'com.facebook.android:facebook-android-sdk:4.1.0'
}

现在如果我想在应用程序运行时想要这些依赖项的名称,我们可以得到它吗?

2 个答案:

答案 0 :(得分:4)

不是真的。它们是编译时依赖项。在您编译的APK中,:

之间没有显着差异
  • compile 'com.facebook.android:facebook-android-sdk:4.1.0'

  • compile project(':facebook'),您在其中克隆了GitHub仓库或其他东西,以便为您提供本地库项目,而不是从存储库引用工件

  • 在你的应用中写了一堆带有Facebook名称和包的课程(同上一堆资源)

如果您正在编写应用程序,并且使用不同的构建类型和/或产品风格,并且每个应用程序具有不同的依赖关系,则可以使用BuildConfig来检测正在运行的构建变体。

如果您正在编写库,并且想要在运行时确定开发人员是否添加了某种依赖关系,那么您可以做的最好的事情就是嗅探该依赖关系中是否存在某些事物以及它们是否符合您的期望。此细节将因依赖性而异(例如,查看Play服务是否存在一组硬编码检查;查看Facebook SDK是否存在将具有一组单独的硬编码检查)。这不会区分我上面的三颗子弹,但它是你能够做的最好的。

答案 1 :(得分:1)

将一些代码放到@CommonsWare关于在运行时查找jar依赖项的注释

import org.kohsuke.args4j.CmdLineParser;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

public class RuntimeDependenciesExample {

    public static void main(String[] args) {
        // just to add some dependency for our list
        CmdLineParser parser = null;
        RuntimeDependenciesExample main = new RuntimeDependenciesExample();
        main.findRuntimeDependencies();
        main.checkSystemClassPath();
    }

    private void checkSystemClassPath() {
        System.out.println("System classpath:\n" + System.getProperty("java.class.path").replace(':', '\n'));
    }

    private void findRuntimeDependencies() {
        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        do {
            System.out.println("Using classloader: " + classloader.toString());
            URL[] urls = ((URLClassLoader) classloader).getURLs();
            List<String> jars = new ArrayList<>();
            for (URL url : urls) {
                jars.add(url.getFile());
            }

            printFiltered(jars);
            classloader = classloader.getParent();
        } while (classloader != null);
    }

    private void printFiltered(List<String> jars) {
        System.out.println("\nFiltered jar list:");
        for (String jarPath : jars) {
            if (!jarPath.contains("jre") && !jarPath.contains("jdk")) {
                System.out.println(jarPath);
            }
        }
        System.out.println('\n');
    }
}

可以输出这样的东西,你可以得到jar列表:

Using classloader: sun.misc.Launcher$AppClassLoader@29453f44

Filtered jar list:
/Users/someUser/dev/test-code/build/classes/main/
/Users/someUser/dev/test-code/build/resources/main/
/Users/someUser/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.3.11/f6b34997d04c1538ce451d3955298f46fdb4dbd4/groovy-all-2.3.11.jar
/Users/someUser/.gradle/caches/modules-2/files-2.1/args4j/args4j/2.32/1ccacebdf8f2db750eb09a402969050f27695fb7/args4j-2.32.jar
/Applications/IntelliJ%20IDEA%2016%20EAP.app/Contents/lib/idea_rt.jar


Using classloader: sun.misc.Launcher$ExtClassLoader@266474c2

Filtered jar list:
/usr/local/Cellar/subversion/1.8.10_2/lib/libsvnjavahl-1.0.dylib
/System/Library/Java/Extensions/MRJToolkit.jar


System classpath:
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/charsets.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/deploy.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/cldrdata.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/dnsns.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/jaccess.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/jfxrt.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/localedata.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/nashorn.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunec.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/ext/zipfs.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/javaws.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jce.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jfr.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jfxswt.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/jsse.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/management-agent.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/plugin.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/resources.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/lib/rt.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/ant-javafx.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/dt.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/javafx-mx.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/jconsole.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/packager.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/sa-jdi.jar
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/tools.jar
/Users/someUser/dev/test-code/build/classes/main
/Users/someUser/dev/test-code/build/resources/main
/Users/someUser/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.3.11/f6b34997d04c1538ce451d3955298f46fdb4dbd4/groovy-all-2.3.11.jar
/Users/someUser/.gradle/caches/modules-2/files-2.1/args4j/args4j/2.32/1ccacebdf8f2db750eb09a402969050f27695fb7/args4j-2.32.jar
/Applications/IntelliJ IDEA 16 EAP.app/Contents/lib/idea_rt.jar