Thursday, February 1, 2024
HomeiOS DevelopmentGradle Tutorial for Android: Getting Began – Half 2

Gradle Tutorial for Android: Getting Began – Half 2


In Half 1 of this tutorial, you discovered the way to learn and construct Gradle recordsdata and the way to handle dependencies in a number of methods. On this half, you’ll find out about barely extra complicated components of Gradle. By the top, you’ll have the ability to:

  1. Signal your releases and have totally different construct varieties.
  2. Create Gradle duties and plugins.
  3. Create construct flavors for revenue.

Getting Began

Obtain the starter challenge by clicking the Obtain Supplies hyperlink on the prime or backside of the tutorial. You’ll decide up the place you left off in Half 1.

This a part of the tutorial will concentrate on the way to use Kotlin script, because it’s now the popular manner of writing Gradle recordsdata. Nonetheless, each little bit of Kotlin script code may have its Groovy equal so you can too find out about it. If the choice Groovy model doesn’t exist, you’ll be able to assume that the particular little bit of code you’re is an identical for each instances.

Getting Able to Publish: Working with Product Flavors and Construct Sorts

Within the final article, you completed constructing your app. Now, you’re pondering of the way to revenue from it :]

Money money money

One resolution is to have a number of variations of your app: a free model and a paid model. Thankfully, Gradle helps this on the construct stage and lets you outline the boundaries of various construct varieties. However earlier than you get began, you have to perceive how Gradle lets you work with totally different app variations.

Introducing Construct Sorts

By default, there are two construct varieties – debug and launch. The one distinction between them is the worth of the debuggable parameter. In different phrases, you should use the debug model to assessment logs and to debug the app, however the launch sort is used to publish your app to the Google Play Retailer. Configure properties to the construct varieties by including the next code within the android block of your module-level construct.gradle.kts file:


buildTypes {
  launch {
  }
  debug {
  }
}

Specify the type-specific settings of your software within the debug and launch blocks.

Studying About Construct Signing

One of the essential configurations of the construct is its signature. And not using a signature, you gained’t have the ability to publish your software as a result of it’s essential to confirm you as an proprietor of the particular software. Whilst you don’t must signal the debug construct – Android Studio does it robotically — the discharge construct ought to be signed by a developer.

Notice: To proceed, you have to generate the keystore on your launch construct. Check out this tutorial to discover a step-by-step information.

When your keystore is prepared, add the code under within the android block and above the buildTypes block (the order of declaration issues) of the module-level construct.gradle.kts file:


signingConfigs {
  create("launch") {
    storeFile = file("path to your keystore file")
    storePassword = "your retailer password"
    keyAlias = "your key alias"
    keyPassword = "your key password"
  }	
}

If you happen to’re utilizing Groovy, add this code as a substitute:


signingConfigs {
  launch {
    storeFile file("path to your keystore file")
    storePassword "your retailer password"
    keyAlias "your key alias"
    keyPassword "your key password"
  }
}

Within the signingConfigs block, specify your signature data for the construct varieties. Take note of the keystore file path. Specify it with respect to the module listing. In different phrases, in case you created a keystore file within the module listing and named it “keystore.jks”, the worth it is best to specify can be equal to the identify of the file.

Replace the buildTypes block to signal your launch construct robotically:


launch {
  signingConfig = signingConfigs.getByName("launch")
}

And the Groovy model:


launch {
  signingConfig signingConfigs.launch
}

Or, in case you’re utilizing Groovy:

Then, you should definitely maintain keystorePassword.gradle.kts ignored by your model management system. Different strategies embody conserving the password in an OS-level atmosphere variable, particularly in your distant Steady Integration system, comparable to CircleCI.

  1. When you’ve revealed your app to the Google Play Retailer, subsequent submissions should use the identical keystore file and password, so maintain them secure.
  2. Make certain NOT to commit your keystore passwords to a model management system comparable to GitHub. You are able to do so by conserving the password in a separate file from construct.gradle.kts, say keystorePassword.gradle.kts in a Signing listing, after which referencing the file from the app module-level construct.gradle.kts by way of:
    
    apply(from = "../Signing/keystorePassword.gradle.kts")
    
    
    apply from: "../Signing/keystorePassword.gradle"
    

Notice: There are two essential issues associated to your keystore file:


apply(from = "../Signing/keystorePassword.gradle.kts")

apply from: "../Signing/keystorePassword.gradle"

Utilizing Construct Flavors

With a view to create a number of variations of your app, you have to use product flavors. Flavors are a solution to differentiate the properties of an app, whether or not it’s free/paid, staging/manufacturing, and so forth.

You’ll distinguish your app flavors with totally different app names. First, add the next names as strings within the strings.xml file:


<string identify="app_name_free">Socializify Free</string>
<string identify="app_name_paid">Socializify Paid</string>

And take away the present:


<string identify="app_name">Socializify</string>

Now that the unique app_name string is now not accessible, edit your AndroidManifest.xml file and exchange android:label="@string/app_name" with android:label="${appName}" contained in the software tag.

Subsequent, add the next code within the android block of your module-level construct.gradle.kts file:


// 1
flavorDimensions.add("appMode")
// 2
productFlavors {
  // 3
  create("free") {
    // 4
    dimension = "appMode"
    // 5
    applicationIdSuffix = ".free"
    // 6
    manifestPlaceholders["appName"] = "@string/app_name_free"
  }
  create("paid") {
    dimension = "appMode"
    applicationIdSuffix = ".paid"
    manifestPlaceholders["appName"] = "@string/app_name_paid"
  }
}

Right here’s what’s taking place within the code above:

  1. You must specify the flavour dimensions to correctly match the construct varieties. On this case, you want just one dimension – the app mode.
  2. Within the productFlavors, specify a listing of flavors and their settings. On this case, free and paid.
  3. Specify the identify of the primary product taste – free.
  4. It’s necessary to specify the dimension parameter worth. The free taste belongs to the appMode dimension.
  5. Because you wish to create separate apps free of charge and paid performance, you want them to have totally different app identifiers. The applicationIdSuffix parameter defines a string that’ll be appended to the applicationId, giving your app distinctive identifiers.
  6. The manifestPlaceholders lets you modify properties in your AndroidManifest.xml file at construct time. On this case, modify the appliance identify relying on its model.

The Groovy equal could be:


// 1
flavorDimensions = ["appMode"]
// 2
productFlavors {
  // 3
  free {
    // 4
    dimension "appMode"
    // 5
    applicationIdSuffix ".free"
    // 6
    manifestPlaceholders.appName = "@string/app_name_free"
  }
  paid {
    dimension "appMode"
    applicationIdSuffix ".paid"
    manifestPlaceholders.appName = "@string/app_name_paid"
  }
}

Sync your challenge with Gradle once more. After the challenge sync, run the duties command, and see in case you can spot what’s modified:

./gradlew duties

You’ll get an analogous record of duties to the one you bought if you ran this command the primary time:


...
Construct duties
-----------
...
assembleDebug - Assembles fundamental outputs for all Debug variants.
assembleFree - Assembles fundamental outputs for all Free variants.
assemblePaid - Assembles fundamental outputs for all Paid variants.
assembleRelease - Assembles fundamental outputs for all Launch variants.
...

Spot the distinction? Take a look at the duties underneath the Construct duties part, and also you’ll see some new ones there. You now have separate instructions for every construct sort and construct taste.

Run the command:

./gradlew assembleDebug

When the command completes, examine the output listing:


ls -R app/construct/outputs/apk

Right here’s what you’ll see:


free paid

app/construct/outputs/apk/free:
debug

app/construct/outputs/apk/free/debug:
app-free-debug.apk   output-metadata.json

app/construct/outputs/apk/paid:
debug

app/construct/outputs/apk/paid/debug:
app-paid-debug.apk   output-metadata.json

You need to have two builds generated – freeDebug and paidDebug.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments