Why duplicate folders in an Android project? - android-studio

I've found out that over a course of several years, a lot of programs keep seemingly duplicate "project folders" in the Android Studio, why is that?
To elaborate a bit further, if you import their projects, and if you take a look at there folder structure, there is going to be something like this:
Java
|--com.myproject.spaceInvader
|--com.myproject.spaceInvader(test)
|--com.myproject.spaceInvader(alphaTest)
What are these? Something generated by 3rd party testing tools?

When you create a project in Android Studio, it has a standard structure:
From official documentation:
main
Contains the "main" sourceset files: the Android code and
resources shared by all build variants (files for other build variants
reside in sibling directories, such as src/debug/ for the debug build
type). AndroidManifest.xml Describes the nature of the application and
each of its components. For more information, see the
AndroidManifest.xml documentation. java/ Contains Java code sources.
test
Contains code for local tests that run on your host JVM.
androidTest
Contains code for instrumentation tests that run on an
Android device. For more information, see the Android Test
documentation.

Related

Adding prebuilt native libraries to Android Studio project

I am currently working on a wearOS app written in Kotlin that needs to consume a native library.
I have cloned the library from github and built it. The output consists of a libs folder with 4 different subfolders containing .so files for the different ABIs and a java folder with two java classes to interact with the library.
I have been reading the android docs but I still can't figure out how I need to import these files in my project to actually use the library. I don't have a lot of experience in android development, could anybody explain to me wat to do or just point me to the right docs/integration guide?

Adding unit tests to existing Android Studio project

How do I add unit or integration tests to and existing Android Studio project?
I've tried adding folders in Project View as well as creating tests through the context menu (right-click).
I can't get Android Studio to recognize the folder as test folders.
I've also tried:
How to get Android Studio to recognize file as source (test)
Unit testing in android studio
Create unit tests in android studio
I can't get Android Studio to recognize the folder as test folders.
Following below folder structure, you should be able to see all your test cases labeled as "test" when select your Project View as "Android"
|- src
\-- androidTest // for android instrumentation tests
\-- main // your source code
\-- test // for unit tests
Adding a test directory labeled 'androidTest' is only recognized as a test directory if you already have directory 'test' under your src/ directory.
This is as of Android 3.1.3.
I had a similar issue, my Android TV had no 'androidTest' nor 'Test' directories, and I wanted to add some unit tests to the project.
What I did was to create an new Android project from scratch (regular app, not for TV), and I copied the test directory from the new project into my existing project (mantaining the folder structure that #shizhen mentioned above).
Finally I renamed the package folder's names and I got the ExampleUnitTest to run and work ok. After that, I could begin writing unit tests on my Android TV project.
Hope it Helps! It's probably not the best solution, but it works
Although posted answers are pretty straight forward, you may also try this auto generation plugin. This will handle everything for you and more, it'll also try to auto write test cases.
Use Unit Test Architect
Although TDD should be the approach for writing tests, but there may be a lot of untested code already written many times in larger projects.
One day I got frustrated with writing test cases of existing, older codebase. Hence, I thought of auto generating all the unit test cases.
I have created an Open-source Gradle Plugin which can be used for the above task. It is already hosted on mavenCentral. I have used it to generate test cases for my projects. But it can be used in any gradle project, (android, java, kotlin, kotlin+java). It may be rough around the edges but it has done it's job well for me.
BuildScript Dependency:
classpath "io.github.orange-3:unit-test-architect:$PLUGIN_VERSION"

How to link ndk module using Android Studio

I have an Android Studio project to which I've added an ndk module. My ndk module builds and creates lots of different .so files in various build/intermediates directories. However, when I run the app it is unable to load the library .so from my System.loadLibrary call.
Java files that I include in the same module are found just fine so I was expecting Android Studio to 'find' the ndk files as well.
In looking at the google hello-libs demo I see that they have added a step in the CMakeLists.txt file to set the LIBRARY_OUTPUT_DIRECTORY:
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../distribution)
set_target_properties(gperf
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY
"${distribution_DIR}/gperf/lib/${ANDROID_ABI}")
and then added an associated reference in app:build.gradle
jniLibs.srcDirs = ['../distribution/gperf/lib']
Adding similar entries to my project allowed it to run properly.
Is there a simpler way to cause an ndk compiled module to be included in the main application? If not, is there a way to do it without having to figure out all the relative ../../ path elements. I guess I was hoping the module CMakeLists.txt would be
the same configuration regardless of what other projects it might be included in but having to know where it's including project is located seems to break this.

What do Android Studio modules produce?

What do Android Studio modules produce? e.g. a jar per module, an apk per module?
How do dependencies between modules affect these?
Or is it solely an IDE fiction?
Well if you are getting started I would advice you check out this page:
https://developer.android.com/tools/projects/index.html It contains what you seek.
"There are only a handful of files and folders generated for you, and some of them depend on whether you use Android Studio or the android tool to generate your module. As your application grows in complexity, you might require new kinds of resources, directories, and files."
-As in the the dev.android link I just posted above

Android Studio - How to open multiple project in single window?

I have downloaded Android Studio and started using it for my Android development.
I need to know, how to open multiple number of projects in a single window like Eclipse. Expecting some help, thanks.
IntelliJ IDEA creates a project for the entire code base you work with, and a module for each of its individual components. So, IntelliJ IDEA module is more like an Eclipse project, and project is roughly similar to Eclipse workspace. There's no exact equivalent to Eclipse's workspace that contains all your work, but you can open multiple projects in multiple frames at the same time.
This table can help you see how Eclipse and IntelliJ IDEA concepts map to each other:
Eclipse IDEA
Workspace Project
Project Module
Project-specific JRE Module JDK
User library Global library
Classpath variable Path variable
Project dependency Module dependency
Library Module library
To use the library add it as a dependancy:
File > Project Structure > Modules > Dependencies
Then add the module (android library) as a module dependency.
Open two projects in a single window is not possible in Android Studio / IntelliJ IDEA. So, when you open a second project, you'll have to decide:
New projects can either be opened in a new window or replace the project in the existing window.
How would you like to open the project?
This limitation is useful because your window offers project specific features, like the Changes tab for VCS information, etc.
How to use library projects?
For now, you can copy the library project into your project folder and declare it as a module dependency. If you use the same libraries in different projects, you will end up having the code multiple times.
ProjectA ProjectB
facebook-sdk/ actionbarsherlock/
actionbarsherlock/ bin/
bin/ src/
src/ ...
AndroidManifest.xml
While this feels kind of inconvenient, it helps having all the required sources in VCS. Soon, Gradle, the new build system, will manage these dependencies pleasantly. Here's an example of how the Gradle build could look like to include ActionBarSherlock or similar libs:
repositories {
mavenCentral()
}
dependencies {
compile 'com.actionbarsherlock:library:4.2.0'
}
In this answer you'll find some reasons why this solution does not work yet.
write code in settings.gradle
include ':ProjectName'
project(':ProjectName').projectDir = new File(rootDir, '/ProjectName')

Resources