2015年4月5日日曜日

IabHelper#queryInventoryAsync()

IabHelper#queryInventoryAsync(boolean, List<String>, Listener)

第一番目の引数 boolean
trueの場合、消費されたアイテム及び消費されてないアイテムの両方を取得する。
falseの場合、消費されたアイテムだけを取得する。

第二番目の引数 List<String>
取り扱っている商品群を代入する。
この値がnullの場合、取り扱っている商品のうち消費された商品だけが取得される。
この値に取り扱っている商品群を代入しておくと、その商品が消費されていない状況が、Inventory#hasDetails()等を併せ使うことにより、明確に判明する。
管理者は商品の全てを把握しているため、消費されていない商品群を把握する必要は無いが、商品を新規に登録した場合、本当に登録が完了したのかどうか確認したいものである。
売れ残っている商品を顧客に提示する目的でも使う。

第三番目の引数 QueryInventoryFinishedListener
リスナーです。

queryInventoryAsync(Listener)の場合、queryInventoryAsync(true, null, listener)として取り扱われ、消費された商品だけが取得されます。

2015年4月3日金曜日

License Verification Library (LVL) のVT

License Verification Library (LVL) のVT

ServerManagedPolicyでLVLを実装して動かしてみると、VT(Validity Timestamp、有効期間)の値(long型)がLong.MAX_VALUEになっていた。

これは、期間の定めが無く、いつまでも有効であることを意味している。
つまり、検査をするのは最初の1回だけであって、2回目以降は検査をしないということである。2回目以降は検査のためにネットへ接続はしないということであるので、インターネットへの接続を必要としないアプリであっても効果があるということである。

端末利用者にアプリをGoogle Playからダウンロードしてもらった後、ネットに接続している時に、アプリを1回起動して貰えれば検査は完了するのである。

以上がServerManagedPolicyの振る舞いであるが、常時インターネットに接続するアプリであれば、StrictPolicyを使うことを検討しても良い。もちろん、アプリを起動する度にインターネット経由で検査をするので、その分、遅くなるが。

Licensing Referenceに詳しい情報がある。
Table 1のCategoryがLicense check and result及びPolicyとして掲げられたName欄のクラスを必要に応じて適宜変更することになる。
CategoryがLibrary core, no integration neededとして掲げられたクラスは変更をする必要はない。例えば、ILicensingService.javaの冒頭には次のように書かれてある。
This file is auto-generated.  DO NOT MODIFY. Original file: aidl/ILicensingService.aidl
Table 2はGoogle Play serverが返してくる値をまとめた一覧である。基本はResponse Code欄にある値を参照すれば良い。Extras欄の意味は、「Server Response Extras」に説明が書かれている。
ExtrasはServerManagedPolicyでは実装している。
Policyとは、ServerManagedPolicy又はStrictPolicyの基底です。ServerManagedPolicyはひな形としてはGoogleが提供していますが、プログラマが適宜自己責任で改修を行い使うこととなっている、と思います。

以下は私なりの日本語訳です。

License validity period(ライセンス有効期間)

The Google Play licensing server sets a license validity period for all downloaded applications. The period expresses the interval of time over which an application's license status should be considered as unchanging and cacheable by a licensing Policy in the application.

Google Play licensing serverは、すべてのダウンロードされたアプリケーションのライセンスの有効期間を設定します。この期間は、アプリケーションでのライセンスPolicyに基づき、アプリケーションのライセンスの状態が
「不変であり」かつ「キャッシュが可能である」と考えられる時間間隔を表現しています。

The licensing server includes the validity period in its response to all license checks, appending an end-of-validity timestamp to the response as an extra under the key VT.

licensing serverは、ライセンス検査への応答の中に有効期間に関するデータを含めており、extraの「VT」というキーの中に、有効期間の最終日時のデータが有ります。

A Policy can extract the VT key value and use it to conditionally allow access to the application without rechecking the license, until the validity period expires.

Policyは、有効期間が満了するまで、ライセンスを再検査することなく、VTの値を、条件付きで、アプリケーションへのアクセスを許可するために使用することができます。

The license validity signals to a licensing Policy when it must recheck the licensing status with the licensing server.

ライセンス期間というものは、ライセンスPolicyがライセンスサーバーを使ってライセンス状態を再検査しなければならない時を示しています。

It is not intended to imply whether an application is actually licensed for use.

これは、アプリケーションが実際に使用が許可されているかどうかを意味するものではありません。

That is, when an application's license validity period expires, this does not mean that the application is no longer licensed for use 

それは、アプリケーションのライセンス有効期間が満了したとき、アプリケーションの使用が不許可になったことを意味しません。

rather, it indicates only that the Policy must recheck the licensing status with the server.

むしろ、それは、Policyはサーバを使ってライセンスの状態を再検査しなければならない、ということだけを示します。

It follows that, as long as the license validity period has not expired, it is acceptable for the Policy to cache the initial license status locally and return the cached license status instead of sending a new license check to the server.

つまり、ライセンスの有効期間が満了していない限り、Policyは、初期のライセンスのデータをローカルにキャッシュすること、そして(サーバにライセンスの検査を行わせるのではなく)キャッシュされたライセンスのデータを使うこと、が可能です。

The licensing server manages the validity period as a means of helping the application properly enforce licensing across the refund period offered by Google Play for paid applications. 

有料アプリケーションのために、アプリケーションが適切にライセンスを実施する手段として、ライセンスサーバーは有効期間を管理します。(acrossの意味不明)

It sets the validity period based on whether the application was purchased and, if so, how long ago. Specifically, the server sets a validity period as follows:

アプリケーションが購入されたかどうか、どのくらい前なのか、に基づいて有効期間を設定します。サーバが有効期間を設定するのは具体的には以下のとおりです。

●For a paid application, the server sets the initial license validity period so that the license response remains valid for as long as the application is refundable. A licensing Policy in the application may cache the result of the initial license check and does not need to recheck the license until the validity period has expired.

●有料アプリのために、サーバは初期のライセンスの有効期間を設定します。それにより、アプリが返金可能である限りは、ライセンスのレスポンスは有効の状態を維持し続けます。アプリケーションにおけるライセンスPolicyは、初期のライセンスチェックの結果をキャッシュすることができるし、有効期間が満了するまで、ライセンスを再検査する必要はありません。

●When an application is no longer refundable, the server sets a longer validity period — typically a number of days.

●アプリケーションがもはや払い戻しできない場合は、サーバは長い有効期間 - 通常は数日 を設定します。

●For a free application, the server sets the validity period to a very high value (long.MAX_VALUE). This ensures that, provided the Policy has cached the validity timestamp locally, it will not need to recheck the license status of the application in the future.

●無料のアプリケーションのために、サーバーは非常に高い値(Long.MAX_VALUE)を有効期間として設定します。これは、
Policyがローカルに有効期間に関するデータをキャッシュしてしまうことを保証します。今後ずっとアプリケーションのライセンスステータスを再検査する必要はありません。


2015年4月1日水曜日

License Verification Library (LVL)

License Verification Library (LVL)

App Licensing
Android LVL を使う - Securing Android LVL Applications -
Android Application Licensing
LVL (License Verification Library) を使ってみた
Google Play の Licensing サービスを使う
LVL(License Verification Library)の導入
Anti AntiLVL - AntiLVL への簡単な対抗方法について考えてみる
Android 5.0 で LVL が正常動作しない問題の対処

ServerManagedPolicyは、飛行機に乗っている時のように、オフラインで端末を使っている時でも取り扱えるような、規約を定めます。
StrictPolicyは、サーバーとのやり取りをする時だけしか動作しないような規約を定めます。オフラインでは動作しません。
どのように使うのかを、プログラマーは、適宜判断をして、LVLのソースコードを書き換えて使用します。

Licensing Overviewには、次のとおり書かれています。
Google Play considers a user to be licensed if the user is a recorded purchaser of the application.
また、Requirements and Limitationsには、次のとおり書かれています。
You can implement licensing controls for a free app, but only if you're using the service to provide APK expansion files.
LVLは有料アプリで使うのですね。

2015年3月29日日曜日

proguard dontwarn

proguard dontwarn

下記の趣旨のエラーが出た。
Warning: com.google.common.cache.Striped64: can't find referenced class sun.misc.Unsafe
Warning: there were 21 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes, you can suppress
         the warnings with '-dontwarn' options.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
:proguardRelease FAILED

対処方法
次の1行だけを記述したテキストファイルを適当なファイル名(ここでは'my_proguard-project.txt')で、(ルートでは無く)モジュール側のフォルダーに作成する。
-dontwarn sun.misc.Unsafe

(ルートでは無く)モジュール側のbuild.gradleファイルに次のように書く。
    buildTypes{
        release{
            minifyEnabled true//プロガードを実行する
            proguardFile getDefaultProguardFile('proguard-android.txt')
            proguardFile file('my_proguard-project.txt')
        }
getDefaultProguardFile()は${sdk.dir}/tools/proguard/proguard-android.txtを自動的に読み込む。
my_proguard-project.txtは、このプロジェクトのモジュール固有のproguard設定です。
ちなみに、デフォルトでは、minifyEnabledはfalseです。

(参考)Android Tools Project Site

Gradle Plugin User Guide

Gradle Plugin User Guide

取り敢えず日本語版を読んで概要を知る
第34回 バージョン管理 ─プロジェクト管理ファイルについて[中編]
第 4 回・新 IDE : Android Studio を使ってみよう!-Part2-

Gradle Tutorial : Part 6 : Android Studio + Gradle 画面の画像がある。
Gradle Plugin User Guide 本丸。ここに詳細がある。

DSL == Domain Specific Language。

Android Studioでいわゆる「カス」が溜まる現象について

Android Studioでいわゆる「カス」が溜まる現象について

Android Studioで矛盾するエラーメッセージが出る場合があります。

私の事例の場合のエラーは次のような趣旨でした。
Information:Gradle tasks [clean, : compileDebugSources]
Error:orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Information:Gradle tasks [clean, generateDebugSources, generateDebugAndroidTestSources]
Error:orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

上記エラーが出たので、AndroidManifest.xmlファイルにおいて該当文言を削除しました。しかし、引き続き上記エラーが発生し続けたのです。
もはやプロジェクトファイルのどこにも上記エラー文言が存在しないのにも関わらずです。
多くの時間をかけて、やっと解決しました。

解決方法は次のとおりです。
File->Settings...->Compiler (Gradle-based Android Projects)
「Use in-process build」欄のチェックを外す。
「Configure on demand」欄のチェックを外す。
その上で、Clean等を行います。すると、いわゆる「カス」が除かれます。
そうすれば、再度上記チェックをonにしても、エラーは発生しません。

チェックを入れると、コンパイルは速くなるようです。
でも、わけのわからんエラーが発生し、その問題解決のために、コンパイルが早くなった時間以上の、多大な時間を浪費してしまいました。むむむ!

ちなみに、上記処理により、.idea/workspace.xmlの内容は自動的に書き換わります。.idea/workspace.xmlを手動で書き換えても意味ないです。

とにかく、わけわからんエラーが出たら、上記チェックを外す。

プロジェクトの構成を変更したら、自動的に、チェックを外してコンパイルするようにして欲しいな。

その後、上記対応を行っても同様のエラーが発生した。Proguardのtxtファイルが存在しなかったので、作成してやると、上記エラーが無くなった。

2015年3月23日月曜日

stacktrace option, debug option

stacktrace option, debug optionの設定方法

* Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.

上記メッセージが出たら、下記を参照する。
How to Add Stacktrace or debug Option when Building Android Studio Project

Android Studio->File->Settings->Compiler(Gradle-based Android Project)->Command-line Options:
ここの欄に「--stacktrace --debug」と書く。