Development record of developer who study hard everyday.

레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Android인 게시물을 표시합니다. 모든 게시물 표시
,

How to solve error: constant expression required

 How to solve error: constant expression required


After I upgrade Gradle version upto 8.0, I encounter "constant expression required" error.

This is because AGP 8.0 doesn't allow reference to resourceID.

In my case, I use below code:

switch(view.id) {

    case sample1: 

    case sample2:

    ......

}


Solution1:

Change switch statement to if-else statement


Solution2:

Add below properties to gradle.properties file

android.nonFinalResIds = false






















Share:
Read More
,

How to solve "Can not find symbol BuildConfig" errorr

 How to solve "Can not find symbol BuildConfig" errorr



android {
    buildFeatures {
        buildConfig = true
    }
}

Put on above code into build.gradle file (app level).

This is because AGP 8.0 sets the default value of android.defaults.buildfeatures.buildconfig to false.












Share:
Read More
,

How to use safe args of navigation in android

 How to use safe args of navigation in android

android blog

1. Add dependency in build.gradle file(project level)

repositories {
google()
jcenter()
maven {url "https://maven.google.com"}
}
dependencies {

//navigation
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.8.0"

}


2. Add plugin

plugins {
id 'dagger.hilt.android.plugin'
}


3. Add argument's attribute in navigation graph which is xml file

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_navigation_graph"
app:startDestination="@id/homeFragment">

<fragment
android:id="@+id/homeFragment"
android:name="org.techtown.volleyball.fragments.HomeFragment"
android:label="HomeFragment" >

<action
android:id="@+id/action_homeFragment_to_teamNewsFragment"
app:destination="@id/teamNewsFragment" >
            <!-- add argument tag in action tag -->
<argument
android:name="newsUrl"
app:argType="string"
android:defaultValue=""
/>
</action>


</fragment>

<fragment
android:id="@+id/teamNewsFragment"
android:name="org.techtown.volleyball.fragments.TeamNewsFragment"
android:label="TeamNewsFragment">
        <!--Add argument tag in destination fragment-->
<argument
android:name="newsUrl"
app:argType="string"
android:defaultValue=""
/>

</fragment>

</navigation>


4. Navigate with argument

val action = HomeFragmentDirections.actionHomeFragmentToTeamNewsFragment(link)
findNavController().navigate(action)


5. Receive argument in destination fragment.

class TeamNewsFragment : BaseFragment<FragmentTeamNewsBinding>(){
override val layoutResID: Int
get() = R.layout.fragment_team_news
private val args : TeamNewsFragmentArgs by navArgs()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.d(TAG, "onViewCreated")


binding.webView.loadUrl(args.newsUrl)
}



}

We can receive argument with "by navArgs()" which fragment-ktx library provides.






Share:
Read More
,

Android Generic UiState

 Android Generic UiState



sealed class UiState<out T> {
data class Success<out T>(val data: T) : UiState<T>()
data class Error(val exception: Throwable) : UiState<Nothing>()
object Loading : UiState<Nothing>()
}
Share:
Read More
, ,

How to use Java and Kotlin together in android

 How to use Java and Kotlin together in android 




1. Download Kotlin plugin

download kotlin plugin


2. Add dependencies of "kotlin-gradle-plugin" in build.gradle (Project level)

dependencies {
classpath "com.android.tools.build:gradle:$agp_version"
classpath 'com.google.gms:google-services:4.3.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

// Add the Crashlytics Gradle plugin
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'

//kotlin
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'    //here
}


3. Add kotlin plugin id in build.gradle (App level)

plugins {
id 'com.android.application'
id 'kotlin-android'
}


4.  Add sourceSets for kotlin file

android {
compileSdkVersion 34
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "org.techtown.volleyball"
minSdkVersion 21
targetSdkVersion 34
multiDexEnabled true
versionCode 16
versionName "1.2.11"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
buildFeatures {
viewBinding true
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'    //here
}
}


5. Add kotlin dependencies in build.gradle (App level)

dependencies {
//kotlin
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0'

}


6. Sync gradle file and wrie kotlin code.








Share:
Read More
, ,

How to watch CPU usage using adb in android

 How to watch CPU usage using adb in android


1. Show CPU usage respectively in apps.

adb shell top -n 1 -s cpu


->details about top options

-m num : Maximum number of processes to display

-n num : Updates to show before exiting

-d num : Seconds to wait between updates

-s col : Column to sort by (cpu, vss, rss, thr)

-H : Show threads instead of prcesses

-h : Display this help screen


2. Show CPU usage in specific app

adb shell top -n 60 -d 1 | FINDSTR <package name>

This command shows CPU usages during 60 seconds by 1 second.





Share:
Read More
,

(Sloved) Android Studio APK install error: "local path doesn't exist"

 


I encounter the error "Android Studio APK install error: "local path doesn't exist" while building android source.

The solution is

  • Go to your project directory.In that directory search for .apk file....
  • You will find a apk with name of your project+debug-unalighned.apk.
  • Open .iml file insideyour project folder that contain project source directory.
  • Open and add write <option name="APK_PATH" value="/build/apk/(apk file name that was found earlier with apk extension)" /> inside <configration> <configration/> tags.
  • Now go and run your project...


Share:
Read More
, ,

How to separate string data by sentences

How to separate string data by sentences

안드로이드 블로그

When I am developing android app using Kotlin, I had to separate string data by one sentence.

There might be so many solutions, but I want short code using regular expression.

My solution is

1
2
3
4
5
6
7
8
9
10
fun main() {
    val text = "감오리는 사막이나 농촌지역에서 주로 발견되며, 땅속에 다닐 수 있는 능력이 있어 땅속에서 먹이를 찾기 위해 파묻기를 합니다. 감오리는 매우 활발한 동물로 매일 새로운 먹이를 찾으며 유혈자로 알려져 있습니다. 감오리는 오래 살아 가는 동물이며, 50년에서 70년까지 살 수 있습니다. 감오리의 수명은 종류에 따라 다르지만, 평균적으로 10년 정도입니다. 감오리는 인간과 친밀한 관계를 맺을 수 있는 동물로, 그들은 상호 의존적인 관계에서 존재합니다."
 
    val pattern = Regex("[^.!?]+[.!?]")        
    val matches = pattern.findAll(text)
 
    val sentences = matches.map { it.value.trim() }.toList()
 
    println(sentences)
}
cs


Share:
Read More
,

Useful ADB shortkeys

Useful ADB shortkeys



Android Debug Bridge(ADB) is CLI tool which communicate with device.
Android SDK Platform includes ADB package.


- Move directory

adb shell cd "path"


- Reboot device

adb reboot


- Show connected devices

adb devices


- Start server

adb start-server


- Kill server

adb kill-server


- Exectue command to specific device

adb -s "example device name" "example command"


- Connect wifi (Available Under Android 10)

adb connect "example device ip address"


- Move file to device

adb push "example local file" "example device directory"


- Take device file to local

adb pull "example device file" "example local directory"


- Get writing and reading permission

adb shell mount -o remount,rw /system


- Turn on wifi

adb shell svc wifi enable


-Turn off wifi

adb shell svc wifi disable


- Turn on mobile data

adb shell svc data enable


- Turn off mobile data

adb shell svc data disable


- Change device's date

1) adb shell "su 0 toybox date 042523592021.59"
(toybox date use MMDDhhmm[[CC]YY][.ss] format)

2) adb shell "su 0 toolbox date -s 20210425.235959"
(toolbox date use YYYYMMDD.HHmmss format)


- Symbolic link

adb shell ln -sf "example original file" "example target file"


- Show system property list

adb shell getprop


- Start activity

adb shell am start -a android.intent.action.MAIN -n "example/package/activity_name"


- Start service

adb shell am startservice -n "example/package/service_name"


- Start broadcast

adb shell am broadcast -a "example broadcast name"


- Start Broadcast extra

1) adb shell am broadcast -a $(example_action_name) -es ${example_extra_key} ${example_extra_string_value}

2) adb shell am broadcast -a $(example_action_name) -ez ${example_extra_key} $(extra_boolean_value)

3) adb shell am broadcast -a $(example_action_name) -ei $(example_extra_key) $(example_extra_int_value)


- Verify specific package's PID

adb shell pidof "example package name"


-Kill Process

adb shell am force-stop "example package name"
adb shell kill -9 PID (Available only if root)


- Show installed packages

adb shell cmd package list packages


- Find package's path

adb shell pm path "example package name"


- Screen capture

adb shell screencap -p /sdcard/Download/"example file name"
adb pull /sdcard/Download/"example file name" "local directory"


- Remove file

adb shell rm "example file"


- Remove directory

adb shell -r "example file"





Share:
Read More
,

Text-To-Speech(TTS) not worked above Android 13

 Text-To-Speech(TTS) not worked above Android 13

Android development blog


Text-to-speech(TTS) is the underlying software used by screen readers when they convert text into spoken content..

However, you can not have TTS service basically from Android 13.

You must take action following below guide.

  1. Open your device Settings .
  2. Select Accessibility and then Text-to-speech output.
  3. Choose your preferred engine, language, speech rate, and pitch.
    • The default text-to-speech engine choices vary by device. Options can include Google's Text-to-speech engine, the device manufacturer's engine, and any third-party text-to-speech engines that you've downloaded from the Google Play Store.





Share:
Read More
,

Variety of Stream in Java(자바 스트림 종류)

Variety of Stream in Java

자바 스트림 종류

Android development blog


종류IO 대상 기준자료의 종류 기준스트림의 기능 기준
FileInputStream입력 스트림바이트 단위기반 스트림
FileReader입력 스트림문자 단위기반 스트림
BufferedInputStream입력 스트림바이트 단위보조 스트림
BufferedReader입력 스트림문자 단위보조 스트림
FileOutputStream출력 스트림바이트 단위기반 스트림
FileWriter출력 스트림문자 단위기반 스트림
BufferedOutputStream출력 스트림바이트 단위보조 스트림
BufferedWriter출력 스트림문자 단위보조 스트림


✊ 문자 기반 스트림

자바의 Char 형은 2 byte를 사용하는데, 바이트 단위는 입출력 단위가 1 byte이므로 문자를 처리하기 어려웠다.

이를 보완하기 위해 등장한 것이 바로 문자 기반 스트림이다.


✋ 보조 스트림

기반 스트림은 대상에 직접 자료를 읽고 쓰는 기능의 스트림이다.

보토 스트림은 스트림의 기능을 향상시키거나 새로운 기능을 추가할 수 있는 스트림을 말한다.

오로지 보완하는 용도이기에 실제데이터를 주고 받지 않고, 데이터를 입출력하는 기능은 없다.

따라서 스트림을 먼저 생성한 후, 보조 스트림을 생성하여 사용한다.


출처: 여기











Share:
Read More
,

Get Uri from android drawable resource

 Get Uri from android drawable resource

Android development blog

1. What I have in my android drawable folder

android project drawable folder


2. Get Uri from drawable

val imgResourceId = context.resources.getIdentifier("album" + song.image.substringBefore("."), "drawable", context.packageName)
val imageUri = Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(context.resources.getResourcePackageName(imgResourceId))
.appendPath(context.resources.getResourceTypeName(imgResourceId))
.appendPath(context.resources.getResourceEntryName(imgResourceId))
.build()


3. Result

android.resource://net.twobeone.battlesinger/drawable/album00290

android.resource://net.twobeone.battlesinger/drawable/album01468

android.resource://net.twobeone.battlesinger/drawable/album49492


Share:
Read More