Bagaimana Cara Tips dan urutan langkah Gradle pada ANDROID STUDIO?

Gradle dan Android plugin untuk Gradle menyediakan cara yang fleksibel untuk menghimpun, membuat, dan memaketkan aplikasi atau library Android Anda. Halaman ini mengumpulkan beberapa tips dan konfigurasi penting untuk membantu Anda memaksimalkan hasil dari masing-masing build. Jika Anda ingin mempelajari cara mempercepat build, baca Mengoptimalkan Kecepatan Build Anda.
Jika Anda baru mengenal Gradle, pelajari dasar-dasarnya dengan membaca Mengonfigurasi Build Anda. Anda juga bisa memeriksa Dokumentasi referensi DSL Android plugin untuk mempelajari properti yang digunakan di halaman ini lebih lanjut.

Mengelola project dan sumber

Berikut adalah beberapa konfigurasi untuk mengelola modul project beserta sumbernya. Untuk mempelajari cara membuat serta mengelola project dan modul lebih lanjut, baca Ringkasan Project.

Mengubah konfigurasi kumpulan sumber default

Anda bisa menggunakan blok sourceSets di file build.gradle tingkat modul untuk mengubah di mana Gradle mencari untuk mengumpulkan file untuk setiap komponen set sumber.
    android {
      ...
      sourceSets {
        // Encapsulates configurations for the main source set.
        main {
          // Changes the directory for Java sources. The default directory is
          // 'src/main/java'.
          java.srcDirs = ['other/java']

          // When you list multiple directories, Gradle uses all of them to collect
          // sources. You should avoid specifying a directory which is a parent to one
          // or more other directories you specify.
          res.srcDirs = ['other/res1', 'other/res2']

          // For each source set, you can specify only one Android manifest.
          // The following points Gradle to a different manifest for this source set.
          manifest.srcFile 'other/AndroidManifest.xml'
          ...
        }

        // Create additional blocks to configure other source sets.
        androidTest {

          // If all the files for a source set are located under a single root
          // directory, you can specify that directory using the setRoot property.
          // When gathering sources for the source set, Gradle looks only in locations
          // relative to the root directory you specify. For example, after applying
          // the configuration below for the androidTest source set, Gradle looks for
          // Java sources only in the src/tests/java/ directory.
          setRoot 'src/tests'
          ...
        }
      }
    }
    ...
    

Mengonfigurasi properti lingkup project

Untuk project yang mencakup beberapa modul, sebaiknya tentukan properti di level project dan bagikan ke seluruh modul. Anda bisa melakukannya dengan menambahkan properti tambahan ke blok ext dalam file build.gradle tingkat atas.
    buildscript {...}
    allprojects {...}

    // This block encapsulates custom properties and makes them available to all
    // modules in the project.
    ext {
        // The following are only a few examples of the types of properties you can define.
        compileSdkVersion = 28
        // You can also use this to specify versions for dependencies. Having consistent
        // versions between modules can avoid behavior conflicts.
        supportLibVersion = "28.0.0"
        ...
    }
    ...
    
Untuk mengakses properti ini dari modul dalam project yang sama, gunakan sintaks berikut pada file build.gradle tingkat modul.
    android {
      // Use the following syntax to access properties you define at the project level:
      // rootProject.ext.property_name
      compileSdkVersion rootProject.ext.compileSdkVersion
      ...
    }
    ...
    dependencies {
        implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
        ...
    }
    

Mengelola library dan dependensi

Gradle menyediakan mekanisme yang kuat untuk mengelola dependensi, baik library jarak jauh maupun modul library lokal.

Menargetkan build tertentu dengan konfigurasi dependensi

Jika Anda hanya menginginkan dependensi untuk kumpulan sumber varian build tertentu atau kumpulan sumber pengujian, gunakan huruf kapital untuk nama konfigurasi dependensi dan berikan awalan dengan nama varian build atau kumpulan sumber pengujian.
    android {...}

    // Creates Gradle dependency configurations to use in the dependencies block.
    configurations {
      // For variants that combine a product flavor and build type, you need to
      // intitialize a placeholder for its dependency configuration.
      freeDebugRuntimeOnly{}
      ...
    }

    dependencies {
        // Adds an implementation dependency only to the "free" product flavor.
        freeImplementation 'com.google.firebase:firebase-ads:9.8.0'
        // Adds a runtimeOnly dependency only to the "freeDebug" build variant.
        freeDebugRuntimeOnly fileTree(dir: 'libs', include: ['*.jar'])
        // Adds a remote binary dependency only for local tests.
        testImplementation 'junit:junit:4.12'
        // Adds a remote binary dependency only for the instrumented test APK.
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

Membuat beberapa versi aplikasi

Gradle dan plugin Android memungkinkan Anda membuat beberapa versi aplikasi dari satu modul dengan mengonfigurasi varian build.

Mengonfigurasi dukungan beberapa APK

Dengan plugin Android, Anda bisa membuat beberapa APK yang masing-masing menargetkan ABI atau kepadatan layar, dan memanfaatkan dukungan multi APK dari Google Play.

Mengonfigurasi APK terpisah untuk setiap kepadatan layar

Untuk membuat APK terpisah untuk kepadatan layar yang berbeda, tambahkan blok android.splits.density ke file build.gradle modul Anda.
    android {
      ...
      splits {

        // Configures multiple APKs based on screen density.
        density {

          // Enables building multiple APKs.
          enable true

          // Specifies a list of screen densities Gradle should not create APKs for.
          exclude "ldpi", "mdpi"

          // Alternatively, you can use the following to clear the default list of
          // screen densities and specify only the screen densities you want to build
          // APKs for:
          // reset()
          // include "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"

          // Specifies a list of compatible screen size settings. This property
          // configures the <compatible-screens> element in the manifest. You
          // typically don't need to configure this manifest property, but it's
          // important when building multiple APKs based on screen density.
          compatibleScreens 'normal', 'large', 'xlarge'
        }
      }
    }
    

Mengonfigurasi APK terpisah untuk setiap ABI

Untuk membuat APK terpisah untuk setiap ABI, tambahkan blok android.splits.abi ke file build.gradlemodul Anda.
    android {
      ...
      splits {

        // Configures multiple APKs based on ABI.
        abi {

          // Enables building multiple APKs.
          enable true

          // By default all ABIs are included, so use reset() and include to specify that we only
          // want APKs for x86, armeabi-v7a, and mips.
          reset()

          // Specifies a list of ABIs that Gradle should create APKs for.
          include "x86", "armeabi-v7a", "mips"

          // Specify that we want to also generate a universal APK that includes all ABIs.
          universalApk true
        }
      }
    }
    

Mengonfigurasi kode versi dinamis

Secara default, saat Gradle menghasilkan APK untuk project Anda, setiap APK memiliki informasi versi yang sama, seperti yang ditetapkan dalam file build.gradle tingkat modul. Karena Google Play Store tidak mengizinkan multi APK untuk satu aplikasi jika semuanya memiliki informasi versi yang sama, Anda harus memastikan setiap APK memiliki versionCode yang unik sebelum menguploadnya ke Play Store.
Anda bisa melakukan ini dengan logika build khusus yang menetapkan kode versi berbeda ke setiap APK pada waktu build. Misalnya, saat membuat APK terpisah untuk setiap ABI, proses pemberian versi APK otomatis akan terlihat seperti ini:
    android {
      ...
      defaultConfig {
        ...
        versionCode 4
      }
      splits {
        ...
      }
    }

    // Map for the version code that gives each ABI a value.
    ext.abiCodes = ['armeabi-v7a':1, mips:2, x86:3]

    // For per-density APKs, create a similar map like this:
    // ext.densityCodes = ['hdpi': 1, 'xhdpi': 2, 'xxhdpi': 3, 'xxxhdpi': 4]

    import com.android.build.OutputFile

    // For each APK output variant, override versionCode with a combination of
    // ext.abiCodes * 1000 + variant.versionCode. In this example, variant.versionCode
    // is equal to defaultConfig.versionCode. If you configure product flavors that
    // define their own versionCode, variant.versionCode uses that value instead.
    android.applicationVariants.all { variant ->

      // Assigns a different version code for each output APK
      // other than the universal APK.
      variant.outputs.each { output ->

        // Stores the value of ext.abiCodes that is associated with the ABI for this variant.
        def baseAbiVersionCode =
                // Determines the ABI for this variant and returns the mapped value.
                project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))

        // Because abiCodes.get() returns null for ABIs that are not mapped by ext.abiCodes,
        // the following code does not override the version code for universal APKs.
        // However, because we want universal APKs to have the lowest version code,
        // this outcome is desirable.
        if (baseAbiVersionCode != null) {

          // Assigns the new version code to versionCodeOverride, which changes the version code
          // for only the output APK, not for the variant itself. Skipping this step simply
          // causes Gradle to use the value of variant.versionCode for the APK.
          output.versionCodeOverride =
                  baseAbiVersionCode * 1000 + variant.versionCode
        }
      }
    }
    

Menggabungkan beberapa ragam produk

Dalam beberapa situasi, Anda mungkin ingin menggabungkan konfigurasi dari beberapa ragam produk.. Untuk melakukannya, Android plugin untuk Gradle memungkinkan Anda membuat grup ragam produk, yang disebut dimensi ragam.
Contoh kode berikut ini menggunakan properti flavorDimensions untuk membuat dimensi ragam "mode" guna mengelompokkan ragam produk "full" dan "demo", serta dimensi ragam "api" guna mengelompokkan konfigurasi ragam produk berdasarkan level API. Gradle kemudian menggabungkan ragam produk dari dimensi "mode" dengan yang ada dalam dimensi "api".
    android {
      ...
      buildTypes {
        debug {...}
        release {...}
      }

      // Specifies the flavor dimensions you want to use. The order in which you
      // list each dimension determines its priority, from highest to lowest,
      // when Gradle merges variant sources and configurations. You must assign
      // each product flavor you configure to one of the flavor dimensions.
      flavorDimensions "api", "mode"

      productFlavors {
        demo {
          // Assigns this product flavor to the "mode" flavor dimension.
          dimension "mode"
          ...
        }

        full {
          dimension "mode"
          ...
        }

        // Configurations in the "api" product flavors override those in "mode"
        // flavors and the defaultConfig block. Gradle determines the priority
        // between flavor dimensions based on the order in which they appear next
        // to the flavorDimensions property above--the first dimension has a higher
        // priority than the second, and so on.
        minApi24 {
          dimension "api"
          minSdkVersion '24'
          // To ensure the target device receives the version of the app with
          // the highest compatible API level, assign version codes in increasing
          // value with API level. To learn more about assigning version codes to
          // support app updates and uploading to Google Play, read Multiple APK Support
          versionCode 30000 + android.defaultConfig.versionCode
          versionNameSuffix "-minApi24"
          ...
        }

        minApi23 {
          dimension "api"
          minSdkVersion '23'
          versionCode 20000  + android.defaultConfig.versionCode
          versionNameSuffix "-minApi23"
          ...
        }

        minApi21 {
          dimension "api"
          minSdkVersion '21'
          versionCode 10000  + android.defaultConfig.versionCode
          versionNameSuffix "-minApi21"
          ...
        }
      }
    }
    ...
    

Memfilter varian

Anda bisa memfilter varian build yang tidak diinginkan menggunakan blok variantFilter dalam file build.gradle modul. Contoh kode berikut ini memberi tahu Gradle untuk tidak membuat varian apa pun yang menggabungkan ragam produk "minApi21" dan "demo":
    android {
     ...
     buildTypes {...}

     flavorDimensions "api", "mode"
     productFlavors {
        demo {...}
        full {...}
        minApi24 {...}
        minApi23 {...}
        minApi21 {...}
      }

      variantFilter { variant ->
        def names = variant.flavors*.name
        // To check for a build type instead, use variant.buildType.name == "buildType"
        if (names.contains("minApi21") && names.contains("demo")) {
          // Gradle ignores any variants that satisfy the conditions above.
          setIgnore(true)
        }
      }
    }
    ...
    

Menguji aplikasi Anda

Untuk mempelajari cara menjalankan pengujian unit lokal dan terpadu lebih lanjut, baca Menguji Aplikasi Anda.

Mengonfigurasi opsi lint

Anda bisa mengonfigurasi opsi lint tertentu menggunakan blok lintOptions di file build.gradle tingkat modul Anda. Untuk mempelajari penggunaan lint bagi project Android Anda lebih lanjut, baca Menyempurnakan Kode Anda dengan Lint.
    android {
      ...
      lintOptions {
        // Turns off checks for the issue IDs you specify.
        disable 'TypographyFractions','TypographyQuotes'
        // Turns on checks for the issue IDs you specify. These checks are in
        // addition to the default lint checks.
        enable 'RtlHardcoded', 'RtlCompat', 'RtlEnabled'
        // To enable checks for only a subset of issue IDs and ignore all others,
        // list the issue IDs with the 'check' property instead. This property overrides
        // any issue IDs you enable or disable using the properties above.
        check 'NewApi', 'InlinedApi'
        // If set to true, turns off analysis progress reporting by lint.
        quiet true
        // if set to true (default), stops the build if errors are found.
        abortOnError false
        // if true, only report errors.
        ignoreWarnings true
      }
    }
    ...
    

Mengonfigurasi setelan manifes instrumentasi

Ketika Gradle membuat APK pengujian Anda, tindakan ini secara otomatis menghasilkan file AndroidManifest.xml dan mengkonfigurasinya dengan node <instrumentation>. Anda bisa mengubah beberapa setelan untuk node ini dengan membuat file manifes lain dalam kumpulan sumber pengujian atau mengonfigurasi file build.gradle tingkat modul seperti yang ditunjukkan dalam contoh kode berikut ini.
    android {
      ...
      // Each product flavor you configure can override properties in the
      // defaultConfig block. To learn more, go to Configure Product Flavors.
      defaultConfig {
        ...
        // Specifies the application ID for the test APK.
        testApplicationId "com.test.foo"
        // Specifies the fully-qualified class name of the test instrumentation runner.
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        // If set to 'true', enables the instrumentation class to start and stop profiling.
        // If set to false (default), profiling occurs the entire time the instrumentation
        // class is running.
        testHandleProfiling true
        // If set to 'true', indicates that the Android system should run the instrumentation
        // class as a functional test. The default value is 'false'
        testFunctionalTest true
      }
    }
    ...
    

Mengubah tipe build pengujian

Secara default, semua pengujian dijalankan terhadap tipe build debug. Anda bisa mengubahnya menjadi tipe build lain dengan menggunakan properti testBuildType dalam file build.gradle tingkat modul. Misalnya, jika Anda ingin menjalankan pengujian terhadap tipe build "staging", edit file tersebut seperti yang ditunjukkan dalam cuplikan berikut ini.
    android {
        ...
        testBuildType "staging"
    }
    

Mengonfigurasi opsi pengujian Gradle

Untuk menentukan opsi yang mengubah cara Gradle menjalankan semua pengujian Anda, konfigurasikan blok testOptions di build.gradle tingkat modul.
    android {
      ...
      // Encapsulates options for running tests.
      testOptions {
        // Changes the directory where Gradle saves test reports. By default, Gradle saves test reports
        // in the path_to_your_project/module_name/build/outputs/reports/ directory.
        // '$rootDir' sets the path relative to the root directory of the current project.
        reportDir "$rootDir/test-reports"
        // Changes the directory where Gradle saves test results. By default, Gradle saves test results
        // in the path_to_your_project/module_name/build/outputs/test-results/ directory.
        // '$rootDir' sets the path relative to the root directory of the current project.
        resultsDir "$rootDir/test-results"
      }
    }
    
Untuk menentukan opsi bagi pengujian unit lokal saja, konfigurasikan blok testOptions.unitTests.
    android {
      ...
      testOptions {
        ...
        // Encapsulates options for local unit tests.
        unitTests {
          // By default, local unit tests throw an exception any time the code you are testing tries to access
          // Android platform APIs (unless you mock Android dependencies yourself or with a testing
          // framework like Mockito). However, you can enable the following property so that the test
          // returns either null or zero when accessing platform APIs, rather than throwing an exception.
          returnDefaultValues true

          // Encapsulates options for controlling how Gradle executes local unit tests. For a list
          // of all the options you can specify, read Gradle's reference documentation.
          all {
            // Sets JVM argument(s) for the test JVM(s).
            jvmArgs '-XX:MaxPermSize=256m'

            // You can also check the task name to apply options to only the tests you specify.
            if (it.name == 'testDebugUnitTest') {
              systemProperty 'debug', 'true'
            }
          }
        }
      }
    }
    

Mengoptimalkan build Anda

Bagian ini menyediakan beberapa konfigurasi untuk membantu mempercepat build "full" dan "incremental" Anda. Untuk mempelajari lebih lanjut, baca Mengoptimalkan Kecepatan Build Anda.

Menciutkan kode Anda

Android Studio menggunakan ProGuard untuk menciutkan kode Anda. Untuk project baru, Android Studio menggunakan file setelan default (proguard-android.txt) dari tools/proguard/folder SDK Android. Untuk penciutan kode yang lebih maksimal, cobalah file proguard-android-optimize.txt yang terdapat di lokasi yang sama.
    android {
      buildTypes {
        release {
          minifyEnabled true
          proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
                                               'proguard-rules.pro'
        }
      }
      ...
    }
    ...
    
Untuk menambahkan aturan ProGuard khusus bagi setiap varian build, konfigurasikan properti proguardFilestambahan untuk setiap ragam. Misalnya, contoh berikut ini menambahkan flavor2-rules.pro ke "flavor2". Sekarang, versi rilis "flavor2" menggunakan ketiga aturan ProGuard karena aturan dari blok rilis juga diterapkan.
    android {
      ...
      buildTypes {
        release {
          minifyEnabled true
          proguardFiles getDefaultProguardFile('proguard-android.txt'),
                 'proguard-rules.pro'
        }
      }
      productFlavors {
        flavor1 {
          ...
        }
        flavor2 {
          proguardFile 'flavor2-rules.pro'
        }
      }
    }
    ...
    

Memublikasikan aplikasi Anda

Untuk mempelajari cara memublikasikan aplikasi ke Google Play lebih lanjut, baca Memublikasikan Aplikasi Anda.

Menandatangani aplikasi

Meskipun Android Studio menyediakan cara mudah untuk mengonfigurasikan penandatanganan untuk rilis build dari UI, Anda bisa secara manual mengonfigurasi blok signingConfigs di file build.gradle modul Anda:
    android {
      ...
      defaultConfig { ... }

      // Encapsulates signing configurations.
      signingConfigs {
        // Creates a signing configuration called "release".
        release {
          // Specifies the path to your keystore file.
          storeFile file("my-release-key.jks")
          // Specifies the password for your keystore.
          storePassword "password"
          // Specifies the identifying name for your key.
          keyAlias "my-alias"
          // Specifies the password for your key.
          keyPassword "password"
        }
      }
      buildTypes {
        release {
          // Adds the "release" signing configuration to the release build type.
          signingConfig signingConfigs.release
          ...
        }
      }
    }
    ...
    

Menghapus informasi penandatanganan pribadi dari project Anda

Secara default, konfigurasi penandatanganan dicatat dalam teks biasa ke file build.gradle modul. Jika Anda bekerja dengan tim atau project open source, Anda bisa memindahkan informasi sensitif keluar dari file build dengan melakukan langkah-langkah berikut ini.
  1. Buat file bernama keystore.properties dalam direktori root project dan sertakan informasi berikut:
        storePassword=myStorePassword
        keyPassword=myKeyPassword
        keyAlias=myKeyAlias
        storeFile=myStoreFileLocation
        
  2. Dalam file build.gradle, muat file keystore.properties seperti berikut ini (harus diletakkan sebelum blok android):
        // Creates a variable called keystorePropertiesFile, and initializes it to the
        // keystore.properties file.
        def keystorePropertiesFile = rootProject.file("keystore.properties")
    
        // Initializes a new Properties() object called keystoreProperties.
        def keystoreProperties = new Properties()
    
        // Loads the keystore.properties file into the keystoreProperties object.
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    
        android {
          ...
        }
        ...
        
  3. Masukkan informasi penandatanganan yang disimpan dalam objek keystoreProperties:
        android {
          signingConfigs {
            config {
              keyAlias keystoreProperties['keyAlias']
              keyPassword keystoreProperties['keyPassword']
              storeFile file(keystoreProperties['storeFile'])
              storePassword keystoreProperties['storePassword']
            }
          }
          ...
        }
        ...
        
  4. Klik Sinkronisasikan Sekarang di baris notifikasi.
Untuk mempelajari penandatanganan aplikasi lebih lanjut, baca Menandatangani Aplikasi Anda.

Menyederhanakan pengembangan aplikasi

Tips berikut ini membantu memudahkan proses pengembangan aplikasi Android Anda.

Gunakan nilai resource dan kolom khusus yang sama dengan kode aplikasi Anda

Pada waktu build, Gradle menghasilkan class BuildConfig sehingga kode aplikasi Anda bisa memeriksa informasi tentang build saat ini. Anda juga bisa menambahkan kolom khusus ke class BuildConfig dari file konfigurasi build Gradle menggunakan metode buildConfigField() dan mengakses nilai-nilai tersebut dalam kode waktu proses aplikasi. Demikian juga, Anda bisa menambahkan nilai resource aplikasi dengan resValue().
    android {
      ...
      buildTypes {
        release {
          // These values are defined only for the release build, which
          // is typically used for full builds and continuous builds.
          buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
          resValue("string", "build_time", "${minutesSinceEpoch}")
          ...
        }
        debug {
          // Use static values for incremental builds to ensure that
          // resource files and BuildConfig aren't rebuilt with each run.
          // If these rebuild dynamically, they can interfere with
          // Apply Changes as well as Gradle UP-TO-DATE checks.
          buildConfigField("String", "BUILD_TIME", "\"0\"")
          resValue("string", "build_time", "0")
        }
      }
    }
    ...
    
Dalam kode aplikasi, Anda bisa mengakses properti dengan cara berikut:
    ...
    Log.i(TAG, BuildConfig.BUILD_TIME)
    Log.i(TAG, getString(R.string.build_time))
    

Menggunakan properti yang sama dengan manifes

Dalam beberapa situasi, Anda mungkin perlu mendeklarasikan properti yang sama dalam manifes dan kode Anda (misalnya, saat mendeklarasikan otoritas untuk FileProvider). Daripada mengupdate properti yang sama di beberapa lokasi untuk merefleksikan perubahan, tetapkan saja satu properti dalam file build.gradlemodul agar tersedia untuk manifes dan kode Anda, seperti yang ditunjukkan dalam contoh berikut ini. Untuk mempelajari lebih lanjut, baca Memasukkan Variabel Build ke dalam Manifes.
    android {
      // For settings specific to a product flavor, configure these properties
      // for each flavor in the productFlavors block.
      defaultConfig {
        // Creates a property for the FileProvider authority.
        def filesAuthorityValue = applicationId + ".files"
        // Creates a placeholder property to use in the manifest.
        manifestPlaceholders =
          [filesAuthority: filesAuthorityValue]
          // Adds a new field for the authority to the BuildConfig class.
          buildConfigField("String",
                           "FILES_AUTHORITY",
                           "\"${filesAuthorityValue}\"")
      }
      ...
    }
    ...
    
Dalam manifes, akses placeholder dengan cara berikut:
    <manifest>
      ...
      <application>
        ...
        <provider
          android:name="android.support.v4.content.FileProvider"
          android:authorities="${filesAuthority}"
          android:exported="false"
          android:grantUriPermissions="true">
          ...
        </provider>
      </application>
    </manifest>
    
Mengakses kolom FILES_AUTHORITY dalam kode aplikasi Anda akan terlihat seperti ini:
    ...
    val contentUri: Uri = FileProvider.getUriForFile(context, BuildConfig.FILES_AUTHORITY, myFile)
    

Was this page helpful?

sumber: https://developer.android.com/studio/build/gradle-tips

Komentar

Postingan Populer