Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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’
- However if you add more modules, another module name is added: include ':app', ':feature1', ':feature2', ':feature3'
- 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.
- 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.
- Convert an existing app module to a library module =
- 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:
- - Open the module-level build.gradle file.
- - Delete the line for the applicationId. Only an Android app module can define this.
- - At the top of the file, you should see the following: apply plugin: 'com.android.application'
- - Change it to the following: apply plugin: 'com.android.library'
- - 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.
- - When you want to build the AAR file, select the library module in the Project window and then click Build > Build APK.
- Adding it to library =
- - Add the compiled AAR (or JAR) file (the library must be already built):
- - Click File > New > New Module.
- - Click Import .JAR/.AAR Package then click Next.
- - Enter the location of the compiled AAR or JAR file then click Finish.
- Import the library module to your project (the library source becomes part of your project):
- - Click File > New > Import Module.
- - Enter the location of the library module directory then click Finish.
- - 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
- - the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
- - In your settings.gradle, add the modules: include ':app', ':my-library-module'
- - Open the app module's build.gradle file add the dependency via implementation project(":my-library-module")
- implementation (keyword) =
- 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:
- build.gradle (toplevel file) = Every module has a build.gradle file but the top build.gradle file handles all the others.
- 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 :
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * The buildscript block is where you configure the repositories and
- * dependencies for Gradle itself—meaning, you should not include dependencies
- * for your modules here. For example, this block includes the Android plugin for
- * Gradle as a dependency because it provides the additional instructions Gradle
- * needs to build Android app modules.
- */
- buildscript {
- /**
- * The repositories block configures the repositories Gradle uses to
- * search or download the dependencies. Gradle pre-configures support for remote
- * repositories such as JCenter, Maven Central, and Ivy. You can also use local
- * repositories or define your own remote repositories. The code below defines
- * JCenter as the repository Gradle should use to look for its dependencies.
- *
- * New projects created using Android Studio 3.0 and higher also include
- * Google's Maven repository.
- */
- repositories {
- google()
- jcenter()
- }
- /**
- * The dependencies block configures the dependencies Gradle needs to use
- * to build your project. The following line adds Android plugin for Gradle
- * version 3.5.2 as a classpath dependency.
- */
- dependencies {
- classpath 'com.android.tools.build:gradle:3.5.2'
- }
- }
- /**
- * The allprojects block is where you configure the repositories and
- * dependencies used by all modules in your project, such as third-party plugins
- * or libraries. However, you should configure module-specific dependencies in
- * each module-level build.gradle file. For new projects, Android Studio
- * includes JCenter and Google's Maven repository by default, but it does not
- * configure any dependencies (unless you select a template that requires some).
- */
- allprojects {
- repositories {
- google()
- jcenter()
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 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:
- ##################################################################
- ##################################################################
- //main build.gradle file
- ext {
- // The following are only a few examples of the types of properties you can define.
- compileSdkVersion = 28
- // You can also create properties to specify versions for dependencies.
- // Having consistent versions between modules can avoid conflicts with behavior.
- supportLibVersion = "28.0.0"
- }
- ##################################################################
- ##################################################################
- ##################################################################
- ##################################################################
- //other module's build.gradle file
- android {
- // Use the following syntax to access properties you defined at the project level:
- // rootProject.ext.property_name
- compileSdkVersion rootProject.ext.compileSdkVersion
- ...
- }
- ...
- dependencies {
- implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
- ...
- }
- ##################################################################
- ##################################################################
- **************************************************************************************************************************************
- ##################################################################
- ##################################################################
- //inside buildscript
- ext.versions = [
- kotlin_version: '1.3.61',
- room : '2.2.3',
- retrofit : '2.7.1'
- ]
- dependencies {
- compile "org.springframework:spring-context:${versions.kotlin_version}"
- }
- ##################################################################
- ##################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement