I'd like to test my Fragment. In order to do it I need to declare a test Activity which would contain the testing fragment.
As of 1.0 AndroidStudio release I'd like to know whether there's opportunity to declare an Activity (for testing purposes) inside the debug build type or androidTest folder. At this point I have to add java file to the main Sourceset and corresponding record into main AndroidManifest.xml.
Thanks.
7 years and no answer.
I do not know if it is possible to add it to androidTest, but you can add it to the src folder under a folder named debug/java/com/app_name/package_name, then, at the very least, it won't get added to the release
Related
In on of my Android Studio Kotlin projects, sometimes .KT is shown and sometimes not, in the Project explorer, in the Activity folder.
What is the criteria used by Android Studio to show or hide the .KT extension ?
Thanks
If a kotlin file has exactly one outer class/object/interface as the name of the file, then Android studio just shows the name without extension.
This could happen if activity does not meet one or more of these points:
One activity file should contain only one class.
One activity file class name declaration must correspond to the good filename (filename must equals class name).
There are no top-level declarations (constants, variables).
Solution :
One activity file should contain only one class.
One activity file class name declaration must correspond to the good filename (filename must equals class name).
My Problem might not be valid. And the points I mention here might be little incorrect as I am neither perfect nor expert.
I have a shopping application and I want to start building a modular application. Like I want to add Affiliate User( the module which adds certain functionality or this will display some extra pages in an application) in the application.
A similar situation happens in the Frameworks: We add and enable the certain module and in return framework load everything as required.
for this, I want following changes like:
add an entry in the NavigationView displaying "Affiliate Label".
load fragments (just adding one more fragment for one more label/option from navigation view).
Let's say I have a library project that contains a Fragment and all relevant code.
How can I build the application automatically let's say just by writing "true" somewhere in the XML?
Automatically here means label is added, Intents are performed on click of label etc.
<Modules>
<enable>true/false</enable>
</Modules>
This is just the simple scenario.
You could do this (like everywhere when it comes to writing code) in many possible ways.
The "file" way:
Make a new file named something like modules.txt with key value pairs. Load the file and check whether a module is enabled or not.
The "Constant" way:
Make an abstract class which only contains public static final variables which describe your modules.
The "package manager" way:
See create Android Application plugins/extensions (apk)
The "multiple" apk way:
Note that this is not reccomended!
we encourage you to develop and publish a single APK
multiple apk support
To add to codewing's answer, you can also use Gradle's resource management capabilities to accomplish this, so you only ever need to look in one place for an enabled/disabled status.
For this, you have 2 solid options.
The first starts with a boolean which can be split by flavor:
<bool name="module_x_enabled">true</bool>
The second would be to inject your values into a String resource after Gradle merges the resources by adding something like this to your build.gradle file, then comparing that enabled value:
<string name="module_x_enabled">MODULE_X_ENABLED_PLACEHOLDER</string>
android.applicationVariants.all{ variant ->
variant.mergeResources.doLast{
replaceInValues(variant, 'MODULE_X_ENABLED_PLACEHOLDER', MODULE_X_ENABLED)
}
}
def replaceInValues(variant, fromString, toString) {
File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/values/values.xml")
String content = valuesFile.getText('UTF-8')
content = content.replaceAll(fromString, toString)
valuesFile.write(content, 'UTF-8')
}
Where MODULE_X_ENABLED would be a setting in your gradle.properties file like:
MODULE_X_ENABLED=true
Edit: or better yet,
Why not pull the settings from some kind of server so that you don't need to rebuild and relaunch to update a client's module?
I am using product flavors for my app and I need different default activities for each product flavor. Since I can not override classes in main/java, I need to delete the default activity in this folder and copy a custom version of it to flavor1/java and flavor2/java.
Now my project does not compile, because it can not find the default activity. In the manifest in main/java I did not declare a default Activity. Only in the two flavors manifests. Any ideas how to solve this issue?
Someone please fix my title. This problem is so weird that I have no idea what to call it.
Here is what my src folder should, and usually does, look like in my Package Explorer view:
Here is what is looks like now:
I don't remember what I did to do this (Eclipse is the most buggy program I've ever used in my life). I think it might have happened when I was attempting to create a JAR file with my classes.
The problem with this is:
My app always crashes on startup.
I don't have error messages. I could write iynbiun in my classes and there would be no red underline. It will always let me run (therefore compile my classes) my app even with uncompilable code.
How can I make it go back to the way it was in the first image?
A little help for you.
Just create a new package named as com.mikeyaworski.calculator as it before(make sure same package name is inside gen folder).
Then drag the calculator folder from com.mikeyaworski to com.mikeyaworski.calculator.
I think after this step you just have to rename the package because after dragging of calculator folder the package should be renamed as com.mikeyaworski.calculator.calculator.
So just rename it as usual and delete the empty com.mikeyaworski package.
As my knowledge if our package name is com.mikeyaworski.abcd then all activities are saved inside com folder -> mikeyaworski folder -> abcd folder .
Sometimes due to some changes the package path destroys and all our android related classes becomes plane java files. So if we reconstruct the package path and put all java classes inside this it must be as before.
I had developed 3 applications in android where the major functionalities are the same but the UI looks different. Images and the background color of the screens are different.
NOw, i want to create a single code base from which i can generate multiple .apk files for the 3 apps.
I tried creating 3 different packages for src folder for the 3 apps. But i dont know how to set the res folder for these apps.
Need pointers on creating a single code base from which we can generate multiple .apk files which includes only the respective src and res folders.
Use an Android Library Project that contains all your common code.
Create separate Android projects that reference the Library Project (you will need to copy your Manifest into each of these and make sure all components are declared with their full Java package name).
Put any resources specific to each app (drawables, colors etc) into the individual project resource folders and they will override similarly named resources in the library project at build time.
i think the best option is to use ant, you'll need to add an ant target for each build and change the resource folder.
if you use the generated build.xml, the res folder is defined like this
<property name="resource.absolute.dir" location="res" /> so you'll want to override that
Can't you put all of your common code into a library project and then just reference that project from each of the 3 unique projects that each contain the relevant resources.
Update: This answer is now obsolete when using the Gradle build system.
Why don't you use a single application, that does three different things based on SharedPreferences values set by the user, or from context at install time. If you really want to separate, you can have three different activities, and you decide which one to launch from a silent main Activity that redirects to either of the different ones.
An alternative is to have a unique activity that inflates itself dynamically from 3 different layouts at onCreate time.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (...custom check for layout... equals(layout1)) {
setContentView(R.layout.main_layout1);
} else if (... equals(layout2)) {
setContentView(R.layout.main_layout2);
} else if (... equals(layout3)) {
setContentView(R.layout.main_layout3);
} else {
throw new IllegalStateException("Unknown layout!");
}
... your onCreate stuff....
}
It will make code maintenance easier (only one code source to modify, only one version-list and changeset to maintain)
Check here:
How to use SharedPreferences in Android to store, fetch and edit values
I would suggest using Gradle flavors.
It seems to explain all the basics really well. I just finished converting to Gradle today, and it works great. Custom app icons, names, and strings, etc.
As the website explains, part of the purpose behind this design was to make it more dynamic and more easily allow multiple APKs to be created with essentially the same code, which sounds similar what you're doing.
Also see a recent question I had, referring to your project structure and using custom code for each app.