Android開発でJavaバージョンに関するビルドエラー

ビルドエラーの内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:7.4.2.
Required by:
project :
> No matching variant of com.android.tools.build:gradle:7.4.2 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but:
- Variant 'apiElements' capability com.android.tools.build:gradle:7.4.2 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
- Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
- Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
- Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

原因

Java のバージョンが合っていない

IDEの設定

Android Studio>Settings>Build,Execution,Deployment>Build Tools>Gradle>Gradle JKDでjava11を設定する
パスをコピーしておく

プロジェクト本体の設定

gradle.propertiesファイルにコピーしたパスを追記する。
例えば以下など

1
/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home

【Android】ZonedDateTime型のモック

ZonedDateTime型のモックの作成方法

1
2
3
import org.threeten.bp.ZonedDateTime

every { user.contractedAt } returns ZonedDateTime.parse("2022-07-13T09:00:00Z")

以上


余談だが、最初は以下のような書き方をしようとして、

1
every { user.contractedAt } returns ZonedDateTime.of(2022, 7, 13, 0, 0, 0, 0, ZoneId.of("Asia/Tokyo"));

io.mockk.MockKException: no answer found for: ZoneIdのエラーが出て詰んだ。

Privacy Policy of Poker Action Counter

Privacy Policy

reon777 built the Poker Action Counter app as a Freemium app. This SERVICE is provided by reon777 at no cost and is intended for use as is.

This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service.

If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy.

The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which are accessible at Poker Action Counter unless otherwise defined in this Privacy Policy.

Information Collection and Use

For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information. The information that I request will be retained on your device and is not collected by me in any way.

The app does use third-party services that may collect information used to identify you.

Link to the privacy policy of third-party service providers used by the app

Log Data

I want to inform you that whenever you use my Service, in a case of an error in the app I collect data and information (through third-party products) on your phone called Log Data. This Log Data may include information such as your device Internet Protocol (“IP”) address, device name, operating system version, the configuration of the app when utilizing my Service, the time and date of your use of the Service, and other statistics.

Cookies

Cookies are files with a small amount of data that are commonly used as anonymous unique identifiers. These are sent to your browser from the websites that you visit and are stored on your device’s internal memory.

This Service does not use these “cookies” explicitly. However, the app may use third-party code and libraries that use “cookies” to collect information and improve their services. You have the option to either accept or refuse these cookies and know when a cookie is being sent to your device. If you choose to refuse our cookies, you may not be able to use some portions of this Service.

Service Providers

I may employ third-party companies and individuals due to the following reasons:

  • To facilitate our Service;
  • To provide the Service on our behalf;
  • To perform Service-related services; or
  • To assist us in analyzing how our Service is used.

I want to inform users of this Service that these third parties have access to their Personal Information. The reason is to perform the tasks assigned to them on our behalf. However, they are obligated not to disclose or use the information for any other purpose.

Security

I value your trust in providing us your Personal Information, thus we are striving to use commercially acceptable means of protecting it. But remember that no method of transmission over the internet, or method of electronic storage is 100% secure and reliable, and I cannot guarantee its absolute security.

Links to Other Sites

This Service may contain links to other sites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by me. Therefore, I strongly advise you to review the Privacy Policy of these websites. I have no control over and assume no responsibility for the content, privacy policies, or practices of any third-party sites or services.

Children’s Privacy

These Services do not address anyone under the age of 13. I do not knowingly collect personally identifiable information from children under 13 years of age. In the case I discover that a child under 13 has provided me with personal information, I immediately delete this from our servers. If you are a parent or guardian and you are aware that your child has provided us with personal information, please contact me so that I will be able to do the necessary actions.

Changes to This Privacy Policy

I may update our Privacy Policy from time to time. Thus, you are advised to review this page periodically for any changes. I will notify you of any changes by posting the new Privacy Policy on this page.

This policy is effective as of 2022-06-21

Contact Us

If you have any questions or suggestions about my Privacy Policy, do not hesitate to contact me at r.saito1226+pac@gmail.com.

This privacy policy page was created at privacypolicytemplate.net and modified/generated by App Privacy Policy Generator

Androidのボタンに枠線を付ける方法

Androidのボタンに枠線を付ける方法です。
OutlinedButtonを使えば簡単なのですが、そこに気付かずにハマってしまいました。。

hoge.xml

1
2
3
4
5
<com.google.android.material.button.MaterialButton
android:id="@+id/hoge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/HogeButton" />

styles.xml

1
2
3
4
5
6
7
8
9
10
11
<style name="HogeButton" parent="Widget.Material3.Button.OutlinedButton">
<item name="android:layout_marginLeft">7dp</item>
<item name="android:layout_marginRight">7dp</item>
<item name="android:paddingTop">17dp</item>
<item name="android:paddingBottom">17dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
<item name="cornerRadius">3dp</item>
<item name="strokeWidth">2dp</item>
<item name="strokeColor">#EDA844</item>
</style>

【Android】Activity/Fragment周りのデータ受け渡し方法(Java)

はじめに

Activity/Fragment周りのデータ受け渡しの方法まとめです。
Javaです。

ActivityからActivity

送信元のActivity

1
2
3
4
Intent intent = new Intent(this, ToActivity.class);
intent.putExtra("NAME", "太郎");
startActivity(intent);
finish();

送信先のActivity(ToActivity)

1
2
3
4
5
6
7
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
String name = intent.getStringExtra("NAME");
}

ポイントはbundleが不要なところです。

ActivityからFragment

送信元のActivity

1
2
3
4
5
6
7
8
ToFragment fragment = new ToFragment();
Bundle bundle = new Bundle();
bundle.putString("NAME", "太郎");
fragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, fragment)
.commit();

送信先のFragment(ToFragment)

1
2
3
4
5
6
7
8
9
  @Override
public void onResume() {
super.onCreate();

Bundle args = getArguments();
if (args != null) {
String name = args.getString("NAME");
}
}

ポイントはbundleを使うところです。

FragmentからActivity

確認中

FragmentからFragment

確認中

Android Studioでエラー直前で止まってくれる設定方法

Android Studioで実行したアプリがエラーで落ちる時に、落ちる直前で止まってくれる方法です。

以下の画像の中のCreat breakpointのテキストをクリックすればOKです。

昔調べたけどやり方分からなくて、ログ見てたらたまたま気付いた。
もっと早くに知りたかった。。

【Android開発】ImageViewをDataBindingする方法【Kotlin】

ImageVIewにDataBindingする方法です。

まずはレイアウトファイル
hoge.xml

1
2
<ImageView
android:id="@+id/icon_menu"/>

次にFragment or Activityファイル
hoge.kt

1
binding.iconMenu.setImageResource(R.drawable.ic_action_hoge)

以上。

めちゃくちゃハマったけど最終的にシンプルに書けば良いだけだった。。
ググると複雑なやり方が出てくるのはどういうアレなんだろう。
これが正式なやり方じゃないかもだけど、とりあえず動いたので良かった。

【kotlin】FragmentでViewBindingを導入する方法【Android開発】

2022/5/26追記
公式ページが十分分かりやすいのでこれ見れば十分ですね。。
https://developer.android.com/topic/libraries/view-binding

以下、元の文章。

kotlinでFragmentのViewBindingを導入する方法です。

Fragmentのkotlinファイルで以下の通りに実装します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

class WalkThroughFirstFragment : Fragment(), View.OnClickListener {

private lateinit var binding: FragmentWalkThroughBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentWalkThroughBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.walkThroughButton1.setOnClickListener(this)
}

override fun onClick(view: View?) {

val intent = Intent(activity, SettingActivity::class.java)
startActivity(intent)
}
}

以上です。

Caching disabled for task ':app:mergeDebugNativeLibs' エラーの対処法

エラーについて

ビルド時に以下のエラーが発生しました

1
2
3
4
5
6
7
8
9
10
11
12
Caching disabled for task ':app:mergeDebugNativeLibs' because:
Build cache is disabled
Skipping task ':app:mergeDebugNativeLibs' as it is up-to-date.
Task :app:mergeDebugNativeLibs in app Finished
:app:mergeDebugNativeLibs (Thread[Execution worker for ':',5,main]) completed. Took 0.034 secs.
:app:stripDebugDebugSymbols (Thread[Execution worker for ':',5,main]) started.

> Task :app:stripDebugDebugSymbols NO-SOURCE
Task :app:stripDebugDebugSymbols in app Starting
Skipping task ':app:stripDebugDebugSymbols' as it has no source files and no previous output files.
Task :app:stripDebugDebugSymbols in app Finished
:app:stripDebugDebugSymbols (Thread[Execution worker for ':',5,main]) completed. Took 0.003 secs.

解決策

おそらくこのエラーの原因はtargetSDKバージョンが31以上であり、かつJDKバージョンが11より小さいことです。
以下の通りにすれば解決します。

  • JDKバージョンを11以上にする
  • Android Studio 2020.3(もしくはそれより最新版)をインストールする

JDKバージョンを11以上にするには、最新のAndroid Studioをダウンロードして、Android Studio → Preferences → Build Tools → GradleのGradle JDKを11以上に指定してください。

Activity間で自作ArrayListを渡す方法

Activity間で自作ArrayListを渡す方法です。
Kotlinです。
Parcelable化するところが肝です。

流れ

一覧画面から詳細画面に自作クラスのArrayListを渡す流れです。

一覧画面
MyListActivity.kt
詳細画面
MyDetailActivity.kt
渡すデータ
MyDataList:ArrayList<MyData>

送信元のActivity(MyListActivity.kt

1
2
3
4
val intent = Intent(this, MyDetailActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
intent.putParcelableArrayListExtra("MyDataList", MyDataList.list);
startActivity(intent)

送信するデータのリスト(MyDataList.kt

1
2
3
4
5
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class MyDataList(var list:ArrayList<MyData>) : Parcelable

送信する個別データ(MyData.kt

1
2
3
4
5
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class MyData(var timeStamp: String) : Parcelable

送信先のActivity(MyDetailActivity.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
import android.os.Bundle

class MyDetailActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 本来はgetParcelableExtraが正しそうだけど以下のエラーになるのでgetSerializableExtraを使う
// Not enough information to infer type variable T
// val myDataList = intent.getParcelableExtra("MyDataList") as ArrayList<MyData>
val myDataList = intent.getSerializableExtra("MyDataList") as ArrayList<*>
setContentView(view)
}
}

ちなみにMyDataがただのStringやIntgerならputStringArrayListExtraなどを使えば良いのでParcelable化する必要はないです。

今回はMyDataが自作クラスなのでputParcelableArrayListExtraを使う必要があり、これを使うためにParcelable化が必要だったということです。

以上です。

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×