繁体   English   中英

项目中的 build.gradle 与应用程序中的 build.gradle

[英]build.gradle in the project vs. build.gradle in the app

我在 Android Studio 中使用 IntelliJ 开始了一个项目。

该项目包括两个名为build.gradle的文件。 一个在文件夹app下,一个在 main 文件夹下,这是我的项目名称,比如MyProject

为什么需要两个? 两个build.gradle有什么区别?

Android Studio项目由模块,库,清单文件和Gradle构建文件组成。

每个项目都包含一个顶级 Gradle构建文件。 该文件名为build.gradle ,可以在顶级目录中找到。

该文件通常包含所有模块的常用配置,常用功能。

例:

  //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()
     }
  }

所有模块都有一个特定的build.gradle文件 此文件包含有关模块的所有信息(因为项目可以包含更多模块),作为配置,构建tyoes,用于签署apk的信息,依赖项....

例:

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'

}

您可以在此处找到更多信息: http//developer.android.com/sdk/installing/studio-build.html

这是它的答案,当您以这种方式使用它时效果很好

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;
        }
      }

    );
  }
}



build.gradle(项目:我的应用程序)

顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

每个项目都包含一个顶级 Gradle 文件。 它通常包含所有模块的通用配置。 无论这个顶级 Gradle 文件中包含什么,它都会影响所有模块。

例子:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha3'

        //Maven plugin
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(模块:app)

特定模块的构建文件(在其中添加依赖项、签名配置、构建类型、风格等)

所有模块都有一个特定的 Gradle 文件。 无论这个 gradle 文件中包含什么,它只会影响包含在其中的模块。

例子:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.hrskrs.gesturefun"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            zipAlignEnabled true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            zipAlignEnabled true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':gesture-fun')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.jakewharton:butterknife:7.0.1'
}

只是@Salami Tobi 的一个问题,¿对象(或变量...)。 因为我在 Java 中的形成(当然,我第一次接触这种编程语言......)是在 2004 年,并且这个 VM 的版本并不优于 Java_1.2.xxxx......,但我不'不记得在我学习课程的时候说过它!!

我指的是这三行,“final Widget largeScreen;final Widget?mediumScreen;final Widget?smallScreen;”

我可以推断(虽然我的推断可能不正确......)小部件“mediumScreen”和“smallScreen”可能只能在一个假定的“:mediumScreen”和“:smallScreen”Android Studio模块中创建(并且在Java本身中,问号在 Android Studio IDE 之外没有任何意义......),但这只是我的一个假设......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM