从库模块导入特定的风味

时间:2019-05-15 14:47:44

标签: java android android-gradle build.gradle

我创建了具有两种风格的库模块。每种风味都以不同的方式实现FlavourActivity.java。在我的应用中,我将导入库模块作为依赖项,但我想指定要导入的风味

我的问题是我可以在我的应用gradle文件中添加依赖项,但是我无法访问FlavourActivity.java。尽管我的应用程序已导入了库,但仍无法访问它。


结构

我的结构如下:

app/
├── src/
│   ├── Activity.java

Library/
├── lottieYes/
│   ├── FlavourActivity.java
├── lottieNo/
│   ├── FlavourActivity.java

Android插件版本:3.4.0 Gradle版本:5.1.0


资源

我的应用gradle文件如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.asousa.testing.moduleflavour"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

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

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    implementation project(path: ':mylibrary', configuration: 'lottieYes')
}

我的库gradle文件如下:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    publishNonDefault true

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    flavorDimensions "lottie"

    productFlavors {
        lottieYes {
            dimension "lottie"
        }

        lottieNo {
            dimension "lottie"
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

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

configurations {
    lottieYes
    lottieNo
}

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    lottieYesImplementation 'com.airbnb.android:lottie:2.5.6'
}


我已经阅读了以下文章:

我还检查了以下question,但由于最新的gradle版本不起作用,因此无法解决我的问题。

但是他们都没有帮助我。有人有类似的问题吗?

3 个答案:

答案 0 :(得分:0)

为了将来的记录,我通过根据功能状态声明java.srcDirsres.srcDirs找到了解决问题的方法。


我声明了gradle外部属性,用于声明该功能是否启用。

project.ext {
  hasLottie = true
}

有关此blog post

的更多信息

在我的图书馆gradle文件上:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    publishNonDefault true

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            if (hasLottieSupport.toBoolean()) {
                java.srcDirs += 'lottieYes/java'

            } else {
                java.srcDirs += 'lottieNo/java'
                res.srcDirs += 'lottieNo/res'
            }
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

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

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'

    if (hasLottieSupport.toBoolean()) {
        implementation 'com.airbnb.android:lottie:2.5.6'
    }
}

答案 1 :(得分:0)

根据我在Multi flavor app based on multi flavor library in Android Gradle中的新答案

在您的库gradle文件中,删除"The prisoners will all stand." "BAILIFF" "All present, stand and make respectful attention to honouredJudge." "BAILIFF" "Before this gracious court now appear these prisoners toanswer for the multiple and grievous savageries of their species. Howplead you, criminal?" "BAILIFF" "Criminals keep silence!" "BAILIFF" "You will answer the charges, criminals." "BAILIFF" "Criminal, you will read the charges to the court." "BAILIFF" "All present, respectfully stand. Q" "BAILIFF" "This honourable court is adjourned. Stand respectfully. Q" "BAILIFF" "Hold it right there, boy." "MCCOY" "What about my age?" "MCCOY" "Troubles me? What's so damned troubling about not having died? How old do you think I am?" "MCCOY" "Explain how you remember that so exactly." "MCCOY" "I don't see any points on your ears, boy, but you sound like aVulcan." "MCCOY" "Almost as bad." "MCCOY" "They are, they are. And damned annoying at times "MCCOY" "Well, this is a new ship, but she's got the right name. Now youremember that, you hear." "MCCOY" "You treat her like a lady, and she'll always bring you home." "MCCOY" ,关键是要在app的build.gradle中创建与库中相同的flavorDimension和productFlavor:

configurations

执行gradle同步,然后可以在Build Variants窗口中选择lottieYes ** BuildVariant以启用Lottie支持。并且lottieYes / java中的类也将可访问。

答案 2 :(得分:0)

你可以试试这个。

apply plugin: 'com.android.application'

android {
   ......
   flavorDimensions "lottie"

   productFlavors {
     lottieYes {
        dimension "lottie"
     }

     lottieNo {
        dimension "lottie"
     }
   }
}

dependencies {
   ......
   lottieNoimplementation project(path: ':mylibraryX')
   lottieYesimplementation project(path: ':mylibraryY')

}