Skip to content

Commit d693d48

Browse files
Support non-final resource IDs (#29)
1 parent b74c395 commit d693d48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+986
-221
lines changed

CHANGELOG.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Change Log
22
==========
33

4+
Version 1.2.0 *(2021-03-14)*
5+
----------------------------
6+
* New: Support for non-final resource IDs. See [README.md](https://github.com/MatthiasRobbers/shortbread##non-final-resource-ids)
7+
for detailed usage instructions.
8+
* Update: `androidx.annotation:annotation` to `1.1.0`
9+
* Update: Android Gradle plugin to `4.1.2`
10+
* Migrated publishing from JCenter to Maven Central
11+
12+
413
Version 1.1.0 *(2020-07-21)*
514
-----------------------------
615
* New: Support for incremental annotation processing
@@ -13,7 +22,7 @@ Version 1.1.0 *(2020-07-21)*
1322

1423
Version 1.0.2 *(2017-09-24)*
1524
-----------------------------
16-
* Fix: Annotated methods are called before `onCreate()` (#13)
25+
* Fix: Annotated methods are called before `onCreate()` ([#13](https://github.com/MatthiasRobbers/shortbread/issues/13))
1726
* Update: Support annotations library to `26.0.2`. This requires the new Google Maven Repository:
1827

1928
```groovy
@@ -29,13 +38,11 @@ Version 1.0.2 *(2017-09-24)*
2938

3039
Version 1.0.1 *(2017-03-04)*
3140
-----------------------------
32-
3341
* Fix: `Shortbread.create(context)` can now also be called if there are no `@Shortcut` annotations in the code, which before produced a crash. Previously created shortcuts are now removed.
3442
* Fix: Internal `NullPointerException` when an activity containing method shortcuts is not launched via a method shortcut
3543
* Add Javadoc for the public API
3644

3745

38-
Version 10.2.0 *(2017-02-11)*
46+
Version 1.0.0 *(2017-02-11)*
3947
-----------------------------
40-
4148
Initial release

README.md

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,72 @@ you want the shortcut to call.
99

1010
The four shortcuts above are produced by the following code:
1111

12-
```java
12+
```kotlin
1313
@Shortcut(id = "movies", icon = R.drawable.ic_shortcut_movies, shortLabel = "Movies")
14-
public class MoviesActivity extends Activity {
14+
class MoviesActivity : Activity() {
1515

1616
// ...
1717

1818
@Shortcut(id = "add_movie", icon = R.drawable.ic_shortcut_add, shortLabel = "Add movie")
19-
public void addMovie() {
20-
// code to add movie, could show an AddMovieDialogFragment for example
19+
fun addMovie() {
20+
// could show an AddMovieDialogFragment for example
2121
}
2222
}
2323
```
2424

25-
```java
25+
```kotlin
2626
@Shortcut(id = "books", icon = R.drawable.ic_shortcut_books, shortLabel = "Books")
27-
public class BooksActivity extends Activity {
27+
class BooksActivity : Activity() {
2828

2929
// ...
3030

3131
@Shortcut(id = "favorite_books", icon = R.drawable.ic_shortcut_favorite, shortLabel = "Favorite books")
32-
public void showFavoriteBooks() {
33-
// code to display favorite books, could show a FavoriteBooksFragment for example
32+
fun showFavoriteBooks() {
33+
// could show a FavoriteBooksFragment for example
3434
}
3535
}
3636
```
3737

3838
To display the shortcuts, call `Shortbread.create(Context context)` as early as possible in the app, for
3939
example in `onCreate` of a custom `Application`.
4040

41-
```java
42-
public class App extends Application {
41+
```kotlin
42+
class App : Application() {
4343

44-
@Override
45-
public void onCreate() {
46-
super.onCreate();
44+
override fun onCreate() {
45+
super.onCreate()
4746

48-
Shortbread.create(this);
47+
Shortbread.create(this)
4948
}
5049
}
5150
```
5251

5352
Shortcuts can be customized with attributes, just like using the framework API.
5453

54+
<details open>
55+
<summary>Kotlin</summary>
56+
57+
```kotlin
58+
@Shortcut(
59+
id = "books",
60+
icon = R.drawable.ic_shortcut_books,
61+
shortLabel = "Books",
62+
shortLabelRes = R.string.shortcut_books_short_label,
63+
longLabel = "List of books",
64+
longLabelRes = R.string.shortcut_books_long_label,
65+
rank = 2, // order in list, relative to other shortcuts
66+
disabledMessage = "No books are available",
67+
disabledMessageRes = R.string.shortcut_books_disabled_message,
68+
enabled = true, // default
69+
backStack = [MainActivity::class, MainActivity::class],
70+
activity = MainActivity::class, // the launcher activity to which the shortcut should be attached
71+
action = "shortcut_books" // intent action to identify the shortcut from the launched activity
72+
)
73+
```
74+
</details>
75+
<details>
76+
<summary>Java</summary>
77+
5578
```java
5679
@Shortcut(
5780
id = "books",
@@ -68,31 +91,83 @@ Shortcuts can be customized with attributes, just like using the framework API.
6891
activity = MainActivity.class, // the launcher activity to which the shortcut should be attached
6992
action = "shortcut_books" // intent action to identify the shortcut from the launched activity
7093
)
71-
public class BooksActivity extends Activity { /*...*/ }
7294
```
95+
</details>
96+
7397
Download
7498
--------
7599

76-
### Java
100+
Shortbread is available on `mavenCentral()`.
101+
102+
<details open>
103+
<summary>Kotlin</summary>
104+
105+
```groovy
106+
apply plugin: 'kotlin-kapt'
107+
108+
dependencies {
109+
implementation 'com.github.matthiasrobbers:shortbread:1.2.0'
110+
kapt 'com.github.matthiasrobbers:shortbread-compiler:1.2.0'
111+
}
112+
```
113+
</details>
114+
<details>
115+
<summary>Java</summary>
77116

78117
```groovy
79118
dependencies {
80-
implementation 'com.github.matthiasrobbers:shortbread:1.1.0'
81-
annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.1.0'
119+
implementation 'com.github.matthiasrobbers:shortbread:1.2.0'
120+
annotationProcessor 'com.github.matthiasrobbers:shortbread-compiler:1.2.0'
82121
}
83122
```
123+
</details>
84124

85-
### Kotlin
125+
Non-final resource IDs
126+
----------------------
127+
If you are using resource IDs in `@Shortcut` attributes auch as `shortLabelRes`, which is recommended, you may see this
128+
warning in Android Studio:
129+
> Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them as annotation attributes.
86130
87-
```groovy
88-
apply plugin: 'kotlin-kapt'
131+
If the annotation is located inside a library, the project won't even compile. To overcome this, add the Shortbread
132+
Gradle plugin and apply it to your modules:
89133

90-
dependencies {
91-
implementation 'com.github.matthiasrobbers:shortbread:1.1.0'
92-
kapt 'com.github.matthiasrobbers:shortbread-compiler:1.1.0'
134+
```groovy
135+
buildscript {
136+
repositories {
137+
mavenCentral()
138+
}
139+
dependencies {
140+
classpath 'com.github.matthiasrobbers:shortbread-gradle-plugin:1.2.0'
141+
}
93142
}
94143
```
95144

145+
```groovy
146+
apply plugin: 'com.github.matthiasrobbers.shortbread'
147+
```
148+
149+
Now make sure you use `R2` instead of `R` inside all `@Shortcut` annotations. If you are using `mipmap` as shortcut icon
150+
resource type, switch to `drawable` by simply moving the resources from the `mipmap-` folders to the corresponding
151+
`drawable-` folders.
152+
153+
```kotlin
154+
@Shortcut(icon = R2.drawable.ic_shortcut_movies, shortLabelRes = R2.string.label_movies)
155+
```
156+
157+
The plugin uses the [Butter Knife][2] Gradle plugin to generate the `R2` class with final values. Referencing them does
158+
not make the warning disappear (you can suppress it with `@SuppressLint("NonConstantResourceId")`), but most likely will
159+
be the only way to use resource IDs in annotations in the future.
160+
<details>
161+
<summary>Alternative to Gradle plugin</summary>
162+
163+
If for you can't (or don't want to) use the plugin, you can use the additional deprecated string attributes like
164+
`iconResName`, `shortLabelResName` and so on.
165+
166+
```kotlin
167+
@Shortcut(iconResName = "ic_shortcut_movies", shortLabelResName = "label_movies")
168+
```
169+
</details>
170+
96171
License
97172
-------
98173

@@ -113,3 +188,4 @@ License
113188

114189

115190
[1]: https://developer.android.com/guide/topics/ui/shortcuts.html
191+
[2]: https://github.com/JakeWharton/butterknife

build.gradle

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
apply plugin: "com.github.ben-manes.versions"
2+
13
buildscript {
24
repositories {
35
jcenter()
46
google()
57
}
68
dependencies {
7-
classpath 'com.android.tools.build:gradle:3.6.4'
8-
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.12.0'
9-
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
9+
classpath 'com.android.tools.build:gradle:4.1.2'
10+
classpath 'com.github.ben-manes:gradle-versions-plugin:0.36.0'
11+
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.14.2'
1012
}
1113
}
1214

@@ -18,12 +20,11 @@ subprojects {
1820
}
1921

2022
ext {
21-
compileSdkVersion = 29
22-
buildToolsVersion = '29.0.3'
23+
compileSdkVersion = 30
24+
buildToolsVersion = '30.0.3'
2325
minSdkVersion = 14
2426
sourceCompatibilityVersion = JavaVersion.VERSION_1_8
2527
targetCompatibilityVersion = JavaVersion.VERSION_1_8
26-
androidxAnnotationVersion = '1.1.0'
2728
autoServiceVersion = '1.0-rc7'
2829
incapVersion = '0.3'
2930
}

gradle.properties

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx1536m
22
android.useAndroidX=true
33

44
GROUP=com.github.matthiasrobbers
5-
VERSION_NAME=1.1.0
5+
VERSION_NAME=1.2.0
66

77
POM_URL=https://github.com/matthiasrobbers/shortbread
88
POM_SCM_URL=https://github.com/matthiasrobbers/shortbread
@@ -16,5 +16,3 @@ POM_LICENCE_DIST=repo
1616
POM_DEVELOPER_ID=matthiasrobbers
1717
POM_DEVELOPER_NAME=Matthias Robbers
1818
POM_DEVELOPER_URL=https://github.com/matthiasrobbers/
19-
20-
RELEASE_SIGNING_ENABLED=false

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip

publish.gradle

Lines changed: 0 additions & 12 deletions
This file was deleted.

sample/build.gradle

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
google()
5+
mavenLocal()
6+
}
7+
8+
dependencies {
9+
classpath "com.github.matthiasrobbers:shortbread-gradle-plugin:${VERSION_NAME}"
10+
}
11+
}
12+
113
apply plugin: 'com.android.application'
14+
apply plugin: 'com.github.matthiasrobbers.shortbread'
215

316
android {
417
compileSdkVersion project.compileSdkVersion
@@ -7,7 +20,7 @@ android {
720
defaultConfig {
821
applicationId "com.example.shortbread"
922
minSdkVersion project.minSdkVersion
10-
targetSdkVersion 29
23+
targetSdkVersion 30
1124
versionCode 1
1225
versionName "1.0"
1326
}

sample/proguard-rules.pro

Lines changed: 0 additions & 17 deletions
This file was deleted.

sample/src/main/java/com/example/shortbread/books/BooksActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import android.widget.TextView;
66

77
import com.example.shortbread.R;
8+
import com.example.shortbread.R2;
89

910
import shortbread.Shortcut;
1011

11-
@Shortcut(id = "books", icon = R.drawable.ic_shortcut_books, shortLabelRes = R.string.label_books, rank = 1)
12+
@Shortcut(id = "books", icon = R2.drawable.ic_shortcut_books, shortLabelRes = R2.string.label_books, rank = 1)
1213
public class BooksActivity extends Activity {
1314

1415
@Override
@@ -17,8 +18,8 @@ protected void onCreate(Bundle savedInstanceState) {
1718
setContentView(R.layout.activity_books);
1819
}
1920

20-
@Shortcut(id = "favorite_books", icon = R.drawable.ic_shortcut_favorite, shortLabel = "Favorite books", rank = 2,
21-
disabledMessage = "You have no favorite books")
21+
@Shortcut(id = "favorite_books", icon = R2.drawable.ic_shortcut_favorite, shortLabelRes = R2.string.label_books_method,
22+
rank = 2, disabledMessage = "You have no favorite books")
2223
public void showFavoriteBooks() {
2324
((TextView) findViewById(R.id.text)).setText("Favorite books");
2425
}

sample/src/main/java/com/example/shortbread/movies/MoviesActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
import com.example.shortbread.MainActivity;
88
import com.example.shortbread.R;
9+
import com.example.shortbread.R2;
910
import com.example.shortbread.books.BooksActivity;
1011

1112
import shortbread.Shortcut;
1213

13-
@Shortcut(id = "movies", action = "movie_shortcut", icon = R.drawable.ic_shortcut_movies, rank = 3,
14+
@Shortcut(id = "movies", action = "movie_shortcut", icon = R2.drawable.ic_shortcut_movies, rank = 3,
1415
backStack = {MainActivity.class, BooksActivity.class})
1516
public class MoviesActivity extends Activity {
1617

@@ -20,7 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
2021
setContentView(R.layout.activity_movies);
2122
}
2223

23-
@Shortcut(id = "add_movie", icon = R.drawable.ic_shortcut_add, shortLabel = "Add movie", rank = 4)
24+
@Shortcut(id = "add_movie", icon = R2.drawable.ic_shortcut_add, shortLabelRes = R2.string.label_movies_method, rank = 4)
2425
public void addMovie() {
2526
((TextView) findViewById(R.id.text)).setText("Add movie");
2627
}

0 commit comments

Comments
 (0)