はじめに
こんにちは!
既存のアプリにCameraXを使ってカメラを実装しようした時に設定で色々エラーが起こっていたので、そのことについてまとめます。
build.gradleでのエラー
dependencies {
・・・・
// CameraX core library using the camera2 implementation
def camerax_version = "1.2.1"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
}
上記のbuild.gradleにAndrodXを使用できるよう設定した時に起きたエラーが以下になります。
1. Dependency 'androidx.camera:camera-view:1.2.1' requires libraries and applications that
1. Dependency 'androidx.camera:camera-view:1.2.1' requires libraries and applications that
depend on it to compile against version 33 or later of the
Android APIs.
:app is currently compiled against android-31.
Also, the maximum recommended compile SDK version for Android Gradle
plugin 7.2.1 is 32.
Recommended action: Update this project's version of the Android Gradle
plugin to one that supports 33, then update this project to use
compileSdkVerion of at least 33.
Note that updating a library or application's compileSdkVersion (which
allows newer APIs to be used) can be done separately from updating
targetSdkVersion (which opts the app in to new runtime behavior) and
minSdkVersion (which determines which devices the app can be installed
on).
・・・・
なんかめっちゃ長いエラーが出たので上記はその一部です。。
大事なのは以下になります。要約するとCompileのバージョンを33以上にしてください。というエラーになります。
requires libraries and applications that
depend on it to compile against version 33 or later of the
ということこで対応としては以下になります。
修正前
compileSdk 31
修正後
compileSdk 33
上記のCompileSDKを33にあげれば解決になりました!
ちなみに少し話はそれますが、cameraxの安定版が1.2.1みたいですね!(2023/5/31現在)
ただバグ対応されたα版などもあるので必要によってバージョンを上げたりしましょう!
参考:Android デベロッパー CameraX
AndroidManifestでのエラー
次は完全に自分の凡ミスなのですが、対応時にカオスな状況になっていました。笑
下の画像がその時の画像なのですが、見た目通り上だけプレビューになり、下がなぜか1つ前のアクティビティが見えているという奇妙なレイアウトになっているということがありました。当時は「何これ。。」と思っていました。
スケールモード(※下記の参考)の問題か?とか思って変えてみましたが、変わらなかったです。(今思うと変わらないこともおかしいです。。)
他にも試行錯誤をしましたが、戻ることがありませんでした。。
参考:Android デベロッパー プレビューを実装する
原因
上で長々と書いてきましたが、結論はAndroidManifestが原因でした。
Activityを追加するごとにAndroidManifestに <activity .../> とか追加しますよね。
その時に実はコピペで対応していました。
その時に入ってはまずいものを記載してしまいました。それが以下になります。
<activity android:name="CameraActivity"
android:screenOrientation="portrait"
android:hardwareAccelerated="false"
android:theme="@style/Theme.TranslucentBackground" />
どれだかわかりましたか?
入ってはいけないものは2つあります。
android:hardwareAccelerated="false"
とandroid:theme="@style/Theme.TranslucentBackground"
になります。
hardwareAcceleratedに関してはカメラの場合はtrue
にしなければなりません。
hardwareAcceleratedがfalseだったためにスケールモードが効かなかったようです。
また重ねてthemeの設定で<item name="android:windowBackground">#00000000</item>
の透過の設定がされていましたそれにより1つ前のアクティビティが見えているという奇妙なレイアウトが表示されていたということになります。
最後に
全画面プレビューを表示する前でに時間がかかりました。
Manifestやgladleで適当にコピペをすると痛い目に合いますね(←自業自得。笑)
なのでもしレイアウトでおかしな表示になる場合はManifest含めて疑ってみるのも良いでしょう!
そしてgladleについても理解しながら進めると良いと思います。
こんなところで時間を取られないようにお互いに気をつけたいですね。
最後までご覧いただきありがとうございました。
(ふぅできた。。)