Sharing Android tests between two projects - java

I'm writing automated unit tests for an Android application, and I have come across a very odd situation.
The application is carefully constructed to target API8 - API 19 without needing multiple releases. One part of the tests, however, requires that I test a specific class without any internet access or mobile data. I've found a very hacky way to do this, but the solution comes in two parts: one for API 10 and above, and one for API 8 that requires a permission not available to applications targeting >= API 10.
What I want to do is to create two test projects, one that targets API 8 and one that targets everything else. What I want to avoid is duplicating the test files that work across all versions. As far as I can tell, there's only the manifest and a Utils class that needs to be different.
Is there any way to store the tests in a central location, and have them included and ran from both tests?

On Eclipse, create the tests to be shared on one of the projects, as usual.
When you are happy with the test result, go to the other folder and create a new Folder inside:
Right Button on Project >> New >> Folder
Press "Advanced Button"
Select "Link to alternate location (Linked Folder)
Press the "Browse..." button and browse to the folder where previous tests are located.
Finally, right click on the linked folder and select:
Build Path >> Use as Source folder.

Related

How To Upload Android Library For Private Use Only?

I have an Android app but require to create two Different copies of it with different package names. The reason for wanting two package names is that I want to upload them to two different Stores which requires the developer to provide a different package name.
To simplify the update process and fixing the issues I thought of dividing my code to different libraries.
I have now created an android library and now want to upload it so that I can simply use the library in my projects by simply adding the line compile com.adc.aaa...
But I do not want those library to be accessible to other developers either directly of maybe through google Search. Is it possible to achieve this?
These are few links which I have visited regarding this matter
1) https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
2) https://mayojava.github.io/android/library/creating-and-distributing-your-own-android-library/
Can anyone please help.
Suposing your module in android studio is called "library" you can simply add the module to your app project locally without the need to upload it to bitray.
You can add a local module as a depedency to your project using AS or manually:
1) Using AS:
Go to File -> Project structure. Click on the app module and then depedencies, click on the plus sign and then choose 3 - Module Dependency. If you library is in the same folder as your app it'll show up there. Just select it and it will be added as a depedency like the other compile dependencies.
2) Manually
In your settings.gradle file add:
include ':app', ':library'
In your app leve build.gradle:
implementation project(':library')
If you library module is somewhere else you can inport it to your project using:
File -> New-> Import Module

How can SonarQube be used to view results for specific directories (packages) of a project?

I want to investigate how certain rules and issues have been violated in a certain package, however, from what I have seen in the Components-interface in SonarQube, it is only possible to view leaf node-directories and I am interested in viewing the entire folder, including sub-folders and files in that folder.
E.g.
Project
src
package1
subdirectoryone
done
code-a.java
dtwo
code-x.java
dthree
code-i.java
code-1.java
code-2.java
subdirectorytwo
package2
subdirone
subdirtwo
In this example I would like to investigate the issues and rule violations in subdirectoryone including the code files at all levels in the directory.
Is there a way I can do this?
I use something like this:
<sonar.projectName>Some project name (com/your-directory only)</sonar.projectName>
<sonar.exclusions>org/**/*, com/acme/**/*</sonar.exclusions>
<sonar.test.exclusions>org/**/*, com/acme/**/*</sonar.test.exclusions>
<sonar.sources>${project.build.sourceDirectory}/com/your-directory</sonar.sources>
This excludes everything but the code I want to be analyzed (exclusions properties) and define my source dir as ${project.build.sourceDirectory}/com/your-directory (this is where the code I want to be analyzed is)
Then the analysis is run using maven:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package -Dtest=com/your-directory/**/*Test.java
(note the -Dtest= restriction for the tests)
Some reading: http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus
In SonarQube 4.5 if you start from the dashboard and click the Issues link you will see all the issues for every module. In the left box below the lists of Severities and Rules is a list of all the modules and sub-modules in your project. If you select from that list it will narrow down the centre and right boxes to just the packages and files within that module. Unfortunately it doesn't filter the rules or severities in the top boxes.
In SonarQube 5.3 if you go to the issues tab you can select the module checkbox on the left side and select from your high runners. Near the top of the page is a hierarchy for the module you selected which you can click on to see the dashboard for that module or sub-module and then click issues for just that part. Then if you want a sub-module that isn't in high runners note that the URL is editable if you are careful and know your target hierarchy.
I haven't found a better way than that yet in 5.3, but after only a few days playing with a recent 5.3 upgrade I am generally liking the UI changes.

How to hide unnecessary files from intellij project view?

Given a sample gradle project with project structure as shown:
As you can see there are bunch of stuff you actually dont ever need to see in idea but you need them to exist...
I know about ignored file/folder types under Editor/File Types but those are affecting the libs and projects, idea will ignore them everywhere while i only need to hide few unnecessary entries in project view.
Primary question : how do we hide them from project view?
Also for very big projects somethimes it is easy if some related modules are shown one after another... but do not do like that alphabeticaly.
Secondary question : is it possible to have customized order in project view?
I'm using Intellij 15 EAP IU-142.5047.6
Usually I use the Scopes support for filtering out/in elements.
I like this support and it can be use as the scope for other tool windows such as Hierarchy Call, and Find in path dialog. Moreover you can have many scopes and easily switch between them. The support for scratch files and version control - change sets - benefits from scopes support.
In order to create a new Scope:
open Settings > Appearance & Behavior > Scopes
Create a new scope by clicking on + icon
and then use include/exclude (recursively)
Kind regards
In IntelliJ, version 2022.1.3 Ultimate Edition:
Settings -> Editor-> File types
select "Ignored Files and Folders" tab
Click on the "+" sign to add the file extension to be ignored (files with this extension will not be displayed in the "Project" view).

Implementing ActionBar-PulltoRefresh by Chris Banes, library errors etc

Following up on my previous question about importing a certain library from github in my project, I decided I wanted to implement a different one, but I get lots of errors at this point.
Importing Android Listview refresh by pull, project-properties error
In line with Google currently using Chris Banes' ActionBar-PullToRefresh: https://github.com/chrisbanes/ActionBar-PullToRefresh,
I've decided I wanted to used that library as well for the Android app that I'm currently developing for my internship. When I try to import the existing code into the workspace from the unpacked zip-file though, I instantly get a ton of errors, in the library as well as in the example activities. For instance, in the library folder, in ActionBarPullToRefresh.java, it underlines Class within the first private hashmap and says references to the generic type Class should be parameterized.
The IDE (eclipse) also gives tons of errors to the other folders included, and when I add the library folder to the libraries of my main project, using the properties -> android tab, it carries over the errors to that project (quite logically).
Could anyone send me into a direction as to how I should successfully import and use this library?
I will write this here for future PullToRefresh users. I use IntelliJ IDEA instead of eclipse, so remember that the word Module and Project are equivalent. When you have to add a project to Eclipse, the action is add module in IDEA.
Example with ActionBarSherlock (ABS from now).
What you have:
You have a Module named: MyProject. This is your main Android module.
You want to add PullToRefresh to a few Fragments (for example) in your MyProject.
Step One: Download and Copy.
I personally like to have a clone or fork of the libraries I'm using to read the commits and change logs. Not only you learn by seeing other people's code, but also you get the "latest news" about the libraries you use. It's a win-win situation. Remember what Jeff Atwood said: "Read The Source, Luke". So in order to do this, Clone ActionBar-PullToRefresh from the right URL.
Now I don't like having the clone with my modules/projects so I usually clone projects somewhere else and later manually move what I need. It's tedious but that way I only get what I want. So what do you need then? The clone contains two important directories: Extras and Library. We're going to need stuff from these.
Copy the library directory to your project root: /path/to/your/project/pulltorefresh
Copy the corresponding (ABS or Compat) extra to your project root as well…
This is what you got so far:
/path/to/your/project/MyProject
/path/to/your/project/pulltorefresh
/path/to/your/project/pulltorefresh-abs (or compat)
Step Two Import these:
Import these modules (projects in eclipse) into the project/workspace. This should be simple if you know your IDE, but as a quick reference: File -> Import Module -> find the pulltorefresh folder from above (and the -abs or compat later) and follow the wizard. If you're using Eclipse, check this link to know how to do it.
Now you should have in your left panel (⌘1) the following structure
MyProject
pulltorefresh
pulltorefresh-abs
Step 3 Add the right references
Assuming MyProject has actiobnarsherlock.jar (because you need it) or the corresponding android support library for compat and that you have the corresponding android-support-v13 (or v4) as well…
pulltorefresh doesn't need any library reference.
pulltorefresh-abs needs a reference to: pulltorefresh, android-suppport-vX and actiobbarsherlock
MyProject needs a reference to: pulltorefresh-abs, android-suport-vX and actiobbarsherlock
Of course both android-support and actiobarsherlock could sit in MyProject and be exported (and taken from there) by the other libraries, but that's project management 101 ;)
Step 4 Integrate!
This is easier, just follow the Quick Start Guide.
REMEMBER: in older versions of the lib you had to have an attacher in an Activity, that is no longer necessary. Read the documentation, it's very simple (and much more flexible this way).
Wait, What If You Don't Use Either Sherlock or Compat??
I haven't done this yet, but my guess is:
Don't import -abs or -compat in extras. Just import pulltorefresh.
From your project, reference pulltorefresh.
Hold on… what about imports?!
Getting the right imports is important for any library, you have to pay attention and not blindly let your IDE do it for you (or at least always double check what you're importing). You'd be surprised to see how many "name collisions" are out there.
For Action Bar Sherlock based projects, the imports should be:
import uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
For a non sherlock pure project:
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
Where did you get those?!?!?!?!
By looking at the samples, Luke.
For example, here's for the stock version.
The final step would be to profit… but I leave that up to you ;)
If you still have an issue, the guys are friendly provided you don't ask lazy questions. Feel free to follow the project and participate in the issues page at github.
Good Luck refreshing!
I don't use Gradle myself, but we all should, as it is a quantum leap for build technology in the Java (JVM) world (from Gradle introduction). This is how to reference projects libraries for PTR v 0.9.3:
Your project has to reference PullToRefresh aka PTR
PTR has to reference SmoothProgressBar aka SPB, which you can find here, on github
Both PTR and SPB should be Android libraries, so make sure that checkbox is checked (Properties -> Android -> IsLibrary)
The "tricky" part: Once you have cloned SPB on your local machine copy the contents of SmoothProgressBar\library\src\main\java into SmoothProgressBar\library\src\main\src (that is the structure ADT expects)
remember to add the support library to SPB if you target lower that 14
Assuming you know how to reference an android library project in eclipse (if not, google it), that is all about you need to do.
Good luck! :)
Non-senior developers/git users might find the following link useful (like I did) http://www.rahuljiresal.com/2014/04/actionbar-pull-to-refresh-on-android/
rahuljiresal has created a git project that contains both Chris Banes' Actionbar-PullToRefresh AND the referencing SmoothProgressBar all in one: https://github.com/rahuljiresal/ActionBar-PullToRefresh
The steps described above by Martin i.e.
1. Download/ Clone git project locally
2. Import PTR library into Eclipse
3. Reference PTR library from your project
4. Integrate
are still valid, but the whole process is incredibly easier and plug-and-play.

Multiple builds from same code base in Eclipse

I'm pretty new to Java and Eclipse coming from an iOS/xCode background. I have an iOS project that has 2 builds, 1 that uses a test server and 1 that uses a live server. In xCode this was simply a case of adding a new build target, a Preprocessor Macro, than using #ifdef in code to use separate url's for each build.
Porting this over to Android, I have this list of things I need to do each time I want to build/test between the 2 versions
TO SWITCH BETWEEN LIVE AND TEST
Rename Application Package com.mybus.myapp/com.mybus.myapptestserver (Right click, Android Tools, Rename Application Package).
Rename com.mybus.myapp folder to com.mybus.myapptestserver.
Change Map API key in manifest
Change SENDER_ID in BeginActivity.java (Notification app ID).
Search and change all references com.mybus.myapp/com.mybus.myapptestserver (Including SharedPreferences).
Change URL's in ConnectionHelper.java and PasswordResetConnection.java
Change icon and label in Manifest for Application & BeginActivity.
Remove crash reporter (ACRA) from MyApplication.java.
Change .setSmallIcon(R.drawable.ic_test_launcher) & .setContentTitle("myapptestserver") in GCMIntentService.java
Surely there is an easier way to build seperate builds and allow them both on a device at the same time?
You could use Ant script to do this. Once you customise your Ant script to handle all these changes, you can get the final output by running the ant release command.
An small example from where you could start with. I have written a post about it. This ant script, doesn't change any values in files, but what it does, is outputs the final apk, with a chosen name format, and puts it in a specified folder.
http://techdroid.kbeanie.com/2011/09/automating-builds-on-android-part-1.html
http://techdroid.kbeanie.com/2011/09/automating-builds-on-android-part-2.html
Disclaimer: These are links to my blog posts.

Categories