Android Huawei maps agconnect-services.json can't be located - android

I have the issue with Huawei maps after introducing product flavours and issue is referenced to the fact that agconnect-services.json can't be located
E/HMSSDK_AGCUtils: Get client/cp_id failed: java.io.FileNotFoundException: agconnect-services.json
So everything was working fine until product flavours are introduced.
The configuration of product flavours are following:
flavorDimensions "provider"
productFlavors {
huawei {
dimension "provider"
}
google {
dimension "provider"
}
}
sourceSets {
huawei {
java {
srcDirs 'src/huawei/java'
}
res {
srcDirs 'src/huawei/res'
}
}
google {
java {
srcDirs 'src/google/java'
}
res {
srcDirs 'src/google/res'
}
}
}
if (getGradle().getStartParameter().getTaskRequests().toString().contains("Huawei"))
{
apply plugin: 'com.huawei.agconnect'
}
classpath 'com.huawei.agconnect:agcp:1.6.3.300'
So the package name keeps the same nothing is changing for flavour. I tried positioning .json file to app/src/huawei but nothing helped.
This is my current setup.
I tried positioning .json file to app/src/huawei, app/src/debugHuawei, app/src/debugHuawei, app/, app/src/huaweiDebug, app/src/debug/huawei but didn't help.
Thanks in advance

You could try configuring the directory according to the current options of Build Variant. like following:

you have to put this agconnect-services file outside the src folder:
just app/agconnect-services.json
also in your screenshot there are 2 agconnect-services.json files, delete them and put only 1 file in the path I indicated above

Related

Build type specific folder not recognized for a product flavor

I have configured my project to have multiple product flavors. I have the following code in my module build.gradle:
android {
// Rest of the configuration omitted for clarity
buildTypes {
debug {
// ...
}
release {
// ...
}
}
productFlavors {
paid
free
}
}
If I create a file (any file: Java, resources, ...), in the paid, free, debug or release, it is recognized by Android Studio and I can use it in my project.
However, if the same file is created in paidDebug (or a similar folder) it is not recognized in Android Studio. Do I need any extra configuration for this to work? Is this not supported (yet)?
Source code files with the same name should be placed in all of the alternative source sets (but not in the 'main').
Either:
app/src/free/java/com/domain/myapp/MainActivity.java
app/src/paid/java/com/domain/myapp/MainActivity.java
Or:
app/src/freeDebug/java/com/domain/myapp/MainActivity.java
app/src/freeRelease/java/com/domain/myapp/MainActivity.java
app/src/paidDebug/java/com/domain/myapp/MainActivity.java
app/src/paidRelease/java/com/domain/myapp/MainActivity.java
Resource files with the same name can be placed in any source set, including 'main'.
app/src/main/res/layout/activity_main.xml
app/src/paidDebug/res/layout/activity_main.xml
app/src/paidRelease/res/layout/activity_main.xml
In this case, when building the 'free' flavor, the layout file from 'main' set will be used. But, during the build of the 'paid' flavor, the specific version of the layout will be used.
You can specify specific source directories in your build.gradle file. For example, to add testFreeRelease to unit test sources and testFree to android integration test sources:
android {
[...]
sourceSets {
main.java.srcDirs += 'src/main/java'
testFreeRelease.java.srcDirs += 'src/testFreeRelease/java'
androidTestFree.java.srcDirs += 'src/androidTestFree/java'
}
}
This also works for Kotlin. Just switch java with kotlin.

Getting an app_name from strings.xml for current product flavor in gradle

In my app, I have a lot of different product flavors, around 10 actually.
The way they are done is something like : eng_testDebug, eng_prodDebug, eng_testRelease, eng_prodRelease e.t.c for other languages.
So now I'v got a task to add "- Test" to the app name for build with test in the build name.
Right now I found a solution to parse string.xml from main sourceSet and build my app name using manifestPlaceHolders in a gradle file. But it works only for main flavor.
Here is a code for getting default strings.xml app_name I am using:
def getAppName() {
def stringsFile = android.sourceSets.main.res.sourceFiles.find { it.name.equals 'strings.xml' }
String s = new XmlParser().parse(stringsFile).string.find { it.#name.equals 'app_name' }.text();
return s.replaceAll("\"", "");
}
And here I modify it to add "Test":
def getEditedAppName() {
if (getCurrentFlavor().contains("test")) {
return getAppName() + "(Test)";
} else {
return getAppName();
}
}
getCurrentFlavor() returns me a flavor name used in :assemble task.
Basically, the question is how can I get other sourceSet files depending on my current building flavor so I can parse flavor's app_name?
Unfortunately defying an app_name string for each flavor in a gradle file is not an valid option for me.
For each flavor you can create a folder named like the flavor in you src folder. There you should also see your main folder. In those flavor folders you can create the res folder and override or add additional resource files.
Here you can create the strings.xml and change the app name as well as drawables and so on.
http://ptrprograms.blogspot.de/2014/08/build-variations-using-gradle-and.html
Edit based on comment:
I got these two flavors:
productFlavors {
appFlavorA{
applicationId "de.test.appaA"
}
appFlavorB{
applicationId "de.test.appaB"
}
}
Now I got the following structure for strings.xml
ProjctRoot/app/src/main/res/values/strings.xml
ProjctRoot/app/src/appFlavorA/res/values/strings.xml
ProjctRoot/app/src/appFlavorB/res/values/strings.xml
This will result in the usage of the corresponding strings.xml for the flavorflavor
If I understand the question correctly, you want to have different app names based on your product flavors.
In your gradle file:
android {
productFlavors {
stagingone{
}
stagingtwo{
}
}
}
Assuming that you have this sample entry, just place a strings.xml file that contains different app_name for each of the flavors.
src/stagingone/res/values/strings.xml
src/stagingtwo/res/values/strings.xml
I don't think you can manually do that (change app names at runtime), as highlighted in this Stackoverflow thread.
Android : Change App Label Programatically
But, there is another way. Do it the same, but for app launcher icons for the flavors. Good luck.

Android build.gradle importing flavors from another file

I have put the product Flavors into another file called other.gradle and it looks like this:
project.ext.myflavors = {
mock {
applicationId "com.mysite.mock"
}
}
and i am able to successfully access the closure in my build.gradle file like this:
myflavors()
but i get an error that mock method is not defined.
Error:Gradle DSL method not found: 'mock()'
Is there no way to just define code from another file and import the code itself into the build file ? Or how can i import the flavors from another file ?
The build flavors could be defined in a separate file (build_flavors.gradle) like this:
android {
productFlavors {
flavorA {
// ...
}
flavorB {
// ...
}
}
}
and then imported into build.gradle:
apply plugin: 'com.android.application'
apply from: './build_flavors.gradle'
android {
// the rest of your android configuration
}
It is perfectly fine to access productFlavors multiple times. So adding some or all flavors in a script included by your build script will work.
Create a Gradle script that contains the logic that decides which flavor(s) should be added:
if (someCondition()) {
android {
productFlavors {
one {
applicationId = 'com.example.oneapp'
}
}
}
} else {
android {
productFlavors {
two {
applicationId = 'com.example.twoapp'
}
}
}
}
Now include this script from your build script. Reusing the scripts is easy if you put it under (a subfolder of) the root project. For example:
apply from: rootProject.file('build-scripts/flavor-picker.gradle')
Note that your IDE may not notice changes to the flavor selection script or gradle.properties, so if you make changes to them you will probably have to manually reimport the Gradle files to see the correct set of available tasks.

Test Source Directories for Multi Dimension Flavors not recognized in Android Studio

I have an issue with test and androidTest source directories for multi-dimension flavors.
Given the following flavors:
flavorDimensions "taste", "serving"
productFlavors {
chocolate {
flavorDimension "taste"
}
strawberry {
flavorDimension "taste"
}
kiwi {
flavorDimension "taste"
}
sample {
flavorDimension "serving"
}
whole {
flavorDimension "serving"
}
}
There are no issues with "non-test" source directories (of any flavor combination) being recognized in Android Studio:
src/sample, src/whole, src/chocolate, src/strawberry, src/kiwi,
src/chocolateSample, src/chocolateWhole, src/strawberrySample, src/strawberryWhole, src/kiwiSample, src/kiwiWhole
My issue is with "test" source directories.
Only single dimension flavors are recognized: src/testSample, src/testWhole, src/testChocolate, src/testStrawberry, src/testKiwi.
Multi-Dimension flavors are not: src/testChocolateSample, src/testChocolateWhole, src/testStrawberrySample, src/testStrawberryWhole, src/testKiwiSample, src/testKiwiWhole
This is also the case for the "androidTest" equivalent source directories.
I am under the impression that it is the app.iml which is not correctly generated. While understanding that we should NEVER do this, the folders are correctly recognized if I were to manually add in the missing entries.
Why should this work with non-test source directories but fail with test source directories? Is this a known issue or a limitation by the gradle plugin?
I tried researching this, but only found topics related single flavor dimensions for test source folders or mult-dimension flavors for non-test source folders. There is nothing with regards to multi-dimension flavors for test source folders.
You should be able to fix this by modifying your android app build.gradle file. For example, to add testChocolateSample to unit tests and testChocolate to android integration tests:
android {
[...]
sourceSets {
main.java.srcDirs += 'src/main/java'
testChocolateSample.java.srcDirs += 'src/testChocolateSample/java'
androidTestChocolate.java.srcDirs += 'src/androidTestChocolate/java'
}
}
This also works for Kotlin. Just switch java with kotlin.

Separating integration tests from unit tests in Android Studio

I'm trying to separate out integration tests in Android Studio 0.9.
I have added the following to the build file:
sourceSets {
integrationTest {
java.srcDir file('src/integrationTest/java')
}
}
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
I've run into a couple of issues:
The task will run but it doesn't have the rest of the project files available so I get errors about missing classes. There are some Java specific solutions I've found such as:
http://selimober.com/blog/2014/01/24/separate-unit-and-integration-tests-using-gradle/
https://blog.safaribooksonline.com/2013/08/22/gradle-test-organization/
But I haven't been able to figure out how to get this to work with Android Studio. Various combinations of main and main.output and playing around with dependencies don't seem to work, I get errors like:
Error:(33, 0) Could not find property 'main' on SourceSet container..
Which makes sense as the android plugin defines its own source sets, but these don't work either.
The IDE doesn't recognise the directory as a test source directory. For testing purposes I changed the source set name to androidTest and it correctly gets the green folder icon and the tests are run along with the existing unit tests that are already defined in androidTest.
#sm4's answer works indeed for a Java module (with apply plugin: 'java'), but unfortunately not for Android application (apply plugin: 'com.android.application') nor Android library modules (apply plugin: com.android.library).
But I have found a workaround:
Create the folders for your integration tests:
src/integrationTest/java
src/integrationTest/res
Add the sourceSets for your new folders:
sourceSets {
integrationTest {
java {
srcDir file('src/integrationTest/java')
}
res {
srcDir file('src/integrationTest/res')
}
}
}
In a pure Java module the java folder would now turn green and the res folder icon would change. In an Android application/library module it does not.
Now create a product flavor identically named as the folder configured in the sourceSet, and it works!
productFlavors {
integrationTest {
}
}
And to put a cherry on top:
configurations {
integrationTestCompile.extendsFrom testCompile
}
I've done exactly this kind of separation in Gradle, but for a pure Java project, not Android. You are not specifying the classpath in source sets, which I think is the issue. Here's the relevant part of the build.gradle:
sourceSets {
integration {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration/java')
}
resources {
srcDir 'src/integration/resources'
}
}
}
configurations {
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
task integrationTest(group: "verification", type: Test) {
testClassesDir = sourceSets.integration.output.classesDir
classpath = sourceSets.integration.runtimeClasspath
}
integrationTest.dependsOn testClasses
IntelliJ idea picks up the folders under src/integration if they have the standard names (java, resources).

Categories

Resources