I have two apps that shares same functionality except images,icons,colors,app name and package change and the URL that gets called in the event of network activity.
In iOS we can create two different apps easily from one source code by using the power of targets.
Here is the link on How to do it in iOS
But how to go about it in android
You should leverage product flavors for this.
In your build.gradle, you will define the flavors like so:
android {
productFlavors {
brand1 {
// ...
}
brand2 {
// ...
}
}
}
You can then create flavor specific resources. If you are creating an icon called ic_launcher.png for example, you would typically put it at a location such as main/res/drawable-xhdpi/ic_launcher.png. Instead, you could put the brand1 version at brand1/res/drawable-xhdpi/ic_launcher.png and the brand2 version at brand2/res/drawable-xhdpi/ic_launcher.png.
When you run gradlew build, it will build all variants. You can also build specific variants such by specifying the variant name like so: gradlew assembleBrand1Debug. In Android Studio you can select the variant you want to see using the "build variants" pane.
Related
The problem i am facing is there is two project(apps) similar and there will be more in future and when i change a code in one i will need to do same changes in other projects. when i searched on web i found product flavors but not sure if it's the answer for my problem.
How can i merge two different projects (apps) in playstore. Projects codes are %85 similar can i use flavors to merge both project in one and will i be able to update them in playstore using different jks with different package names and a few different permissions. Will i need AndroidManifest file for each flavor.
How can i solve this problem. Thanks
To change packageName you have to create product Flavor like
flavorDimensions "versionCode"
productFlavors {
doctorApp {
dimension "versionCode"
applicationId "com.doctor.app"
}
patientApp {
dimension "versionCode"
applicationId "com.patient.app"
}
}
After that create two sibling folder of main after switch project View to Project
app -> src -> main /// write all common classes here
app -> src -> doctorApp // doctor related classes here along with their resources
app -> src -> patientApp // patient related classes here along with their resources
create same package name and res folder under patientApp and doctorApp
In Android Studio I created two configurations:
How can I determine in code which configuration I selected?
I know that there is buildConfigField in /app/build.gradle but the names of the buildTypes do not correspond to the configuration names, so I wonder how does that all fit together.
android {
...
defaultConfig {
...
}
buildTypes {
debug {
...
buildConfigField 'boolean', 'DEBUG', 'true'
}
release {
...
}
}
}
I assume that in Android Studio a configuration corresponds to a schema in Xcode and a buildConfigField corresponds to the Environment Variable in Xcode (me coming from iOS world).
How can I determine in code which configuration I selected?
You don't, insofar as a run configuration is an IDE thing, not an Android thing.
what I want is to define environment variable values that I can use in code, e.g. in a debug build variant I want to connect to the development database, in a release build variant I want to connect to the production database
None of that has anything to do with run configurations. Run configurations are for configuring what is to be run:
the main app
tests for the main app
tests for the something library
etc.
debug versus release are build types, one dimension of the build variant. You choose which build variant the run configurations use via the Build Variants tool, docked by default on the lower-left side of the Android Studio IDE window.
To have different code behavior based upon debug versus release, you can:
Examine BuildConfig.BUILD_TYPE, which will be either debug or release
Use buildConfigField to inject values into BuildConfig from Gradle, based upon build type and/or product flavor
Use resConfig to inject values into resources, such as string resources
Use custom source sets per build type (e.g., src/main/ for your common code, src/debug/ for debug-specific code, src/release/ for release-specific code)
You can configure different resource sets. By default main and debug already exist. To determine at runtime which set was used to build the apk create a new resource file in each resource set, e.g.
app/src/main/res/values/resourceset.xml
and
app/src/debug/res/values/resourceset.xml
and place a single string or integer value inside like this:
<resources>
<string name="resource_set">debug</string>
</resources>
and
<resources>
<string name="resource_set">main</string>
</resources>
You may then use getString() to get the value for R.string.resource_set and you can detect which resource set was used.
I am using this technique to include different Google API client IDs depending on the resource set that was used (to enable debugging with Google APIs and release with another fingerprint then debug).
I have 3 jar files in my Android project's app/libs folder:
api-dev.jar
api-qa.jar
api-prod.jar
I want to use api-dev.jar when I work on the app in the studio (default), build a version of the app using api-qa.jar which will be tested by the QA team, then release the production app with api-prod.jar.
How should I do?
So far I read that I should add a
configurations {
qaCompile
...
}
element to app/build.gradle and use
android {
buildTypes {
...
qa {
...
}
}
}
to define the builds.
I don't know how to point to the appropriate libs/dependencies, I don't know how to make one the default one either, especially in my case where the default one is not the one for the production release...
Also if the API requires a specific key for dev, qa and prod, how do I set it up?
By the way the features are exactly the same between the different builds, the user experience is exactly the same, it's why I want to use builds, not flavors.
Check this out - Add dependency to specific productFlavor and buildType in gradle
Add you jars to the libs directory
Create a build type and product flavor
Use the name to create a specific compile command
Use that to specify your jar file in the libs directory
We have a platform to read poetry with a SQLITE Database say SHAKESPEARE.DB .
We also have another poetry say Wordsworth.DB.
So for each poetry database we want to create a separate application like Shakespeare.apk and wordsworth.apk.
During build we want to mention the Database and mention the name of the APK.
How do we Change Database during android build through gradle and deploy different APK's
I would want to Create and Deploy Database specific APK using Gradle ?
Step #1: Use a consistent means of embedding the database in the app. For this answer, I will assume that you are using SQLiteAssetHelper, with the database packaged as words.db in assets/.
Step #2: Create two product flavors for your app in your app/ module's build.gradle file. For this answer, I will call these flavors vanilla and chocolate:
productFlavors {
vanilla {
applicationId "com.commonsware.android.gradle.hello.vanilla"
}
chocolate {
applicationId "com.commonsware.android.gradle.hello.chocolate"
}
}
(replace the applicationId values with ones more relevant to your project)
Step #3: Create a sourceset for each flavor (app/src/vanilla, app/src/chocolate/).
Step #4: Put a different words.db in assets/ of the flavor (app/src/vanilla/assets/words.db, app/src/chocolate/assets/words.db).
Now, when you build vanilla, it will use the vanilla edition of words.db. When you build chocolate, it will use the chocolate edition of words.db. No Java code changes are required. You can also put different resources in those flavor-specific sourcesets, for different icons, labels, etc. Because you have different applicationId values, both flavors can be installed on your test devices at the same time, and both flavors can be distributed through app distribution channels like the Play store.
I have an Android app that will be distributed in two (or more) brands. All java code for each distributions is exactly same, just different assets/resources (like layouts, drawables, dimensions, etc)
What is the best way to organize the project? I am using Git for version control, and trying to keep all distributions developed as a single project. So I can switch asset/resource sets easily for different branding each time needed.
Is there a good approach for it?
One good approach would be turning your main project code in a library and then, for the other projects (brands), import that library and override the assets as you want.
Gradle Build Variants allow you to have a shared main codebase/resources and multiple variants with custom resources/code associated with each - you then can generate a separate APK for each variant. Of course, using Gradle for Android development requires you use Android Studio (which is currently in beta) as well.
As mentioned above, Gradle Build Variants is the best way to handle this. We have 11 variants (and counting) where the only difference are some configuration values. I organized the project as follows:
app/src/configurations/
flavor1/
common/res/...
release/res/...
stage/res/...
where the common directory holds configurations common to that build variant and the release and stage hold custom values for our release and staging versions (they have slightly different configurations as well).
All configurations common to all build variants live in the normal app/src/main/res folder.
Then in the app's build.grade, I have each product flavor defined:
productFlavors {
flavor1 {
applicationId = "com.example.flavor1"
}
flavor1stage {
applicationId = "com.example.flavor1stage"
}
// etc. for each build flavor
}
android.sourceSets.flavor1 {
res {
srcDirs = ['src/configurations/flavor1/release/res', 'src/configurations/flavor1/common/res', 'src/res']
}
}
android.sourceSets.flavor1stage {
res {
srcDirs = ['src/configurations/flavor1/stage/res', 'src/configurations/flavor1/common/res', 'src/res']
}
}