How to build or incorporate part of AOSP apps into your app?

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’s AndroidManifest.xml
  • Adapt the library’s AndroidManifest.xml according to the AOSP code’s AndroidManifest.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 subdirectories java, 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 the BuildConfig class:
      buildFeatures {
          buildConfig = true
      }
      
    • Change BuildConfig.APPLICATION_ID to BuildConfig.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
    • Change some const val which refer to resources but which fail to compile because they are not considered constant by removing the const modifier

Specific for DeskClock:

  • You might have to introduce new Build.VERSION.SDK_INT checks in ClockContract, e.g. I needed to replace a usage of isOOrLater because it actually required O_MR1:
    val isOMR1OrLater: Boolean
          get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
    

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật