‘Xamarin メモ(実機デバッグ)

実機デバッグ

Xamarinのエミュレータ重いですよね.あとandroidSDKに60GB以上使われて嫌気がしたので実機デバッグできるようにしました.

今回実機デバッグ用のデバイスはnexus5を使っています.

開発者オプション

まずスマホの開発者向けオプションをオンにします.

何も設定していない場合は開発者向けオプションが表示されません.

f:id:mo121_7:20170402181216p:plain

そこで設定 -> 端末情報のビルド番号を7回タップします.   f:id:mo121_7:20170402181447p:plain
これでデベロッパーですね!
f:id:mo121_7:20170402181531p:plain
先ほどはなかった開発者向けオプションが表示されるようになりました.
あとはUSBデバッグをONにするだけ
f:id:mo121_7:20170402182113p:plain

Andorid SDK Manager

Andorid SDK Paltform-toolとGoogle USB Driverをインストールしましょう.
f:id:mo121_7:20170402182623p:plain

設定できているかデバイスマネジャーかコマンドから確認できます.

f:id:mo121_7:20170402182820p:plain
f:id:mo121_7:20170402182936p:plain

ちなみにadbコマンドがないと言われた場合は環境変数のpathに以下を追加してください. f:id:mo121_7:20170402183130p:plain  

バイスが認識されない場合このブログが分かりやすいです.

biz-crew.com

動かしてみる

実行する際に自分の端末を選択してください.

f:id:mo121_7:20170402183552p:plain 今回の場合だとNexus 5ですね
少し待つとデバイス上にビルドしたアプリが起動します f:id:mo121_7:20170402183718p:plain

Xamarin メモ

android emulatorが起動しない

  • エラー内容

Could not find android.jar for API Level 25. This means the Android SDK platform for API Level 25 is not installed. Either install it in the Android SDK Manager (Tools > Open Android SDK Manager…), or change your Xamarin.Android project to target an API version that is installed. (C:\Program Files (x86)\Android\android-sdk\platforms\android-25\android.jar missing.) App2.Android

  • 原因
    インストールされていないAndorid APIで起動しようとしたから

*解決方法 ツール -> Andrioid -> Andriod SDKマネージャーを起動
f:id:mo121_7:20170402123754p:plain

f:id:mo121_7:20170402124003p:plain
インストールしましょう

最近のこと

学生から社畜になりました

(今更ですが)社畜になってから半年が経ちました。一応エンジニアなんですが、プログラミングできてません。。。

会社では主にテストしかしてないです。仕事から帰ってきてプログラミングするのって難しいですよね(言い訳)

そんな訳でもともとない自分の技術力の低下が著しいです。

久々にgit使おうとしてもコマンド忘れてたりするので、もっと勉強しないとやばいなという思いなので、インプットしながらアウトプットを忘れないようにしようと思います。

Visual Studio でXamarin.Formsを編集

Xamarin始めました

去年からやるやる詐欺をしていたXamarinですがやった環境も整い始めてみました

よしXamlを書くぞ

え、ちょっとまって!

f:id:mo121_7:20161009152344p:plain

intellisenceさん仕事してくださいよ!

こんな時は@ytabuchiさんのブログを見よう

見つからない。。。

もういいや こんなときはググろう f:id:mo121_7:20161009154502p:plain お、インストールできそう f:id:mo121_7:20161009154633p:plain

macでASP.NET

前回はmacでコンソールアプリケーションの動か方でしたが今回はASP.NETの実行の仕方について紹介します.

yeomanで雛形の作成

ASP.NETの雛形をyeomanで作成します.

yo aspnet

ASP.NETなのでWeb Applicationを選択します. f:id:mo121_7:20160207181554p:plain

次にアプリケーション名を聞かれます. 今回はWebAppとします. f:id:mo121_7:20160207181830p:plain

実行まで

実行までは

cd WebApp
dnu restore
dnx web

を実行し,ローカルホスト(http://localhost:5000)にアクセスすると動きます. f:id:mo121_7:20160207191130p:plain

visual studio codeでc#コンソールアプリケーションの動かし方

今回はc#macで動かしたいと思います。 c#を動かすと聞けばvisual studio(VS)を思いつくと思いますがmacなんでVSさんは動きません! そこでVSさんの代わりにMSが提供しているVS codeでやってみたいと思います。

下準備

macにはc#コンパイラなどは始めからは入っていません。そこで.NETの互換のmonoをbrewでインストールします。

brew install mono

コンソールアプリケーションの雛形をつくるyeomanをインストール

npm install yo bower grunt-cli -g

DNVMのインストール*1

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

dnvm upgrade -r coreclr
dnvm upgrade -r mono

ターミナルで雛形作成

下準備でインストールしたyeomanでコンソールアプリケーションの雛形を作っていきます。

yo aspnet

するとさまざまな選択肢がでてきますが2番目のConsole Application を選択します。

f:id:mo121_7:20160104214806p:plain

選択するとアプリケーションの名前を聞かれます。

f:id:mo121_7:20160104215215p:plain

今回はデフォルトの名前でいきます。 ここで次のコマンドを実行しろと言われたので実行しときます。

cd "ConsoleApplication"
dnu restore
dnu build 
dnx ConsoleApplication

やっとvs codeの出番です。

vs codeでの設定

先ほど作成したコンソールアプリケーションの雛形をファイルごと開きます。 雛形では

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.Read();
        }
    }
}

このようなコードになっています。

f:id:mo121_7:20160104215751p:plain

デバッグを選択肢しスタートをクリックします。 f:id:mo121_7:20160105010901p:plain

c#ですのでmonoを選択すとlaunch.jsonが生成されます。

次に「comm+shift+B」を押します。

f:id:mo121_7:20160105011516p:plain

configure taske runnerを選択するとtasks.jsonが生成されます。 現在のtasks.jsonはtypescript用なので書き換える必要があり、次のように変えます。

{
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "xbuild",


    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",
    "taskSelector": "/t:",
    // args is the HelloWorld program to compile.
    "args": ["/property:GenerateFullPaths=true"],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "tasks": [
        {
            "taskName": "build",
            // Show the output window only if unrecognized errors occur.
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings
            // and infos in the output.
            "problemMatcher": "$msCompile"
        }
    ]
}

また、プロジェクトファイルが必要のため作成します。 ConsleApplication.csprojの中身は

<Project DefaultTargets = "Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Specify the inputs by type and file name -->
    <ItemGroup>
        <CSFile Include = "Program.cs"/>
    </ItemGroup>

    <Target Name = "Build">
        <!-- Run the Visual C# compilation using input files of type CSFile -->
        <CSC  Sources = "@(CSFile)"
            DebugType="full"
            Optimize="no"
            OutputAssembly="Program.exe" >

            <!-- Set the OutputAssembly attribute of the CSC task to the name of the executable file that is created -->
            <Output TaskParameter="OutputAssembly"
                ItemName = "EXEFile" />
        </CSC>
    </Target>
</Project>

となります。

f:id:mo121_7:20160105012847p:plain

コンパイル

やっときたコンパイル

tasks.jsonを作った時と同じく[comm+shift+B]を押すと

f:id:mo121_7:20160105013135p:plain

exeファイルができました!!

デバックの画面でスタートのボタンを押すと

f:id:mo121_7:20160105013349p:plain

出力されてますね。

f:id:mo121_7:20160105013543p:plain

きちんと動いてます。

*1:詳しくないのでこちらを

twitterからデータを取ってくる[python]

pythonを使ってtwitterからデータを取ってくる方法について書きたいと思います.

  • twitter開発者に登録
    まずtwitter developerに登録する登録する必要が有ります.また,twitterのアカウントに電話番号を紐付けてないと登録することができないので注意が必要です.
    登録するときはhttps://dev.twitter.com/にアクセスし下の方のTOOLSのManage Your Appsから登録します.
    また、keys and access tokesでaccess tokenを発行する必要があります。

  • tweepyのインストール tweepyさん優秀です。pipから簡単にインストールできます。
    pip install tweepy

  • コード 読み込んだデータをファイル出力してますが標準出力することも可能です

#! /usr/bin/env python
# -*- coding: utf-8 -*-


import tweepy
import json
import codecs
from datetime import datetime

"""
tweepyの詳細は
http://www.tweepy.org/
コードはgithubを参照
https://github.com/tweepy/tweepy/blob/v3.2.0/docs/index.rst
"""


#jsonのdatetimeエラー対策
def support_datetime_default(o):
    if isinstance(o, datetime):
        return o.isoformat()
    raise TypeError(repr(o) + " is not JSON serializable")


#キーワードに対してtweetを読み込む
def connect_tw():
    #CK,CS,AT,ASはtwitterの開発者コード
    CK = '' #Consumer Key
    CS = '' #Consumer Secret
    AT = '' #Access Token
    AS = '' #Accesss Token Secert
    
    #twitterにアクセスする処理
    auth = tweepy.OAuthHandler(CK, CS)
    auth.set_access_token(AT, AS)

    api = tweepy.API(auth)

    #keywordは任意
    keywords = u' テスト'
    #リストを用意する
    list = []
    #ツイートを取得しjson形式で書き込む
    for tweet in tweepy.Cursor(api.search, q=keywords).items(100):
        person = {
                u"user_id": tweet.user.id,
                u"screen_name": tweet.user.screen_name,
                u"description": tweet.user.description,
                u"created_at": tweet.created_at,
                u"text": tweet.text
        }
        list.append(person)
    return list

#json形式でファイルを作成する
def output_data(docs):
    
    d = datetime.now()
    filePath = datetime.strftime(d, "getTweet%Y-%m-%d-%H%M.json")
    f = codecs.open(filePath, 'w', 'utf-8')
    #収集されたデータをJSONファイルに格納する         
    json.dump(docs, f, default=support_datetime_default, indent=4, ensure_ascii=False)
    f.close()


def main():
    print 'get tweet'
    text = connect_tw()
    output_data(text)


if __name__ == "__main__":
    main()
  • 最後に tweepyさん優秀だけどstreamingAPI系が動かないので注意が必要です! streamingAPIについては後日書きたいと思います。