Category: Android 開發

Android marshmallow Runtime Permissions

Dangerous permissions need to be granted explicitly

1
2
3
4
5
6
7
8
9
Activity.this.requestPermissions(new String[]
{
Manifest.permission.RECORD_AUDIO
,Manifest.permission.READ_CONTACTS
,Manifest.permission.WRITE_CONTACTS
,Manifest.permission.READ_EXTERNAL_STORAGE
,Manifest.permission.WRITE_EXTERNAL_STORAGE
}
,REQUEST_PERMISSION);

Table 1. Dangerous permissions and permission groups.

Permission Group Permissions
CALENDAR
CAMERA
CONTACTS
LOCATION
MICROPHONE
PHONE
SENSORS
SMS
STORAGE

Defining and Enforcing Permissions

Tags :

[Q&A]How to specify multiple source directory for Android library project

ant.properties

1
source.dir=src:src1/subpath:src2

tools/ant/build.xml
comment or remove the last fileset tag in jar tag

1
2
3
4
5
6
<jar destfile="${out.library.jar.file}">                        
       <fileset dir="${out.classes.absolute.dir}"
            includes="**/*.class"
            excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/Manifest.class ${project.app.package.path}/Manifest$*.class ${project.app.package.path}/BuildConfig.class"/>
       <!--fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" /-->
    </jar>
Tags :

[Q&A] how to use android annotations in gradle

build.gradle

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
buildscript {
    repositories {
        mavenCentral()
    }
 
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}
 
repositories {
    mavenCentral()
    mavenLocal()
}
 
apply plugin: 'android-apt'
 
def AAVersion = '3.3.2'
 
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}
 
dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
Tags : ,

[Q&A] How to solve java.lang.OutOfMemoryError: GC overhead limit exceeded error in android studio

in build.gradle

1
2
3
4
5
6
android {
...
   dexOptions {
      javaMaxHeapSize "2g"
   }
}
Tags :

[Android][Q&A] Proguard retrace missing line numbers (Unknown Source)

add this 2 lines in proguard-project.txt or proguard.txt

1
2
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Tags :

[Q&A]How to center icon using customView in actionbar

1
2
3
4
5
6
7
ActionBar.LayoutParams layoutParams = 
new ActionBar.LayoutParams(
   ActionBar.LayoutParams.MATCH_PARENT,
   ActionBar.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_HORIZONTAL;
View customView = getLayoutInflater().inflate(R.layout.actionbar_custom_view, null);
actionBar.setCustomView(customView, layoutParams);
Tags :

[Q&A] tabs appearing ABOVE actionbar with custom view in NAVIGATION_MODE_TABS

1
2
3
4
5
6
actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));
View homeIcon = findViewById(
   Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
   android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setLayoutParams(new LinearLayout.LayoutParams(0, 0));
((View) homeIcon).setVisibility(View.GONE);
Tags :

[Q&A] How to solve “Gradle project sync failed. Please fix your project and try again”

1
File -> Invalidate caches / Restart
Tags : ,

[Q&A] android Build.CPU_ABI returns wrong cpu type

there is an experience,
in certain android x86 phone, with certain android os version,

a bundle APK includes

/libs/armeabi/a.so
/libs/armeabi-v7a/a.so
/libs/armeabi-v7a/b.so
/libs/x86/a.so
/libs/x86/b.so

String cpu = Build.CPU_ABI; // armeabi-v7a is returned which is wrong, but in other x86 phones, “x86″ is returned correctly

however, if /libs/armeabi/b.so is added back, or /libs/armeabi is removed

String cpu = Build.CPU_ABI; // x86 is returned correctly

I have not figured out the cause yet,
just keep in mind, there must be same amount shared libraries in each arch folders.

Tags : ,

Handy adb commends for Android

list devices

1
adb devices

install & uninstall apk

1
2
adb install -r ~/Download/myapk.apk
adb uninstall com.ynjan.myapp

start an activity

1
2
adb shell am start com.ynjan.myapp/.MainActivity
adb shell am start com.ynjan.myapp/com.ynjan.myapp.MainActivity

list installed packages

1
adb shell pm list packages -f

reboot

1
adb reboot

enter devices’s shell

1
adb shell

make input in shell

1
2
3
4
5
6
7
usage: input ... 
   input text <string> [delay]
   input keyevent <key code number or name>
   input [touchscreen|touchpad] tap <x> <y>
   input [touchscreen|touchpad] swipe <x1> <y1> <x2> <y2>
   input trackball press
   input trackball roll <dx> <dy>

for example

1
2
3
4
#unlock screen
input keyevent 82
#device power on/off
input keyevent 26

android key codes

clear application data

1
adb shell pm clear com.ynjan.myapp

Logging

1
2
3
4
5
6
7
8
9
# log with tag
adb logcat -s TAG_NAME_1
# log with priority
adb logcat "*:PRIORITY"
adb logcat -s TAG_NAME:PRIORITY
# log with keyword (regex)
adb logcat | grep "SOME_KEYWORDS"
# clear log
adb logcat -c
Tags : , ,