What is the use of apk scope in gradle-android?

2019-08-13 16:27发布

问题:

The documentation describes apk scope as package time only dependency, so it won't be used for compilation.

I cannot think of any scenario where I need something at runtime, but not needed for compilation. If I use a jar (not a library project .aar, just a library), my code will definitely have references to the library - and if the library is not found during compilation, I cannot test on a device to check for runtime availability of library.

PS: I understand 'apk' scope is the same as 'package' scope for non-android projects. It would help even if someone can explain from a non-android perspective.

回答1:

If you have a jar contains com.library.A, you can use class A by

try{
   Object o = Class.forName("com.library.A").newInstance().
   if(o!=null){
   }
}catch(Throwable e){
}

You don't need to have it in compilation.

So you can build project faster.When you generate an apk,you can use the library as normal.