官术网_书友最值得收藏!

Setting up Gradle

Gradle is a build system. You can build your Android application without one, but, in that case, you have to use several SDK tools by yourself. That is not simple! This is a part where you need a Gradle and Android Gradle plugin.

Gradle takes all the source files and processes them by tools we mentioned. Then, it packs everything into one compressed file with the .apk extension. APK can be uncompressed. If you rename it by changing its extension to .zip, you can extract the content.

Each build system uses its convention. The most important convention is about placing source code and assets in a proper directory with proper structure.

Gradle is a JVM-based build system, so that practically means that you can write your own script in Java, Groovy, Kotlin, and so on. Also, it's a plugin-based system and is easy to extend. One good example of it is Google's Android plugin. You probably noticed build.gradle files in your project. They are all written in Groovy, so any Groovy code you write will be executed. We will define our Gradle scripts to automate a building process. Let's set up our building! Open settings.gradle and take a look at it:

include ":App"    

This directive tells Gradle that it will build a module named App. The App module is located in the app directory of our project.

Now open build.gradle from project root and add the following lines:

    buildscript { 
      repositories { 
        jcenter() 
        mavenCentral() 
      } 
      dependencies { 
        classpath 'com.android.tools.build:gradle:2.3.3' 
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.3' 
      } 
    } 
 
    repositories { 
      jcenter() 
      mavenCentral() 
    } 

We defined that our build script will resolve its dependencies from JCenter and Maven Central repositories. The same repositories will be used to resolve project dependencies. Main dependencies are added to target each module we will have:

  • Android Gradle plugin
  • Kotlin Gradle plugin

After you updated the main build.gradle configuration, open build.gradle located in the App module directory and add the following lines:

    apply plugin: "com.android.application" 
    apply plugin: "kotlin-android" 
    apply plugin: "kotlin-android-extensions" 
    android { 
      compileSdkVersion 26 
      buildToolsVersion "25.0.3" 
      defaultConfig { 
        applicationId "com.journaler" 
        minSdkVersion 19 
        targetSdkVersion 26 
        versionCode 1 
        versionName "1.0" 
        testInstrumentationRunner  
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-
android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}}
repositories {
jcenter()
mavenCentral()
}dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.3"
compile 'com.android.support:design:26+'
compile 'com.android.support:appcompat-v7:26+'}

The configurations we set enable Kotlin as a development language for our project and Gradle scripts as well. Then, it defines a minimal and target sdk version that an application requires. In our case, this is 19 as minimum and 26 as target. It is important to note that in the default configuration section, we set application ID and version parameters too. The dependencies section sets dependencies for Kotlin itself and some Android UI components that will be explained later.

主站蜘蛛池模板: 板桥市| 大石桥市| 河东区| 平遥县| 抚远县| 开封市| 武宁县| 乌拉特后旗| 恩平市| 中卫市| 金乡县| 祁门县| 谢通门县| 林州市| 拜城县| 浮山县| 台山市| 阳东县| 香港 | 合阳县| 定陶县| 水富县| 辰溪县| 加查县| 和硕县| 阜平县| 巴里| 中方县| 柘荣县| 三门县| 邵武市| 连平县| 海南省| 南华县| 晋州市| 澎湖县| 英超| 广安市| 庐江县| 镇原县| 囊谦县|