Advertisement
coldigniter

Gradle Mastery

Feb 15th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. settings.gradle (file) = located in the root project directory, tells Gradle which modules it should include when building your app. For most projects, the file is simple and only includes the following if you have just started a project: include ‘:app’
  2. However if you add more modules, another module name is added: include ':app', ':feature1', ':feature2', ':feature3'
  3.  
  4. apply plugin: 'com.android.library' = If you want the specific module to have its own android settings (compiledVersion mindSdk etc).Instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. Unlike JAR files, AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
  5.  
  6. apply plugin: 'java-library' = There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects— especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects.
  7.  
  8.  
  9.  
  10. Convert an existing app module to a library module =
  11. If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:
  12. - Open the module-level build.gradle file.
  13. - Delete the line for the applicationId. Only an Android app module can define this.
  14. - At the top of the file, you should see the following: apply plugin: 'com.android.application'
  15. - Change it to the following: apply plugin: 'com.android.library'
  16. - The entire structure of the module remains the same, but it now operates as an Android library and the build will now create an AAR file instead of an APK.
  17. - When you want to build the AAR file, select the library module in the Project window and then click Build > Build APK.
  18.  
  19. Adding it to library =
  20. - Add the compiled AAR (or JAR) file (the library must be already built):
  21. - Click File > New > New Module.
  22. - Click Import .JAR/.AAR Package then click Next.
  23. - Enter the location of the compiled AAR or JAR file then click Finish.
  24. Import the library module to your project (the library source becomes part of your project):
  25. - Click File > New > Import Module.
  26. - Enter the location of the library module directory then click Finish.
  27. - The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of
  28. - the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
  29. - In your settings.gradle, add the modules: include ':app', ':my-library-module'
  30. - Open the app module's build.gradle file add the dependency via implementation project(":my-library-module")
  31.  
  32. implementation (keyword) =
  33. the implementation configuration adds the library named my-library-module as a build dependency for the entire app module. If you instead want the library only for a specific build variant, then instead of implementation, use buildVariantNameImplementation. Considering you already specified productFlavors with buildVariantName:
  34.  
  35. build.gradle (toplevel file) = Every module has a build.gradle file but the top build.gradle file handles all the others.
  36. By default, the top-level build file uses the buildscript block to define the Gradle repositories and dependencies that are common to all modules in the project :
  37.  
  38.  
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. /**
  41. * The buildscript block is where you configure the repositories and
  42. * dependencies for Gradle itself—meaning, you should not include dependencies
  43. * for your modules here. For example, this block includes the Android plugin for
  44. * Gradle as a dependency because it provides the additional instructions Gradle
  45. * needs to build Android app modules.
  46. */
  47.  
  48. buildscript {
  49.  
  50. /**
  51. * The repositories block configures the repositories Gradle uses to
  52. * search or download the dependencies. Gradle pre-configures support for remote
  53. * repositories such as JCenter, Maven Central, and Ivy. You can also use local
  54. * repositories or define your own remote repositories. The code below defines
  55. * JCenter as the repository Gradle should use to look for its dependencies.
  56. *
  57. * New projects created using Android Studio 3.0 and higher also include
  58. * Google's Maven repository.
  59. */
  60.  
  61. repositories {
  62. google()
  63. jcenter()
  64. }
  65.  
  66. /**
  67. * The dependencies block configures the dependencies Gradle needs to use
  68. * to build your project. The following line adds Android plugin for Gradle
  69. * version 3.5.2 as a classpath dependency.
  70. */
  71.  
  72. dependencies {
  73. classpath 'com.android.tools.build:gradle:3.5.2'
  74. }
  75. }
  76.  
  77. /**
  78. * The allprojects block is where you configure the repositories and
  79. * dependencies used by all modules in your project, such as third-party plugins
  80. * or libraries. However, you should configure module-specific dependencies in
  81. * each module-level build.gradle file. For new projects, Android Studio
  82. * includes JCenter and Google's Maven repository by default, but it does not
  83. * configure any dependencies (unless you select a template that requires some).
  84. */
  85.  
  86. allprojects {
  87. repositories {
  88. google()
  89. jcenter()
  90. }
  91. }
  92.  
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  94.  
  95.  
  96. ext block - For Android projects that include multiple modules, it may be useful to define certain properties at the project level and share them across all the modules. By using ext block, you can easily define extras and called them using the following method:
  97.  
  98.  
  99.  
  100.  
  101.  
  102. ##################################################################
  103. ##################################################################
  104. //main build.gradle file
  105. ext {
  106. // The following are only a few examples of the types of properties you can define.
  107. compileSdkVersion = 28
  108. // You can also create properties to specify versions for dependencies.
  109. // Having consistent versions between modules can avoid conflicts with behavior.
  110. supportLibVersion = "28.0.0"
  111.  
  112. }
  113. ##################################################################
  114. ##################################################################
  115.  
  116.  
  117.  
  118.  
  119. ##################################################################
  120. ##################################################################
  121. //other module's build.gradle file
  122. android {
  123. // Use the following syntax to access properties you defined at the project level:
  124. // rootProject.ext.property_name
  125. compileSdkVersion rootProject.ext.compileSdkVersion
  126. ...
  127. }
  128. ...
  129. dependencies {
  130. implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
  131. ...
  132. }
  133. ##################################################################
  134. ##################################################################
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. **************************************************************************************************************************************
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. ##################################################################
  152. ##################################################################
  153. //inside buildscript
  154. ext.versions = [
  155. kotlin_version: '1.3.61',
  156. room : '2.2.3',
  157. retrofit : '2.7.1'
  158. ]
  159.  
  160. dependencies {
  161. compile "org.springframework:spring-context:${versions.kotlin_version}"
  162. }
  163. ##################################################################
  164. ##################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement