8

I started a project in Android Studio, with IntelliJ.

The project includes two files called build.gradle. One is under the folder app, and one is under the main folder which is my project name, say MyProject.

Why the need for two? What is the difference between the two build.gradles?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • Similar question to http://stackoverflow.com/questions/28295933/difference-between-build-gradleproject-and-build-gradlemodule – Suragch Aug 30 '16 at 15:04

2 Answers2

9

Android Studio project consists of modules, libraries, manifest files and Gradle build files.

Each project contains one top-level Gradle build file. This file is named build.gradle and can be found in the top level directory.

This file usually contains common config for all modules, common functions..

Example:

  //gradle-plugin for android
  buildscript {
    repositories {
        mavenCentral()  //or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'        
    }
  }

  // common variables
  ext {
     compileSdkVersion = 19
     buildToolsVersion = "20.0.0"
  }

  // a custom function
  def isReleaseBuild() {
     return version.contains("SNAPSHOT") == false
  }

  //common config for all projects
  allprojects {
     version = VERSION_NAME

     repositories {
       mavenCentral()
     }
  }

All modules have a specific build.gradle file. This file contains all info about this module (because a project can contain more modules), as config,build tyoes, info for signing your apk, dependencies....

Example:

apply plugin: 'com.android.application'


android {
    //These lines use the constants declared in top file
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionName project.VERSION_NAME  //it uses a property declared in gradle.properties
        versionCode Integer.parseInt(project.VERSION_CODE) 
    }

    // Info about signing
    signingConfigs {
        release
    }

    // Info about your build types
    buildTypes {
        if (isReleaseBuild()) {
            release {
                signingConfig signingConfigs.release
            }
        }

        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-debug"
        }
    }

    // lint configuration
    lintOptions {
        abortOnError false
    }
}

//Declare your dependencies  
dependencies {
    //Local library
    compile project(':Mylibrary')
    // Support Libraries
    compile 'com.android.support:support-v4:20.0.0'
    // Picasso
    compile 'com.squareup.picasso:picasso:2.3.4'

}

You can find more info here: http://developer.android.com/sdk/installing/studio-build.html

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Helpful answer, thanks. I've never made a project with multiple modules. In the case that there is only one module, does it matter which build.gradle file I use for, say, a dependency? – Suragch Apr 09 '15 at 02:42
  • @Suragch dependencies are always in the module – Gabriele Mariotti Apr 09 '15 at 05:11
  • What about the `dependencies { classpath com.android.tools.build:gradle:1.1.0' }` section in the top-level build.gradle in your first example? – Suragch Apr 09 '15 at 05:41
  • Perhaps add to your answer what format the Gradle build file is in? A [domain-specific language](https://en.wikipedia.org/wiki/Domain-specific_language)? – Peter Mortensen Jul 27 '21 at 17:56
0

This is the answer to it and it works well when you use it in such a way

import 'package:webapp/layout.dart';

const int largeScreenSize = 1366;
const int mediumScreenSize = 768;
const int smallScreenSize = 360;
const int customScreenSize = 1100;

class ResponsiveWidget extends StatelessWidget {
  final Widget largeScreen;
  final Widget? mediumScreen;
  final Widget? smallScreen;

  const ResponsiveWidget({
    Key? key,
    required this.largeScreen,
    this.mediumScreen,
    this.smallScreen,}) : super(key: key);

  static bool isSmallScreen(BuildContext context) =>
      MediaQuery.of(context).size.width < smallScreenSize;

  static bool isMediumScreen(BuildContext context) =>
      MediaQuery.of(context).size.width <= mediumScreenSize &&
      MediaQuery.of(context).size.width < largeScreenSize;

  static bool isLargeScreen(BuildContext context) =>
      MediaQuery.of(context).size.width <= largeScreenSize;

  static bool isCustomScreen(BuildContext context) =>
      MediaQuery.of(context).size.width >= mediumScreenSize &&
      MediaQuery.of(context).size.width <= customScreenSize;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints){
        double _width = constraints.maxWidth;
        if(_width >= largeScreenSize){
          return largeScreen;
        }
        else if(_width < largeScreenSize && _width >= mediumScreenSize){
          return mediumScreen ?? largeScreen;
        }
        else {
          return smallScreen ?? largeScreen;
        }
      }

    );
  }
}