I want to reuse (parts of) the AOSP DeskClock
app in my app. The sources can be found in Google’s repository but it doesn’t come as a Gradle project.
How to incorporate it into an app, for example as a library module, and build it?
Related questions and answers:
- How to download Source code from Android Google Source With Gradle files
- How to build AOSP app?
- How to compile DeskClock of android4.3 sourcecode
As described in this answer, the AOSP apps don’t come as a gradle project, but are usually compiled in a system build (where they also have access to other dependencies and resources).
So building the source as part of a gradle project is not straight-forward, some adaptions are necessary.
I successfully got a subset of DeskClock (e.g. without Timer, Stopwatch and some further UI parts) to compile with gradle with the following steps:
- Clone the AOSP code
- In a new or existing AndroidStudio/Gradle project, create a new library module (if you want to use it as part of another app, otherwise create an app project)
- Find information for
minSdk
etc. in the AOSP code’sAndroidManifest.xml
- Find information for
- Adapt the library’s
AndroidManifest.xml
according to the AOSP code’sAndroidManifest.xml
(it should not necessarily be the same) - Copy the classes/packages/resources from the AOSP code you need to your library module under
src/main/
(into the respective subdirectoriesjava
,res
etc.) - Try to compile and repeatedly add the missing source files referenced from the copied sources
- Add missing library dependencies; they can be detected by unresolved imports, e.g. those of
androix
libraries - Fix the sources for references which do not work in a normal app/library context:
- Add the following to
build.gradle.kts
if the code references theBuildConfig
class:buildFeatures { buildConfig = true }
- Change
BuildConfig.APPLICATION_ID
toBuildConfig.LIBRARY_PACKAGE_NAME
(in case you are using a library module) - (optional) Remove unused resources with the corresponding inspection
- Fix unresolved references to resource values
R.xxx
- Some are Android platform resources, change the reference to
android.R.xxx
- Some are from libraries, e.g.
androidx.preference.R.attr.dropdownPreferenceStyle
- Some are Android platform resources, change the reference to
- Change some
const val
which refer to resources but which fail to compile because they are not considered constant by removing theconst
modifier
- Add the following to
Specific for DeskClock
:
- You might have to introduce new
Build.VERSION.SDK_INT
checks inClockContract
, e.g. I needed to replace a usage ofisOOrLater
because it actually requiredO_MR1
:val isOMR1OrLater: Boolean get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1