简体   繁体   中英

How to manage and remove unused Gradle Task in Android Studio?

A couple of month ago I worked on an Android project using Android Studio. I created two different version of the app using different products flavors:

  • MyApp: A freemium version offering some In App Purchases
  • MyAppFree: A free version showing ads

The flavors where created/configured within the apps Gradle file. After not working on this project for a couple of month I have now started to work on a new version. After updating Android Studio to the latest version (3.0.1, was 2.x before) and opening the project, I can only build and run the MyAppFree version which has the MyApp features...

It seems that somehow the Gradle tasks and configs are messed up. How can I fix this and create clean and correct tasks?

Project Gradle

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

App Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.MyApp"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 42
        versionName "2.0.1"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "freemium", "free"
    productFlavors {
        MyApp {
            dimension "freemium"
        }
        MyAppFree {
            dimension "free"
            applicationId 'com.example.MyApp.Free'
        }
    }
}

The Gradle projects window within Android Studio lists dozens of Gradle tasks which I have not configured manually. At least I do not remember to have done this...

There are task for the MyApp flavor as well as for the MyAppFree flavor. The wired thing is, that there are also MyAppMyAppFree tasks:

app
    Tasks
        build
            assembleMyApp
            assembleMyAppFree
            assembleMyAppMyAppFree    /* what is this MyAppMyAppFreeStuff? */
            compileMyAppMyAppFreeDebugAndroidTestSources
            compileMyAppMyAppFreeDebugSources
            ...
    Run Configurations
        Projekt:app [assembleMyAppFree]
                /* Why is this set to MyAppFree instead of MyApp? How to change? */
        ...

Since I have not worked on the project for several month I don't remember how exactly I have set up the two flavors and the Gradle tasks. Are the tasks created automatically or has this to be done manually?

  • How can I create a configuration that creates and runs the MyApp flavor instead of the MyAppFree flavor?
  • Do I have to keep all the Gradle tasks and configurations where I do not have any idea what they are and what they do or is it save to clean this up and remove unused configs and tasks?
  • What is the best/correct way to manage all these tasks and configs?

Gradle follows a pattern called convention over configuration . You do not need to configure each task on your own, instead, you can declaratively describe what you want and Gradle (respectively the plugins) will setup the tasks.

Another example for this pattern is the Gradle maven-publish plugin . You can simply configure the publications and target repositories of your project. The plugin ...

  • Establishes a rule to automatically create a GenerateMavenPom task for each MavenPublication added (see the section called “Publications”).
  • Establishes a rule to automatically create a PublishToMavenRepository task for the combination of each MavenPublication added (see the section called “Publications”), with each MavenArtifactRepository added (see the section called “Repositories”).
  • Establishes a rule to automatically create a PublishToMavenLocal task for each MavenPublication added (seethe section called “Publications”).

All Publish[...] tasks will be bundled under a publish lifecycle task.

It is not necessary for you to know the importance of each single task, as plugins may create intermediate and bundling tasks, that only gain their importance from the inner plugin logic.

Also, many plugins just document the importance of their configuration DSL (eg everything in the android closure) and not each task (type). To simply use Gradle and its plugins, its enough to modify the DSL and stick to the lifecycle tasks (eg build , publish , ...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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