I'm ussing this library, the problem starts with:
Tray is based on a ContentProvider. A ContentProvider needs a unique
authority. When you use the same authority for multiple apps you will
be unable to install the app due to a authority conflict with the
error message:
Failure [INSTALL_FAILED_CONFLICTING_PROVIDER]
because i didn't read this before, i used this library to fix problems with multi thread SharedPreference, this project had to become in a module to use in multiple projects.
At the moment of installing every App with this Module, throws the Failure [INSTALL_FAILED_CONFLICTING_PROVIDER] exception.
Because authority is already used in the first installed app.
The questions are:
How can i skip installation of Module that is already installed? (Installing the rest of the app, so error will disapear)
else, How can i structure the projects? to be able to install multiple apps built with the Module that have the ContentProvider Library.
Fixed
For those that need something similar these are the steps to follow:
Step 1. I configured Tray Library in a Main Project "required for the rest of apps"
(the app that hosts database preferences)
Step 2. I extracted from repository the required classes to connect with the Tray ContentProvider, which are:
AbstractTrayPreference.java
ContentProviderStorage.java
ItemNotFoundException.java
Migration.java
OnTrayPreferenceChangeListener.java
PreferenceAccessor.java
Preferences.java
PreferenceStorage.java
SqliteHelper.java
TrayContract.java
TrayDBHelper.java
TrayException.java
TrayItem.java
TrayPreferences.java
TrayProviderHelper.java
TrayRuntimeException.java
TrayStorage.java
TrayUri.java
WrongTypeException.java
in a new package for the common Module.
Step 3. I hardcoded authority name via constant variable:
This must match with Main Project configuration:
// bild.gradle of Main Project
resValue "string", "tray__authority", "<your.app.package>.tray"
// TrayContract.java of Module
#NonNull
private static String getAuthority(#NonNull final Context context) {
return TextUtils.isEmpty(sTestAuthority) ?
AppConstant.TRAY_AUTHORITY: // <-- this one = <your.app.package>.tray
sTestAuthority;
}
Finally I can use the class normally:
// etc
import <your.app.package>.tray.TrayPreferences;
// etc
public class AppPreferencesManager extends TrayPreferences {
// etc
}
it worked :)
Related
Background
Suppose I make an Android library called "MySdk", and I publish it on Jitpack/Maven.
The user of the SDK would use it by adding just the dependency of :
implementation 'com.github.my-sdk:MySdk:1.0.1'
What I'd like to get is the "1.0.1" part from it, whether I do it from within the Android library itself (can be useful to send to the SDK-server which version is used), or from the app that uses it (can be useful to report about specific issues, including via Crashlytics).
The problem
I can't find any reflection or gradle task to reach it.
What I've tried
Searching about it, if I indeed work on the Android library (that is used as a dependency), all I've found is that I can manage the version myself, via code.
Some said I could use BuildConfig of the package name of the library, but then it means that if I forget to update the code a moment before I publish the dependency, it will use the wrong value. Example of using this method:
plugins {
...
}
final def sdkVersion = "1.0.22"
android {
...
buildTypes {
release {
...
buildConfigField "String", "SDK_VERSION", "\"" + sdkVersion + "\""
}
debug {
buildConfigField "String", "SDK_VERSION", "\"" + sdkVersion + "-unreleased\""
}
}
Usage is just checking the value of BuildConfig.SDK_VERSION (after building).
Another possible solution is perhaps from gradle task inside the Android-library, that would be forced to be launched whenever you build the app that uses this library. However, I've failed to find how do it (found something here)
The question
Is it possible to query the dependency version from within the Android library of the dependency (and from the app that uses it, of course), so that I could use it during runtime?
Something automatic, that won't require me to update it before publishing ?
Maybe using Gradle task that is defined in the library, and forced to be used when building the app that uses the library?
You can use a Gradle task to capture the version of the library as presented in the build.gradle dependencies and store the version information in BuildConfig.java for each build type.
The task below captures the version of the "appcompat" dependency as an example.
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0'
}
task CaptureLibraryVersion {
def libDef = project.configurations.getByName('implementation').allDependencies.matching {
it.group.equals("androidx.appcompat") && it.name.equals("appcompat")
}
if (libDef.size() > 0) {
android.buildTypes.each {
it.buildConfigField 'String', 'LIB_VERSION', "\"${libDef[0].version}\""
}
}
}
For my example, the "appcompat" version was 1.4.0. After the task is run, BuildConfig.java contains
// Field from build type: debug
public static final String LIB_VERSION = "1.4.0";
You can reference this field in code with BuildConfig.LIB_VERSION. The task can be automatically run during each build cycle.
The simple answer to your question is 'yes' - you can do it. But if you want a simple solution to do it so the answer transforms to 'no' - there is no simple solution.
The libraries are in the classpath of your package, thus the only way to access their info at the runtime would be to record needed information during the compilation time and expose it to your application at the runtime.
There are two major 'correct' ways and you kinda have described them in your question but I will elaborate a bit.
The most correct way and relatively easy way is to expose all those variables as BuildConfig or String res values via gradle pretty much as described here. You can try to generify the approach for this using local-prefs(or helper gradle file) to store versions and use them everywhere it is needed. More info here, here, and here
The second correct, but much more complicated way is to write a gradle plugin or at least some set of tasks for collecting needed values during compile-time and providing an interface(usually via your app assets or res) for your app to access them during runtime. A pretty similar thing is already implemented for google libraries in Google Play services Plugins so it would be a good place to start.
All the other possible implementations are variations of the described two or their combination.
You can create buildSrc folder and manage dependencies in there.
after that, you can import & use Versions class in anywhere of your app.
In my app I have two product flavors. Both of them used to work fine. Suddenly I started to get ANR error when I try to run the application and the app freezes during start up. The error in Logcat is:
ANR in com.myapp.development
PID: 7937
Reason: executing service com.myapp.development/com.myapp.development.service.MyFirebaseInstanceIDService
The method MyFirebaseInstanceIDService.onTokenRefresh is never called. The app freezes before any of my own code is executed.
The production flavor still works fine.
Everything in the Firebase console is set up correctly. The app is in the Play Store and both flavors used to work.
productFlavors {
production {
applicationId 'com.myapp'
dimension "default"
}
development {
applicationId 'com.myapp.development'
dimension "default"
}
}
As you can see the only difference between the two flavors is the application id. And if I change the application id in the developmnet flavour it starts to work (the app does not freeze).
I tried the following tests:
I change the package id of the development flavor by adding just one letter: 'com.myapp.developmentX'. I also make the corresponding change in google-services.json file . Then the development flavor also starts. But of course Firebase services do not work, because the new application id is not configured in Firebase console.
In the manifest file I remove the MyFirebaseMessagingService and MyFirebaseMessagingService and MyFirebaseInstanceIDService. Then the app still freezes at start up, but the error in logcat changes slightly:
ANR in com.myapp.development
PID: 683
Reason: executing service com.myapp.development/com.google.firebase.iid.FirebaseInstanceIdService
I checked out an old git branch and tried to start it but the problem is still there.
I also tried: Uninstall the app and install again. Clean project. Delete the build directories. Restart the test device. Use the Android emulator. Restart the computer. Restart Android Studio. Update Android Studio to the latest 3.1.4 for Mac. But non of these helped.
It looks like the problem has something to do with the application id and Firebase, but I cannot find what.
Anyone has any ideas?
Update after comments from Shark and sebasira.
In Firebase console I have one single project and there are two applications defined for it - one for each flavour. In that case there is no need of multiple google-services.json, one for each flavour. The file is the same for all flavours and can be places in the root directory :
app/
google-services.json
The file structure is like that:
{
"project_info": {...},
"client": [...],
}
As you see, there is an array of clients. All application IDs (all flavours) are defined in that array. That's why if I download from Firebase the google-services.json file for each application, they are all the same.
Anyway, I tried to put a different google-services.json file for each flavor like that:
app/src/
flavor1/google-services.json
flavor2/google-services.json
But that didn't help.
I was facing exactly same issue, and my app configuration was also exactly similar as yours.
The issue was caused by the latest Facebook SDK I was using in my code, it crashes the Firebase before the app start hence the app goes to ANR.
I set my Facebook SDK version as 4.35.0 and that solved the issue.
Try it out and let me know if it was helpful.
I am attempting to implement message sending from my Android app using AWS SQS. I have included aws-android-sdk-core-2.6.15 and aws-android-sdk-sqs-2.6.15 jar files. With just these, I am unable to resolve AmazonSQSClientBuilder (import com.amazonaws.services.sqs.AmazonSQSClientBuilder) and AWSStaticCredentialsProvider (import com.amazonaws.auth.AWSStaticCredentialsProvider).
These work if I include the aws-java-sdk-1.11.278 jar file. However, this causes the 'DuplicateFileException' when I try to build. If I include only this jar, then I get the 'GC overhead limit exceeded' error.
Is there a smaller package that will allow the import of these necessary classes?
Thanks!
The AWS SDK for Android does not follow the same pattern as AWS SDK for Java.
The applicable constructors can be found in these files depending on whether you want an async client or not:
https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-sqs/src/main/java/com/amazonaws/services/sqs/AmazonSQSAsyncClient.java
https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-sqs/src/main/java/com/amazonaws/services/sqs/AmazonSQSClient.java
One example:
AWSCredentialsProvider awsCredentialsProvider = // Choose one of many classes that implement this for instance, CognitoCachingCredentialsProvider
AmazonSQSClient client = new AmazonSQSClient(awsCredentialsProvider);
About Bintray-release plugin
I am using bintray-release to upload my library to maven.Its doc says how to use it:
Use the publish closure to set the info of your package:
publish {
userOrg = 'novoda'
groupId = 'com.novoda'
artifactId = 'bintray-release'
publishVersion = '0.3.4'
desc = 'Oh hi, this is a nice description for a project, right?'
website = 'https://github.com/novoda/bintray-release'
}
Finally, use the task bintrayUpload to publish
$ ./gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false
In my case
Then I define my publish closure:
publish {
groupId = 'com.uniquestudio'
artifactId = 'parsingplayer'
publishVersion = '2.0.6'
website = 'https://github.com/TedaLIEz/ParsingPlayer'
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream() ;
properties.load( inputStream )
bintrayUser = properties.getProperty('bintrayUser')
bintrayKey = properties.getProperty('bintrayKey')
}
As you can see,out of safety I put bintrayUser and bintrayKey into local.properties.
My Question
First
I know I can put bintrayUser and bintrayKey in loacal.properties and gradle.properties.Is there any other way to store private data while I don't think is't suitable to store private data within current project ?
Second
Everything is ok but when I push my project to CI.I get error:
/home/travis/build/TedaLIEz/ParsingPlayer/local.properties (No such file or directory)
So I want to know How gradle task deal with extension objects,in my case,publish object.Is there any way to fix it?
First, I have to tell you that it is not recommended to ask two questions at once via StackOverflow, mainly because it may be hard to choose a correct answer, if two answers help you with the different questions you asked.
Anyhow, I'll try to answer both of your questions:
First
To use an additional properties file (local.properties in your case) is not a Gradle approach. It is in fact pure Java. You should only read properties on your own in very rare cases and never in a build script. If you really need an additional properties file, develop a Gradle plugin, which handles the file access.
Gradle automatically reads the gradle.properties file, but not only in the project directory, but also in the user-specific gradle home directory (e.g. C:\Users\*<User>*\.gradle). This is helpful to define private data, which won't find its way into version control, even if you forget to ignore the files manually. The defined data will be accessible to any project.
Second
Well, I assume the file local.properties does not exist, because you did neither put it under version control nor let your CI add it automatically. Where should the login data come from?
The solution is simple. Just add the required data to the CI user gradle home directories (e.g. /home/travis/.gradle) gradle.properties file. This way, you can also simply add access right management, by entering the login data of a CI user. Local builds will be published by your local user account (if allowed), CI builds by the CI system.
Appendix
Your question includes the Gradle specific term 'extension', but, to be honest, it got nothing to do with your question. It is correct, that most configuration in Gradle is done via so-called extension objects, that are added to the Project object, but it is an internal term, you do not need to understand it to fix this problem.
Edit: Comment answer
Now I can understand your confusion. Gradle distinguishes between the configuration phase and the execution phase. Nearly everything in your build script is executed during the configuration phase, only task actions (what a task does, e.g. copying, deleting ...), doFirst and doLast closures (so basically tasks) are executed during execution phase. If you define the list of tasks to be executed (via command line), it only affects the execution phase, but your configuration code will be executed at every single build, even if only one independent task is executed afterwards.
To solve this problem, follow the solution in the First block and add your private data to the user-specific Gradle directory gradle.properties file. It will be added to the project object and therefor, it will be accessible from the build file. But, since the file (or the data) does not exist on your CI, accessing it directly will raise an error when building on the CI. You can use the findProperty(propertyName) method as a fail-safe way to access the property value. If the property does not exist, it returns null (in the configuration phase), so no error occurs, as long as you don not execute the bintrayUpload task (which is not your goal on the CI).
This test originally ran fine. Checked out a new branch several days later (with commits from many other developers) and it no longer works.
Test class in the mylibrary library module:
import com.company.mylibrary.BuildConfig;
#RunWith(RobolectricGradleTestRunner.class)
#Config(constants = BuildConfig.class, manifest = "src/main/AndroidManifest.xml", sdk = 21)
public class MyTest {
I have also tried:
#Config(constants = BuildConfig.class, sdk = 21)
#Config(constants = BuildConfig.class, manifest = Config.NONE, sdk = 21)
In the library module's build.gradle
dependencies {
.
.
testCompile 'org.robolectric:robolectric:3.0'
Error message when running inside AS is:
java.lang.RuntimeException: build/intermediates/manifests/full/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml
Error message when running from command line is:
com.company.mylibrary.framework1.feature1.MyTest > testMethod STANDARD_ERROR
java.lang.RuntimeException: build/intermediates/manifests/full/debug/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml
A) Don't know why it is looking there for the manifest
B) That file/directory does not exist
C) src/main/AndroidManifest.xml does exist
Things I have tried:
- deleted the build directory in that library module
- restarted Android Studio
- Build/Clean
- Build/Rebuild Project
- run the test (both inside AS and from command line)
- and tried different versions of the #Config notation
Seems to be in a wonky state that I cannot clear.
I am working on a MacBook Pro. Android Studio 2.0 beta5
You need to set the working directory within the test's run configuration to the module directory.
Well, I've tackled the issue you're facing right now several times and found solution suitable for myself.
Generally, if your test logic does not require access to the application's resources, it's worth using usual RobolectricTestRunner as the time of the test execution is relatively shorter comparing it to the test execution time under RobolectricGradleTestRunner.
If, for some reason, you need access to the specific AndroidManifest.xml file, IMO it's better to come up with test file rather than to operate on the project's one.
By saying 'test file' I mean the following:
Let's start by defining what are the methods that can help us to obtain path to the resources files. The goal is to be able execute tests under Android Studio and, what's more relevant, via CLI (gradle :project:testBuildTypeUnitTest)
Java's System class: System.getProperty('user.dir') returns User's current working directory. Obtaining current directory we are in may help us to obtain paths to the resources we need to run our test having them provided.
Overriding RobolectricGradleTestRunner. To create our customized test runner we need the AndroidManifest.xml, the res directory and the assets directory paths:
public class CompassApplicationRobolectricTestRunner extends RobolectricGradleTestRunner {
private static final int TARGET_SDK_VERSION = Build.VERSION_CODES.LOLLIPOP;
private static final int MIN_SDK_VERSION = Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
public CompassApplicationRobolectricTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
#Override
protected AndroidManifest getAppManifest(Config config) {
final String manifestPath = PathResolver.resolveAndroidManifestPath();
final String resourcesPath = PathResolver.resolveResPath();
final String assetsPath = PathResolver.resolveAssetsPath();
AndroidManifest manifest = new AndroidManifest(
Fs.fileFromPath(manifestPath),
Fs.fileFromPath(resourcesPath),
Fs.fileFromPath(assetsPath)) {
#Override
public int getTargetSdkVersion() {
return TARGET_SDK_VERSION;
}
#Override
public int getMinSdkVersion() {
return MIN_SDK_VERSION;
}
};
return manifest;
}
}
Below, is the link to the example that worked for me. It was developed, however, some time ago and from the time perspective I see it can be done more elegant way so if you decide to apply this solution to your project, organize your path constants to be static and immutable:
https://github.com/dawidgdanski/android-compass-api/blob/master/app-tests/src/test/java/pl/dawidgdanski/compass/PathResolver.java
It's worth remembering that File.separator returns system's default directories separator. It's extremely useful when it comes to provide system-independent paths separated with default separation symbol.
Eventually, if the solution described above is not the one you want to follow, read decent article about setting up testing environment available here:
http://artemzin.com/blog/how-to-mock-dependencies-in-unit-integration-and-functional-tests-dagger-robolectric-instrumentation/
Hope that solves your problem.
In my case, I was running a single test manually (Right click and run) from inside Android Studio and Roboelectric wanted a RELEASE version. The question above was about debug but my test runs for some reason wanted a release version of the manifiest.
java.lang.RuntimeException: build/intermediates/manifests/release/AndroidManifest.xml not found or not a file; it should point to your project's AndroidManifest.xml
I had never done a production build in this project so that build directory had never been created.
After wrestling for a bit with no success (setting the path in configuration, trying to get the path in my CustomRoboelectric file), I just generated a production build so that I had the release path created with a manifest and everything worked.
So my solution was to just run the build to create what Roboelectric wanted.