ApiDemosアプリのダウンロードとビルド方法


【最終更新日】2023-04-07

ApiDemosアプリはAndroid Developersのサンプルとしては提供されておらず、別途 Android GoogleSourceリポジトリからダウンロードする必要が有ります。

※もともとEclipseでビルドすることを想定している(README.txt参照)ためそのままでは現在の AndroidStudioでAPKをビルドすることはできません。

【ダウンロード先】ApiDemos

https://android.googlesource.com/platform/development/+/master/samples/ApiDemos
【ApiDemosのTarアーカイブをダウンロード】
1.解凍したソース

(1) Android.bp ※ビルドに必要な情報が書かれています

package {
    // See: http://go/android-license-faq
    default_applicable_licenses: ["Android-Apache-2.0"],
}

android_test {
    name: "ApiDemos",
    // Only compile source java files in this apk.
    srcs: [
        "src/**/*.java",
        "src/com/example/android/apis/app/IRemoteService.aidl",
        "src/com/example/android/apis/app/IRemoteServiceCallback.aidl",
        "src/com/example/android/apis/app/ISecondary.aidl",
    ],
    static_libs: [
        "androidx-constraintlayout_constraintlayout",
        "androidx.legacy_legacy-support-v4",
    ],
    sdk_version: "current",
    dex_preopt: {
        enabled: false,
    },
    test_suites: ["device-tests"],
}
2.Android Studioで空のサンプルプロジェクトを作成する
プロジェクト名
ApiDemos
アプリケーシID(applicationId) ※ダウンロードしたApiDemosのパッケージ名に一致させる
com.example.android.apis
SDKバージョン(targetSdk) ※最新
31
コンパイルオプション(compileOptions)
JavaVersion.VERSION_1_8
3.設定ファイルをApiDemos用に書き換える
(1) build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}
(2) gradle.properties
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.enableJetifier=true
(3) local.properties ※開発環境 Ubuntu
sdk.dir=/home/yourname/Android/Sdk
(4) settings.gradle
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "ApiDemos"
include ':app'
(5) app/build.gradle: このファイルを編集 aidl.srcDirs と dependencies
plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.android.apis'
    compileSdk 31

    defaultConfig {
        applicationId "com.example.android.apis"
        minSdk 31
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    // AIDLファイルのパスを指定する
    sourceSets {
        main {
            aidl.srcDirs = ['src/main/aidl']
        }
    }
}

dependencies {
    // androidx.constraintlayout, androidx.legacyの2つに置き換える 
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
(6) app/src/main配下のディレクトリ構成
[src/com/example/android/apis/app配下の全てのAIDLファイル(*.aidl)を下記のパッケージにコピーする]
aidl/com/example/android/apis/app/ 
   IRemoteService.aidl
   IRemoteServiceCallback.aidl
   ISecondary.aidl
asstes/
   [解凍したソースのasstesを全てコピーする]
java/
   [プロジェクトのソースを全て削除し、解凍したソースのsrc配下を全てコピーする]
   com/example/android/
     apis/
     mmslib
res/
   [プロジェクトのリソースを全て削除し、解凍したソースのres配下を全てコピーする]
  anim/
  animator/
  drawable/
  drawable-hdpi/
  layout/
  ...[省略]...
  menu/
  ...[省略]...
  values/
  values-hdpi/
  ...[省略]...
  values-xlarge/
  xml/   
 
tests/
AndroidManifest.xml
4.DEBUGビルドする
上記の設定とコピーしたソースでDEBUGビルドすると大量のエラーログが出力されビルドに失敗します。
【ApiDemosビルドエラー】
DEBUGビルドしたときに出力されたエラーログの抜粋
Executing tasks: [:app:assembleDebug] in project /mnt/data-2/ApiDemos

>Task :app:createDebugVariantModel
>Task :app:preBuild UP-TO-DATE
>Task :app:preDebugBuild UP-TO-DATE
>Task :app:mergeDebugNativeDebugMetadata NO-SOURCE
>Task :app:compileDebugRenderscript NO-SOURCE
>Task :app:generateDebugBuildConfig
>Task :app:javaPreCompileDebug
>Task :app:generateDebugResValues
>Task :app:checkDebugAarMetadata
>Task :app:mapDebugSourceSetPaths
>Task :app:generateDebugResources
>Task :app:compileDebugAidl
>Task :app:createDebugCompatibleScreenManifests
>Task :app:extractDeepLinksDebug
>Task :app:mergeDebugResources

>Task :app:processDebugMainManifest FAILED
package="com.example.android.apis" found in source AndroidManifest.xml: 
   /mnt/data-2/ApiDemos/app/src/main/AndroidManifest.xml.
...Warning省略...
...ここからError (見やすくするため改行をいれています) 363箇所 ...
/mnt/data-2/ApiDemos/app/src/main/AndroidManifest.xml:70:9-76:20 Error:
  android:exported needs to be explicitly specified for element 
   . 
Apps targeting Android 12 and higher are required to specify an explicit value for 
 `android:exported` when the corresponding component has an intent filter defined. 
See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
【原因】Android12以降のバージョンではActivityの属性として`android:export`が必須であることを示しています。
Apps targeting Android 12 and higher are required to specify an explicit value for
`android:exported` when the corresponding component has an intent filter defined.
【対応方法】Activityの属性として`android:exported="true"`を追記します ※364箇所エディタで一括置換
<activity android:name="ApiDemos">
<activity android:exported="true" android:name="ApiDemos">
再度ビルドするとエラーがなくなりDEBUG用のAPKが出力されます。
【ApiDemosビルド成功】
5.エミュレータでApiDemosアプリを実行する
【Views -> Date Widgets -> 1. Dialog を押下】
メニューページへ
戻る