I have created 2 flavors in Android Studio's Gradle, and I put the MainActivity file in their respective folder structures.
This is how the project looks:
This is the Gradle flavor part:
productFlavors {
free {
applicationId "com.xxxxx.yyyyy.free"
versionName "1.0.0"
}
full {
applicationId "com.xxxxx.yyyyy"
versionName "1.0.0"
}
}
Unfortunately, when I use the full flavor, the MainActivity is considered unset.
Free flavor:
Full flavor:
The project can be compiled in both flavors, it just can't be run on full flavor as it can't find the Default Activity.
The two files have minimal differences.
What should I check?
Perhaps not directly addressing your issue, but have you considered making a pure Java helper class that provides different functionality to your MainActivity?
The problem here appears to be your moving MainActivity is not being recognized by the AndroidManifest when in full mode, but if you have 2 versions of your helper class everything should be good
Edit
If you want to specify two different activities, use a relative path in your AndroidManifest.
<activity android:name=".MainActivity">
Be cautious to try and reuse as much common code as possible between your two activities to save yourself any headaches down the road though
Related
I have two build variants in my app, one is an standard app edition and the second one is a customization app.
productFlavors {
customConfig {
minSdkVersion 14
applicationId 'es.com.custom'
targetSdkVersion 22
versionCode 3
versionName '3.0.0'
}
standard {
minSdkVersion 14
applicationId 'es.com.standard'
targetSdkVersion 22
versionCode 3
versionName '3.0.0'
}
For the customization I have to implement new features, but just for the customization, so those new features will be not available on the standard version. I am not sure what i have to do.
1.- Two classes , one with the standard requirements and one with the custom requirements
2.- In the standard class do something like:
if (getPackageName()==customConfig )
// do the custom things
else
//do the standard things
Build variants are the result of Gradle using a specific set of rules
to combine settings, code, and resources configured in your build
types and product flavors. Although you do not configure build
variants directly, you do configure the build types and product
flavors that form them.
if(BuildConfig.Flavor.equals("customConfig"))
{
}
else
{
}
Read Building multiple flavors of an Android
You must create source directories for each flavor.
So you will be able to maintain a separate file for the specific flavor.
Please go through the link that will help you.
You can do this in one simple way. You should have a folder named "standard" right now in your project. Just create another folder named "customConfig" (Or it might have been created once the gradle synchronizes) in the same folder where the "standard" folder is.
In your "customConfig" create another folder called "res" (Again you might have it already). Then create another folder "values". In "values" folder you can create your value file. Name the file 'values.xml' and return back to your project. You "values.xml" file should be like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="custom_app_id">es.com.custom</string>s
</resources>
Now in your code you can check which variable environment you are in:
if (getPackageName().equals(getString(R.string.custom_app_id)))// for custom
// do the custom things
else
//do the standard things
Hope this help!
I am trying to implement robobinding with product flavors but the custom code classes under the flavor's source directory never get executed.
Has anyone tried anything similar to this use-case? i.e. used some kind of element binders and then implemented product flavors?
On a certain demo project the structure I have:
-app/src/main/java/demo.app.pkg/MainActivity.java
-app/src/free/java/demo.app.pkg.free/MainActivity.java
build.gradle :
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
free {
applicationId "demo.app.pkg.free"
}
paid {
applicationId "demo.app.pkg.paid"
}
}
and when the MainActivity.java class is modified present under free flavor it does get packaged when the free flavor's debug/release is built. and is executed as well containing the custom code.
(On a side Note: some say that in-order to implement product flavors which has different versions of a single class one needs to have a src folder for main containing common code (but not the classes having same name present in the flavor's src folders) and other 2 src folders which have the class(es) having same name etc. which would essentially segregate the two flavors. This wasn't the case with the sample project I tried above^ it worked flawlessly; even when I had the same named class under main src folder.)
Now, the issue lies when I try to implement similar kind of product flavors (as seen above) in a project already having robobinding implemented.
Structure goes like:
-app/src/main/java/demo.app.pkg/MainActivity.java
-app/src/main/java/demo.app.pkg/MainActivityPresentationalModel.java (class to be replaced by class present under 'free' flavor)
-app/src/main/java/demo.app.pkg/Pojo.java
-app/src/free/java/demo.app.pkg.free/MainActivityPresentationalModel.java
This thing doesn't work. Only the class MainActivityPresentationalModel.java under main gets executed.
Now, if anyone has used view binders especially robobinding or SDK's binders (still under beta) they must be aware of that we get access to a presentational model which is essentially logic where we have setter/getters to get/put values so that they get updated on the UI at runtime and hence my motive to have this class in a product flavor.
Any help is much appreciated.
Reference:
For the first sample project kindly have a look at 'Build-It-Bigger' project on gitHub.
So I have an app that would require to versions with different settings, assets etc but uses the same code base so it will generate two apks. I really have no idea how to do it in gradle. can anyone please point me to a site or a technique in which i can do this? thanks.
Start witch adding product flavors to your build.gradle script and set proper ids
productFlavors {
appOne{
applicationId 'com.app.one'
}
appTwo{
applicationId 'com.app.two'
}
}
When that's done create folders in your src directory called
appOne and appTwo. In those folders you can specify version specific stuff like drawable folders, manifest...
When that's done, remember to choose right build variant in buildvariants menu
I am currently using Android Studio to build a app and I have multiple flavours such as the following:
build1 {
applicationId "com.build1"
versionCode 3
versionName "1.2.0"
}
build2 {
applicationId "com.build2"
versionCode 2
versionName "1.1.0"
}
I am looking to include a new build, build3, that is identical to build2 in every way except it will have one extra pop up when the app opens. I understand how to set up a new flavour but How do you expand one flavour to include another? I am looking to share the code base of build3 with build2 and then add the slight extra code needed. Is there a way that you can do this using gradle or will I have to copy all of the code used in build2 into the build3 directory?
EDIT:
build1 and build2 share common code in the main folder but also have some separate code, string, colours, layouts etc. I am looking to let build3 build from build2 and gain access to its own resources.
I'm trying to understand how i can generate multiple APK from one single project.
I'm using gradle and this is my project's three:
ambrogiocore is the core library module that implements all the common classes and resources.
ambrogioremote is the module that has the :ambrogiocore dependence.
It's work. Now i'm able to generate multiple APK. (just one at the moment)
One apk for each module.
The problem is that i need to manually sync all the AndroidManifest.xml for every future module that i'll include in my project.
Is this the correct way?
Can i automate this operation?
I took a look at productFlavor. Just I don't understand if this tool can help me.
What do you think about?
THANK YOU ALL!
**SOLUTION**
Finally! I found the same solution proposed by #Kai!
Flavor is the way!
This is the best approach for a lots of reasons:
Single-module project
You don't need to copy XML files
RoboGuice will work like a charm. (RoboGuice presents some BIG problem on library projects)
http://www.techotopia.com/index.php/An_Android_Studio_Gradle_Build_Variants_Example
If by "one single source core and multiple project that extends "ProductName", "PackageName" and "resources"" you mean to provide each flavor with unique "ProductName", "PackageName" and "resources", then yes it can be done.
To give each flavor its own unique package, set applicationId for each of your flavors like so:
android {
productFlavors {
flavor_1 {
applicationId = 'com.app.flavor_1'
}
flavor_2 {
applicationId = 'com.app.flavor_2'
}
}
}
To give each flavor its own app name and resources, simply put them in a flavor's own directory, Gralde will merge a flavor's resources with your base resources to create a complete one.
For more detail on how Gralde flavors work, please see Gradle Plugin User Guide
This is my final solution!
All the common files, manifest and base resources are under "src/main"
I moved my projects inside "src/projects"
declared two productFlavor (one per project) specifying applicationId and versionName
created two sourceSet (one per project) indicating the specific res folder.